2013-10-15 6 views
8

Ich versuche, ein Archiv in Java zu entpacken, die Ordner sowie Dateien innerhalb des Archivs enthält. Das Problem ist, dass es immer dann eine FNF-Ausnahme auslöst, wenn es die Ordner erreicht und versucht, sie zu entpacken. Mein unzip Code ist wie folgt:Java entpacken komprimiertes Archiv mit Ordnern FileNotFound Ausnahme

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

Ich dachte, ich Ordner mit

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

behandelt komprimiert hatte, aber das scheint nicht das Problem zu beheben. Was fehlt mir hier?

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" ist ein Ordner, der im Archiv war.

Ich habe versucht, von Daniels Vorschlag

Zugabe
unZippedFile.createNewFile(); 

nach

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

, die eine andere Ausnahme geworfen:

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

Bitte senden Sie den Stacktrace. –

+0

@SotiriosDelimanolis wurde zum ursprünglichen Beitrag hinzugefügt – user1806716

+1

Ich bin mir ziemlich sicher, es ist, weil Sie nicht überprüfen, ob (inZipEntry.isDirectory()), Wenn es ein Verzeichnis ist, machen Sie es statt Bytes zu schreiben. – tom

Antwort

5

diesen Code versuchen, es auf meiner Maschine arbeitet (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

Wo genau sollte das sein? Vor der Initialisierung des Dateiausgabestreams? – user1806716

+0

Ich aktualisierte meine Antwort – Daniel

+0

Ich probierte die aktualisierte, es irgendwie verwirrt Dateien und Verzeichnisse. Hoffe es funktioniert auf Windows Maschinen genauso. – Daniel

1

Es scheint, dass Sie das Verzeichnis zuerst als Datei verarbeiten und eine leere Datei erstellen, die die Erstellung des Verzeichnisses verhindert.

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

Es ist schwer, ganz sicher zu sein, aber so sieht es aus. Die erste Zeile "Entpacken:" stammt aus, als Ihr Code eine leere Datei mit dem Namen D:\UnzipTest\aspell erstellt hat. Bei der nächsten Iteration haben Sie versucht, ein Verzeichnis mit dem gleichen Namen zu erstellen, das wahrscheinlich im Hintergrund fehlgeschlagen ist und den späteren Fehler verursacht hat.