我正在尝试在 java 中解压缩包含文件夹以及存档内文件的存档。问题在于,每当它到达文件夹并尝试解压缩它们时,它都会引发 FNF 异常。我的解压代码如下:
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();
}
}
我以为我已经处理了压缩文件夹
new File(unZippedFile.getParent()).mkdirs();
但这似乎并不能解决问题。我在这里想念什么?
堆栈跟踪:
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”是存档内的一个文件夹。
我尝试了丹尼尔的添加建议
unZippedFile.createNewFile();
后
new File(UnzippedFile.getParent()).mkdirs();
这引发了一个不同的例外:
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)