|
@@ -1,5 +1,6 @@
|
|
|
package com.qmth.teachcloud.common.util;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
import net.lingala.zip4j.core.ZipFile;
|
|
@@ -12,6 +13,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
|
|
|
/**
|
|
@@ -38,28 +40,7 @@ public class Zip4jUtil {
|
|
|
if (CollectionUtils.isEmpty(files)) {
|
|
|
throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
|
|
|
}
|
|
|
- try {
|
|
|
- // 生成的压缩文件
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- ZipParameters parameters = new ZipParameters();
|
|
|
- // 压缩方式
|
|
|
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
- // 要打包的文件夹
|
|
|
- File[] fs = files.toArray(new File[0]);
|
|
|
- // 遍历test文件夹下所有的文件、文件夹
|
|
|
- for (File f : fs) {
|
|
|
- if (f.isDirectory()) {
|
|
|
- zipFile.addFolder(f.getPath(), parameters);
|
|
|
- } else {
|
|
|
- zipFile.addFile(f, parameters);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
- }
|
|
|
+ commonZipFile(scrPath, null, files, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -74,32 +55,7 @@ public class Zip4jUtil {
|
|
|
throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
|
|
|
}
|
|
|
Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
|
|
|
- try {
|
|
|
- // 生成的压缩文件
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- ZipParameters parameters = new ZipParameters();
|
|
|
- // 压缩方式
|
|
|
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
- // 要打包的文件夹
|
|
|
- File currentFile = new File(destPath);
|
|
|
- if (!currentFile.exists()) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
|
|
|
- }
|
|
|
- File[] fs = currentFile.listFiles();
|
|
|
- // 遍历test文件夹下所有的文件、文件夹
|
|
|
- for (File f : fs) {
|
|
|
- if (f.isDirectory()) {
|
|
|
- zipFile.addFolder(f.getPath(), parameters);
|
|
|
- } else {
|
|
|
- zipFile.addFile(f, parameters);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
- }
|
|
|
+ commonZipFile(scrPath, destPath, null, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -119,34 +75,7 @@ public class Zip4jUtil {
|
|
|
throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
|
|
|
}
|
|
|
Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
|
|
|
- try {
|
|
|
- // 生成的压缩文件
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- ZipParameters parameters = new ZipParameters();
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
- parameters.setEncryptFiles(true);
|
|
|
- parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
|
|
|
- parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
|
|
|
- Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
|
|
|
- parameters.setPassword(password);
|
|
|
-
|
|
|
- // 要打包的文件夹
|
|
|
- File[] fs = files.toArray(new File[0]);
|
|
|
- // 遍历test文件夹下所有的文件、文件夹
|
|
|
- for (File f : fs) {
|
|
|
- if (f.isDirectory()) {
|
|
|
- zipFile.addFolder(f.getPath(), parameters);
|
|
|
- } else {
|
|
|
- zipFile.addFile(f, parameters);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
- }
|
|
|
+ commonZipFile(scrPath, null, files, password);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -163,38 +92,7 @@ public class Zip4jUtil {
|
|
|
}
|
|
|
Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩路径不能为空"));
|
|
|
Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
|
|
|
- try {
|
|
|
- // 生成的压缩文件
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- ZipParameters parameters = new ZipParameters();
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
- parameters.setEncryptFiles(true);
|
|
|
- parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
|
|
|
- parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
|
|
|
- Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("压缩密码不能为空"));
|
|
|
- parameters.setPassword(password);
|
|
|
-
|
|
|
- // 要打包的文件夹
|
|
|
- File currentFile = new File(destPath);
|
|
|
- if (!currentFile.exists()) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
|
|
|
- }
|
|
|
- File[] fs = currentFile.listFiles();
|
|
|
- // 遍历test文件夹下所有的文件、文件夹
|
|
|
- for (File f : fs) {
|
|
|
- if (f.isDirectory()) {
|
|
|
- zipFile.addFolder(f.getPath(), parameters);
|
|
|
- } else {
|
|
|
- zipFile.addFile(f, parameters);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
- }
|
|
|
+ commonZipFile(scrPath, destPath, null, password);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -206,13 +104,7 @@ public class Zip4jUtil {
|
|
|
public static void unzip(String scrPath, String destPath) {
|
|
|
Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
|
|
|
Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
|
|
|
- try {
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- zipFile.extractAll(destPath);
|
|
|
- } catch (ZipException e) {
|
|
|
- log.error(SystemConstant.LOG_ERROR, e);
|
|
|
- throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
- }
|
|
|
+ commonUnZipFile(scrPath, destPath, null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -226,9 +118,20 @@ public class Zip4jUtil {
|
|
|
Optional.ofNullable(scrPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("原始路径不能为空"));
|
|
|
Optional.ofNullable(destPath).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压路径不能为空"));
|
|
|
Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压密码不能为空"));
|
|
|
+ commonUnZipFile(scrPath, destPath, password);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip包
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ * @param password
|
|
|
+ */
|
|
|
+ private static void commonUnZipFile(String scrPath, String destPath, String password) {
|
|
|
try {
|
|
|
ZipFile zipFile = new ZipFile(scrPath);
|
|
|
- if (zipFile.isEncrypted()) {
|
|
|
+ if (Objects.nonNull(password) && zipFile.isEncrypted()) {
|
|
|
zipFile.setPassword(password);
|
|
|
}
|
|
|
zipFile.extractAll(destPath);
|
|
@@ -238,13 +141,53 @@ public class Zip4jUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// public static void main(String[] args) throws ZipException {
|
|
|
-// List<File> fileList = Arrays.asList(new File("/Users/king/Downloads/pdf-temp/pdf/2022/08/12/2ecb7386e70940c585be4bbf062a3f51.pdf"),
|
|
|
-// new File("/Users/king/Downloads/pdf-temp/pdf/2022/08/12/8f67b08558bb4841b0109d95efc7ea4a.pdf"));
|
|
|
-// zipFile("/Users/king/Downloads/test1.zip", fileList);
|
|
|
-// zipFile("/Users/king/Downloads/test2.zip", "/Users/king/Downloads/file-temp");
|
|
|
-// zipEncryptFile("/Users/king/Downloads/test1.zip", "/Users/king/Downloads/pdf-temp", "123456");
|
|
|
-// unzip("/Users/king/Downloads/test2.zip", "/Users/king/Downloads/test2");
|
|
|
-// unzipEncryptFile("/Users/king/Downloads/test1.zip", "/Users/king/Downloads/test1", "123456");
|
|
|
-// }
|
|
|
+ /**
|
|
|
+ * 压缩zip包
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ * @param files
|
|
|
+ * @param password
|
|
|
+ */
|
|
|
+ private static void commonZipFile(String scrPath, String destPath, List<File> files, String password) {
|
|
|
+ try {
|
|
|
+ // 生成的压缩文件
|
|
|
+ ZipFile zipFile = new ZipFile(scrPath);
|
|
|
+ ZipParameters parameters = new ZipParameters();
|
|
|
+ // 压缩级别
|
|
|
+ parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
+ // 压缩级别
|
|
|
+ parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
+ if (Objects.nonNull(password)) {
|
|
|
+ parameters.setEncryptFiles(true);
|
|
|
+ parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
|
|
|
+ parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
|
|
|
+ parameters.setPassword(password);
|
|
|
+ }
|
|
|
+
|
|
|
+ File[] fs = null;
|
|
|
+ if (CollectionUtil.isEmpty(files)) {
|
|
|
+ // 要打包的文件夹
|
|
|
+ fs = files.toArray(new File[0]);
|
|
|
+ } else if (Objects.nonNull(destPath)) {
|
|
|
+ File currentFile = new File(destPath);
|
|
|
+ if (!currentFile.exists()) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
|
|
|
+ }
|
|
|
+ fs = currentFile.listFiles();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历test文件夹下所有的文件、文件夹
|
|
|
+ for (File f : fs) {
|
|
|
+ if (f.isDirectory()) {
|
|
|
+ zipFile.addFolder(f.getPath(), parameters);
|
|
|
+ } else {
|
|
|
+ zipFile.addFile(f, parameters);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|