String originalFileName = multipartfile.getOriginalFilename();
String unzipName = originalFileName.substring(0, originalFileName.lastIndexOf("."));
StringBuilder sb = new StringBuilder();
sb.append(tmpPath);
sb.append(unzipName);
sb.append("_");
sb.append(System.currentTimeMillis());
sb.append(".");
sb.append("zip");
originalFileName = sb.toString();
log.info("rename unzip name is {}", originalFileName);
File tmpDir = new File(originalFileName);
try {
multipartfile.transferTo(tmpDir);
} catch (IOException e) {
log.error("压缩包保存临时路径下失败", e.getMessage(), e);
}
UnzipUtility.unzip(tmpDir.getAbsolutePath(), tmpZipPath);
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
ZipFile zipFile = new ZipFile(zipFilePath,"GBK");
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipFile, entry, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
}
zipFile.close();
}
注意windows压缩不要使用LZMA算法,否则解压会失败