package com.whyc.util;
|
|
import net.lingala.zip4j.core.ZipFile;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.File;
|
|
public class Zip4jUtil {
|
|
public static boolean checkZipFileParam(MultipartFile zipFile) {
|
String contentType = zipFile.getContentType();
|
//【导入Zip文件】上传的文件类型不是Zip
|
return "application/zip".equals(contentType) || "application/x-zip-compressed".equals(contentType);
|
}
|
|
/**
|
* zip文件解压
|
*
|
* @param destPath 解压文件路径
|
* @param zipFile 压缩文件
|
* @param password 解压密码(如果有)
|
*/
|
public static boolean unPackZip(File zipFile, String password, String destPath) {
|
try {
|
ZipFile zip = new ZipFile(zipFile);
|
/*zip4j默认用GBK编码去解压,这里设置编码为GBK的*/
|
zip.setFileNameCharset("GBK");
|
System.out.println("begin unpack zip file....");
|
zip.extractAll(destPath);
|
// 如果解压需要密码
|
if (password != null) {
|
if (zip.isEncrypted()) {
|
zip.setPassword(password);
|
}
|
}
|
} catch (Exception e) {
|
return false;
|
}
|
return true;
|
}
|
}
|