java使用解压zip文件,文件名乱码解决方案

时间:2024-01-31 12:23:37
File outFileDir = new File(outDir);
if (!outFileDir.exists()) {
boolean isMakDir = outFileDir.mkdirs();
if (isMakDir) {
log.info("创建压缩目录成功");
}
}
ZipFile zip = new ZipFile(zipFile, "gbk");
for (Enumeration enumeration = zip.getEntries(); enumeration.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
String zipEntryName = entry.getName();
InputStream in = zip.getInputStream(entry);

if (entry.isDirectory()) { //处理压缩文件包含文件夹的情况
File fileDir = new File(outDir + zipEntryName);
fileDir.mkdir();
continue;
}

File file = new File(outDir, zipEntryName);
file.createNewFile();
OutputStream out = new FileOutputStream(file);
byte[] buff = new byte[1024];
int len;
while ((len = in.read(buff)) > 0) {
out.write(buff, 0, len);
}
in.close();
out.close();
}