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)