2013-10-15 12 views
8

Tôi đang cố giải nén một tệp lưu trữ trong java có chứa các thư mục cũng như các tệp bên trong tệp lưu trữ. Vấn đề là nó ném một ngoại lệ FNF bất cứ khi nào nó được vào các thư mục và cố gắng giải nén chúng. đang giải nén của tôi là như sau:Java giải nén lưu trữ nén với các thư mục FileNotFound ngoại lệ

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(); 
     } 
    } 

tôi nghĩ rằng tôi đã nén thư mục xử lý với

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

Nhưng điều đó dường như không khắc phục vấn đề. Tôi đang thiếu gì ở đây?

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" là một thư mục mà là bên trong các kho lưu trữ.

Tôi cố gắng gợi ý Daniel thêm

unZippedFile.createNewFile(); 

sau

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

Đó ném một ngoại lệ khác nhau:

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

Vui lòng đăng stacktrace. –

+0

@SotiriosDelimanolis được thêm vào bài đăng gốc – user1806716

+1

Tôi khá chắc chắn đó là vì bạn không kiểm tra nếu (inZipEntry.isDirectory()), Nếu đó là một thư mục, hãy thay vì viết byte. – tom

Trả lời

5

Hãy thử mã này, nó hoạt động trên máy tính của tôi (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

Chính xác thì ở đâu? Trước khi khởi tạo fileoutputstream? – user1806716

+0

Tôi đã cập nhật câu trả lời của mình – Daniel

+0

Tôi đã thử các tệp và thư mục đã được cập nhật, bằng cách nào đó đã nhầm lẫn. Hy vọng nó hoạt động trên các máy tính Windows theo cùng một cách. – Daniel

1

Dường như bạn đang xử lý thư mục dưới dạng tệp trước và tạo tệp trống ngăn cản việc tạo thư mục.

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

Thật khó để hoàn toàn chắc chắn nhưng đó là hình thức của nó. Dòng "Giải nén:" đầu tiên là từ khi mã của bạn tạo ra một tệp trống có tên D:\UnzipTest\aspell. Trong lần lặp tiếp theo, bạn đã cố gắng tạo một thư mục có cùng tên và thất bại, có thể âm thầm, gây ra lỗi sau.

Các vấn đề liên quan