Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unzip compressed archive with folders FileNotFound exception

I am trying to unzip an archive in java that contains folders as well as files inside of the archive. The issue is that it throws a FNF exception whenever it gets to the folders and tries to unzip them. My unzip code is as follows:

 private void unZipUpdate(String pathToUpdateZip, String destinationPath){
       byte[] byteBuffer = new byte[1024];

       try{
           ZipInputStream inZip = new ZipInputStream(new FileInputStream(pathToUpdateZip));
           ZipEntry inZipEntry = inZip.getNextEntry();
           while(inZipEntry != null){
               String fileName = inZipEntry.getName();
               File unZippedFile = new File(destinationPath + File.separator + fileName);


               System.out.println("Unzipping: " + unZippedFile.getAbsoluteFile());
               new File(unZippedFile.getParent()).mkdirs();


               FileOutputStream unZippedFileOutputStream = new FileOutputStream(unZippedFile);
               int length;
               while((length = inZip.read(byteBuffer)) > 0){
                   unZippedFileOutputStream.write(byteBuffer,0,length);
               }
               unZippedFileOutputStream.close();
               inZipEntry = inZip.getNextEntry();
           }
           inZipEntry.clone();
           inZip.close();
           System.out.println("Finished Unzipping");
       }catch(IOException e){
           e.printStackTrace();
       }
    }

I thought I had compressed folders handled with

new File(unZippedFile.getParent()).mkdirs();

But that doesn't seem to fix the issue. What am I missing here?

Stacktrace:

Unzipping: D:\UnzipTest\aspell
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
    at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:47)
    at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:33)
    at shopupdater.ShopUpdater.main(ShopUpdater.java:67)

"aspell" is a folder that was inside the archive.

I tried Daniel's suggestion of adding

unZippedFile.createNewFile();

after

new File(UnzippedFile.getParent()).mkdirs();

That threw a different exception of:

Unzipping: D:\UnzipTest\aspell
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
    at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:56)
    at shopupdater.ShopUpdater.unZipUpdate(ShopUpdater.java:33)
    at shopupdater.ShopUpdater.main(ShopUpdater.java:76)
like image 423
Giardino Avatar asked Oct 15 '13 20:10

Giardino


2 Answers

Try this code, it works on my machine (ubuntu)

private static void unZipUpdate(String pathToUpdateZip, String destinationPath){
       byte[] byteBuffer = new byte[1024];

       try{
           ZipInputStream inZip = new ZipInputStream(new FileInputStream(pathToUpdateZip));
           ZipEntry inZipEntry = inZip.getNextEntry();
           while(inZipEntry != null){
               String fileName = inZipEntry.getName();
               File unZippedFile = new File(destinationPath + File.separator + fileName);
               System.out.println("Unzipping: " + unZippedFile.getAbsoluteFile());
               if (inZipEntry.isDirectory()){
                   unZippedFile.mkdirs();
               }else{
                   new File(unZippedFile.getParent()).mkdirs();
                   unZippedFile.createNewFile();
                   FileOutputStream unZippedFileOutputStream = new FileOutputStream(unZippedFile);
                   int length;
                   while((length = inZip.read(byteBuffer)) > 0){
                       unZippedFileOutputStream.write(byteBuffer,0,length);
                   }
                   unZippedFileOutputStream.close();                       
               }
               inZipEntry = inZip.getNextEntry(); 
           }
           //inZipEntry.close();
           inZip.close();
           System.out.println("Finished Unzipping");
       }catch(IOException e){
           e.printStackTrace();
       }
    }
like image 181
Daniel Avatar answered Nov 04 '22 12:11

Daniel


It appears you are processing the directory as a file first and creating an empty file that prevents creation of the directory.

Unzipping: D:\UnzipTest\aspell
Unzipping: D:\UnzipTest\aspell\american-w-accents.alias
java.io.FileNotFoundException: D:\UnzipTest\aspell\american-w-accents.alias

It's hard to be completely sure but that's what it looks like. The first "Unzipping:" line is from when your code created an empty file named D:\UnzipTest\aspell. On the next iteration you tried to create a directory with the same name, and that failed, probably silently, causing the later failure.

like image 37
Jim Garrison Avatar answered Nov 04 '22 10:11

Jim Garrison