Search code examples
javazipapache-commonsunzip

Getting FileNotFoundException while unzipping a zip file though Java


I have a method that unzips zip files through Java.

This method works fine.

But recently I was trying to unzip a certain file(final output.zip) using this method but got a runtime exception.

Below is the code for that method:

public static List<String> unzipFiles(File zipFile, File targetDirectory) {
    List<String> files = new ArrayList<String>();                       
    try (FileInputStream fileInputStream = new FileInputStream(zipFile); ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new BufferedInputStream(fileInputStream));){                  
        ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextZipEntry()) != null) {
            int length;
            byte data[] = new byte[bufferGlb];
            String fileName = zipEntry.getName();
            File opFile = new File(targetDirectory, fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(opFile);//This is the line where the exception is thrown
            BufferedOutputStream dest = new BufferedOutputStream(fileOutputStream, bufferGlb);
            while ((length = zipInputStream.read(data, 0, bufferGlb)) != -1) {
                dest.write(data, 0, length);
            }
            dest.flush();
            files.add(fileName);
            fileOutputStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return files;
}
//library import used:
//import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;

It is important to note that I am able to successfully unzip this file using WinRar.

The line at which I am getting the exception is marked with comments in the above code.

The stack trace of the exception that I got is below :

java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
    at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
    at ZipUtility.unzipFiles(ZipUtility.java:281)

Also the file mentioned in the exception, 0123450.pdf is actually there inside the zip file. Please help.


Solution

  • I had faced a similar issue before. The file mentioned in the exception:

    java.io.FileNotFoundException: final output/0123450.pdf (No such file or directory)
        at java.io.FileOutputStream.open0(Native Method) ~[?:1.8.0_191]
        at java.io.FileOutputStream.open(FileOutputStream.java:270) ~[?:1.8.0_191]
        at java.io.FileOutputStream.<init>(FileOutputStream.java:213) ~[?:1.8.0_191]
        at java.io.FileOutputStream.<init>(FileOutputStream.java:162) ~[?:1.8.0_191]
        at ZipUtility.unzipFiles(ZipUtility.java:281)
    

    i.e 0123450.pdf is actually inside a folder final output and not directly inside the zip file. Try using a zip file that contains the files directly inside it