2016-04-08 25 views
0

ile açabilmem mümkün değil. Bir dosyayı java kullanarak açmaya çalışıyorum ama aşağıdaki kod while döngüsünü 'ze' null olarak girmiyor. Ancak aynı dosya 7zip uygulamasını kullanarak unzip mümkün. Birisi bana neden böyle bir şey olduğunu bildirir mi?Bir dosyayı java kullanarak açamıyor ama 7zip

try {

 //get the zip file content 
     ZipInputStream zis = 
      new ZipInputStream(new FileInputStream(zipFile)); 
     //get the zipped file list entry 
     ZipEntry ze = zis.getNextEntry(); 

     while(ze!=null){ 

      String fileName = ze.getName(); 
      File newFile = new File(outputFolder + File.separator + fileName); 

      System.out.println("file unzip : "+ newFile.getAbsoluteFile()); 

      //create all non exists folders 
      //else you will hit FileNotFoundException for compressed folder 
      new File(newFile.getParent()).mkdirs(); 

      FileOutputStream fos = new FileOutputStream(newFile);    

      int len; 
      while ((len = zis.read(buffer)) > 0) { 
      fos.write(buffer, 0, len); 
      } 

      fos.close(); 
      ze = zis.getNextEntry(); 
     } 

     zis.closeEntry(); 
     zis.close(); 

     System.out.println("Done"); 

    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
+0

Bir .7z dosyası mı, yoksa bir .zip dosyası mı? – AJNeufeld

cevap

1

burada Javadocs: https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html başka girdi yoksa getNextEntry() boş döneceğini söylüyor. Zip dosyanızın aslında içinde dosya içerip içermediğini veya boş olduğunu kontrol edin.

Kodunuzu, dosyaları içeren bir zip ile denedim ve düzgün çalıştı. Ben boş bir zip dosyası ile denedim ve boş dosya için ze null oldu ve böylece süre döngü girmedi.

+0

Bunu sıralıyorum. Sorun benim dosyamın sıkıştırılmış ve sıkıştırılmış değildi. GZIPInputStream kullandı ve iyi çalıştı :) – user1496783

+0

Yardım için teşekkürler @posiedon – user1496783