FileNotFoundException when using FileOutputStream

I need to extract tar file by this code. But it' this error when I use FileOutputStream(outputFile);

     " java.io.FileNotFoundException: D:\TestFile\1.png (Access is denied)"

Input is 1.tar file from Drive D:/testFile and extract to same folder

I try check path of outputfile by outputFile.getCanonicalFile but it's ok!! what wrong?

   public class DecompressTarFile {

public ArrayList<File> getTarFileExtracted() {
    return tarFileExtracted;
}

public ArrayList<File> tarFileExtracted = new ArrayList<File>();

public DecompressTarFile(File inputFile, File outputDir) throws FileNotFoundException, ArchiveException, IOException {

    System.out.println("Untaring " + inputFile.getAbsolutePath() + " to dir " + outputDir.getAbsolutePath() + ".");
    InputStream inputStream = new FileInputStream(inputFile);
    TarArchiveInputStream tarStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", inputStream);
    TarArchiveEntry tarEntry = null;
    while ((tarEntry = (TarArchiveEntry) tarStream.getNextEntry()) != null) {
        File outputFile = new File(outputDir, tarEntry.getName());
        System.out.println("Attempting to write output directory " + outputFile.getAbsolutePath());
        if (!outputFile.exists()) {
            System.out.println("Attempting to create output directory " + outputFile.getAbsolutePath());
            if (!outputFile.mkdirs()) {
                throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
            }

        } else {
            System.out.println("Create output file " + outputFile.getAbsolutePath());     

            OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(tarStream, outputFileStream);
            outputFileStream.close();

        }
        tarFileExtracted.add(outputFile);
    }
    tarStream.close();

}

}

and called by main class

 static File tarFileInput = new File("D:/TestFile/haha.tar");
static File tarPathFileOutput = new File("D:/TestFile");

 public static void main(String[] args) throws ArchiveException, IOException {

    decomp = new DecompressTarFile(tarFileInput, tarPathFileOutput);
    //listOutput = decomp.getTarFileExtracted();

}

And this result from this code

   run:
    Untaring D:\TestFile\haha.tar to dir D:\TestFile.
    Attempting to write output directory D:\TestFile\1.png
    Create output file D:\TestFile\1.png
    Exception in thread "main" java.io.FileNotFoundException: D:\TestFile\1.png (Access  is denied)
     at java.io.FileOutputStream.open(Native Method)
     at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
     at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
     at com.service..TarFile.DecompressTarFile.<init>(DecompressTarFile.java:53)
      at com.service..TarFile.MainForTest.main(MainForTest.java:28)

Java Result: 1

Jon Skeet
people
quotationmark

This is the problem:

Attempting to write output directory D:\TestFile\1.png

You've created the target filename as a directory. You need to separate out "the directory I need to exist" and "the file I want to write to". For example:

File outputFile = new File(outputDir, tarEntry.getName());
File outputDirectory = outputFile.getParent();
if (!outputDirectory.exists()) {
    // Try to create the directory
}

Oh, and also you've got a loop condition of:

while (tarEntry != null)

... but you're not changing the value of tarEntry in the loop...

people

See more on this question at Stackoverflow