我想解壓縮包含文件夾以及文件內的文件的Java存檔。問題在於,只要到達文件夾並嘗試將其解壓縮,就會引發FNF異常。我解壓的代碼如下:Java解壓縮文件夾壓縮存檔FileNotFound異常
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)
請發佈堆棧跟蹤。 –
@SotiriosDelimanolis已添加到原文 – user1806716
我敢肯定,這是因爲你沒有檢查if(inZipEntry.isDirectory()),如果它是一個目錄,使它而不是寫字節。 – tom