报错
cn.hutool.core.io.IORuntimeException: ZipException: invalid CEN header (bad entry name)
at cn.hutool.core.util.ZipUtil.toZipFile(ZipUtil.java:68)
at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:542)
at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:517)
at cn.hutool.core.util.ZipUtil.unzip(ZipUtil.java:504)
场景
使用hutool工具类解压文件
String zipPath = "E:\\test\\test.zip";
String destDirPath = "E:\\test";
ZipUtil.unzip(zipPath, destDirPath);
原因
压缩文件编码格式导致,默认编码格式UTF-8,压缩文件编码格式GBK
解决办法
解压时设置编码格式为GBK
String zipPath = "E:\\test\\test.zip";
// 解压目标文件夹
String destDirPath = "E:\\test";
try {
// 解压ZIP文件到目标文件夹
ZipUtil.unzip(zipPath, destDirPath);
}catch (IORuntimeException e){
if(!ObjectUtil.equals(CharsetUtil.defaultCharset(), CharsetUtil.CHARSET_GBK)) {
System.out.println("解压失败,尝试使用GBK编码解压...");
ZipUtil.unzip(zipPath, destDirPath, CharsetUtil.CHARSET_GBK);
}
}