2013-10-15 22 views
8

Arşivdeki dosyaların yanı sıra klasörleri içeren bir arşivdeki java arşivini çıkarmaya çalışıyorum. Sorun, klasörlere her geldiğinde bir FNF istisnası atar ve bunları çözmeyi dener. aşağıdaki gibi Benim unzip kodudur:Java, sıkıştırılmış arşivi klasörlerle sıkıştırın FileNotFound istisnası

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

ben

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

ele klasörleri sıkıştırılmış vardı Ama bu sorunu gidermek için görünmüyor düşündüm. Burada neyi özlüyorum?

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" arşiv içinde bir klasördür.

Ben farklı bir istisna attı
new File(UnzippedFile.getParent()).mkdirs(); 

sonra

unZippedFile.createNewFile(); 

ekleme Daniel'in öneri denedik: o (benim makinede çalışır, bu kodu deneyin

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

Lütfen stacktrace'i gönderin. –

+0

@SotiriosDelimanolis, asıl gönderiye eklenmiştir – user1806716

+1

Eminim, çünkü (inZipEntry.isDirectory()) olup olmadığını kontrol etmediğinizden eminim, eğer bir dizinse, onu bayt yazmak yerine yapın. – tom

cevap

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

Tam olarak nerede olmalıydı? Fileoutputstream'in başlatılmasından önce mi? – user1806716

+0

Cevabımı güncelledim – Daniel

+0

Güncellemeyi denedim, bir şekilde kafası karışmış dosyalar ve dizinler. Umarım windows makinelerinde de aynı şekilde çalışır. – Daniel

1

Dizini önce bir dosya olarak işliyor ve dizinin oluşturulmasını engelleyen boş bir dosya oluşturuyorsunuz.

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

Tam olarak emin olmak zor ama göründüğü gibi. İlk "Unzipping:" satırı, kodunuzun D:\UnzipTest\aspell adında boş bir dosya oluşturduğu zamandan kaynaklanır. Bir sonraki yinelemede, aynı ada sahip bir dizin oluşturmaya çalıştınız ve bu, muhtemelen sessizce başarısızlığa uğradı ve daha sonra başarısızlığa neden oldu.