一、分析下原因(PS:参考大佬的博客地址:https://blog.csdn.net/zbj18314469395/article/details/84109499)
二、解决办法:
把判断条件由【(ze=zin.getNextEntry())!=null】换成【zipEnum.hasMoreElements ()】
使用了枚举, Enumeration 接口(枚举)。
通常用 Enumeration 中的以下两个方法打印向量中的所有元素:
(1) boolean hasMoreElements(); // 是否还有元素,如果返回 true ,则表示至少含有一个元素
(2) public Object nextElement(); // 如果 Enumeration 枚举对象还含有元素,该方法返回对象中的下一个元素。如果没有,则抛出NoSuchElementException 异常。
附上我的代码,已经修改过的,大家可以参考一下
public void processFile(String path) throws Exception {
ZipFile zf = new ZipFile(path);
//主要是这块
java.util.Enumeration zipEnum = zf.entries();
InputStream in = new BufferedInputStream(new FileInputStream(path));
ZipInputStream zin = new ZipInputStream(in, Charset.forName("gbk"));
ZipEntry ze;
//重点是是这块的判断
while (zipEnum.hasMoreElements()) {
ze = (ZipEntry) zipEnum.nextElement();
if (!ze.getName().contains("__MACOSX") && !ze.getName().contains(".DS_Store")) {
if (!ze.isDirectory()) {
System.out.println("##name##" + ze.getName());
System.out.println("##size##" + ze.getSize());
}
}
}
zin.closeEntry();
}