[英]Unable to unzip a file using java but able to unzip it using 7zip
我正在尝试使用Java解压缩文件,但以下代码未进入while循环,因为'ze'为null。 但是,我可以使用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();
}
此处的javadocs: https ://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html表示,如果没有更多条目,getNextEntry()将返回null。 检查您的zip文件中是否实际包含文件或该文件是否为空。
我用包含文件的zip尝试了您的代码,并且运行正常。 我用一个空的zip文件尝试过该操作,并且ze对于空文件为null,因此它没有进入while循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.