matrix.boot.common.utils.ZipUtil Maven / Gradle / Ivy
package matrix.boot.common.utils;
import matrix.boot.common.exception.ServiceException;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;
import java.io.File;
import java.nio.charset.StandardCharsets;
/**
* @author wangcheng
* 2023/8/3
**/
public class ZipUtil {
/**
* 文件路径
*/
private String filePath;
private ZipUtil() {
}
private ZipUtil(String filePath) {
this.filePath = filePath;
}
/**
* 获取实例
*
* @param filePath 文件路径
* @return 实例
*/
public static ZipUtil getInstance(String filePath) {
return new ZipUtil(filePath);
}
/**
* zip压缩
*
* @param password 密码
* @param files 文件列表,可以是文件夹
* @return 文件名称
*/
public String doCompress(String password, File... files) {
return doCompress(password, null, files);
}
/**
* zip压缩
*
* @param password 密码
* @param parameters zip参数
* @param files 文件列表,可以是文件夹
* @return 文件名称
*/
public String doCompress(String password, ZipParameters parameters, File... files) {
AssertUtil.state(files != null && files.length > 0, "doCompress params error");
String fileName = RandomUtil.getUUID() + ".zip";
File resultFile = new File(filePath, fileName);
if (parameters == null) {
parameters = new ZipParameters();
parameters.setCompressionMethod(CompressionMethod.DEFLATE); // 压缩方式
parameters.setCompressionLevel(CompressionLevel.NORMAL); // 压缩级别
if (!StringUtil.isEmpty(password)) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD); // 加密方式
}
}
try (ZipFile zipFile = new ZipFile(resultFile, password.toCharArray())) {
zipFile.setCharset(StandardCharsets.UTF_8);
assert files != null;
for (File file : files) {
if (file.isDirectory()) {
zipFile.addFolder(file, parameters);
} else {
zipFile.addFile(file, parameters);
}
}
return fileName;
} catch (Exception e) {
throw new ServiceException(e);
}
}
/**
* 解压
*
* @param zipFileName zip文件名
* @param folderName 解压文件夹名
* @param password 密码
*/
public void doUnCompress(String zipFileName, String folderName, String password) {
doUnCompress(new File(filePath, zipFileName), new File(filePath, folderName), password);
}
/**
* 解压
*
* @param sourceFile 压缩文件路径
* @param destFolder 目标文件夹
* @param password 密码
*/
public void doUnCompress(File sourceFile, File destFolder, String password) {
AssertUtil.state(!destFolder.exists() || destFolder.isDirectory(), "destFolder is file");
if (!destFolder.exists() && !destFolder.mkdir()) {
throw new ServiceException("destFolder create fail");
}
try (ZipFile zipFile = new ZipFile(sourceFile)) {
zipFile.setCharset(StandardCharsets.UTF_8);
AssertUtil.state(zipFile.isValidZipFile(), "ZipFile not valid");
if (zipFile.isEncrypted() && !StringUtil.isEmpty(password)) {
zipFile.setPassword(password.toCharArray());
}
zipFile.extractAll(destFolder.getAbsolutePath());
} catch (Exception e) {
throw new ServiceException(e);
}
}
}