2013-10-15 186 views
8

我想解壓縮包含文件夾以及文件內的文件的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) 
+0

請發佈堆棧跟蹤。 –

+0

@SotiriosDelimanolis已添加到原文 – user1806716

+1

我敢肯定,這是因爲你沒有檢查if(inZipEntry.isDirectory()),如果它是一個目錄,使它而不是寫字節。 – tom

回答

5

試試這個代碼,它的工作原理在我的機器上(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(); 
     } 
    } 
+0

究竟應該在哪裏?在fileoutputstream的初始化之前? – user1806716

+0

我更新了我的答案 – Daniel

+0

我嘗試了更新,它以某種方式混淆了文件和目錄。希望它能以同樣的方式在Windows機器上運行。 – Daniel

1

看起來您正在將目錄作爲文件首先處理並創建一個空文件,以防止創建該目錄。

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

很難完全確定,但這就是它的樣子。第一個「Unzipping:」行來自你的代碼創建一個名爲D:\UnzipTest\aspell的空文件。在下一次迭代中,您試圖創建一個名稱相同的目錄,並且失敗了,可能是默默無聞,導致以後的失敗。