2016-04-08 6 views
0

javaを使用してファイルを解凍しようとしていますが、 'ze'がnullのためwhileループに入りません。しかし、私は7zipアプリケーションを使用して解凍することができます同じファイル。誰かが私になぜこれが幸せであるかを教えてもらえますか?Javaを使用してファイルを解凍することはできませんが、7zipを使用してファイルを解凍することはできません。

試し{ここ

 //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

.7zファイル、または.zipファイルですか? – AJNeufeld

答えて

1

のJavadoc:https://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.htmlにはより多くのエントリがない場合getNextEntry()はnullを返しますと言っています。 zipファイルに実際にファイルが含まれているか、空であるかどうかを確認します。

ファイルを含むzipでコードを試しましたが、正しく実行されました。空のzipファイルで試してみましたが、空のファイルではzeがnullだったので、whileループには入りませんでした。

+0

私はそれを選別した。問題は、私のファイルがgzipされ、圧縮されていないことでした。私はGZIPInputStreamを使用し、それは正常に実行されました:) – user1496783

+0

@posiedon – user1496783

関連する問題