java 解压文件
点击查看 vue spring boot 上传 文件
String zipName = "D:\\file\\test.zip";
String targetDirName = "D:\\upZip"
public static File upzipFile(String zipName, String targetDirName) throws Exception {
if (!targetDirName.endsWith(File.separator)) {
targetDirName += File.separator;
}
String fileEncode = EncodeUtil.getEncode(zipName, true);
try {
ZipFile zipFile = new ZipFile(zipName, Charset.forName(fileEncode));
ZipEntry zn = null;
String entryName = null;
String targetFileName = null;
byte[] buffer = new byte[4096];
int bytes_read;
Enumeration entrys = zipFile.entries();
File targetFile = null;
FileOutputStream os = null;
InputStream is = null;
while (entrys.hasMoreElements()) {
zn = (ZipEntry) entrys.nextElement();
System.out.println("zn:" + zn);
entryName = zn.getName();
System.out.println("entryName:" + entryName);
targetFileName = targetDirName + entryName;
if (zn.isDirectory()) {
new File(targetFileName).mkdirs();
continue;
} else {
new File(targetFileName).getParentFile().mkdirs();
}
targetFile = new File(targetFileName);
System.out.println("正在创建文件:" + targetFile.getAbsolutePath());
os = new FileOutputStream(targetFile);
is = zipFile.getInputStream(zn);
while ((bytes_read = is.read(buffer)) != -1) {
os.write(buffer, 0, bytes_read);
}
}
os.close();
is.close();
zipFile.close();
System.out.println("解压缩" + zipName + "成功!");
return targetFile;
} catch (IOException err) {
System.err.println("解压缩" + zipName + "失败: " + err);
}
return null;
}