|
@@ -1,12 +1,14 @@
|
|
|
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;
|
|
|
+import net.lingala.zip4j.ZipFile;
|
|
|
import net.lingala.zip4j.exception.ZipException;
|
|
|
import net.lingala.zip4j.model.ZipParameters;
|
|
|
-import net.lingala.zip4j.util.Zip4jConstants;
|
|
|
+import net.lingala.zip4j.model.enums.AesKeyStrength;
|
|
|
+import net.lingala.zip4j.model.enums.CompressionLevel;
|
|
|
+import net.lingala.zip4j.model.enums.CompressionMethod;
|
|
|
+import net.lingala.zip4j.model.enums.EncryptionMethod;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
@@ -136,7 +138,7 @@ public class Zip4jUtil {
|
|
|
}
|
|
|
ZipFile zipFile = new ZipFile(scrPath);
|
|
|
if (Objects.nonNull(password) && zipFile.isEncrypted()) {
|
|
|
- zipFile.setPassword(password);
|
|
|
+ zipFile.setPassword(password.toCharArray());
|
|
|
}
|
|
|
File currentFile = new File(destPath);
|
|
|
if (!currentFile.exists()) {
|
|
@@ -164,41 +166,50 @@ public class Zip4jUtil {
|
|
|
if (!file.exists()) {
|
|
|
file.getParentFile().mkdirs();
|
|
|
}
|
|
|
- // 生成的压缩文件
|
|
|
- ZipFile zipFile = new ZipFile(scrPath);
|
|
|
-// zipFile.setFileNameCharset("UTF-8");
|
|
|
ZipParameters parameters = new ZipParameters();
|
|
|
+ // 压缩方法
|
|
|
+ parameters.setCompressionMethod(CompressionMethod.DEFLATE);
|
|
|
// 压缩级别
|
|
|
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
|
|
|
- // 压缩级别
|
|
|
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
|
|
|
+ parameters.setCompressionLevel(CompressionLevel.NORMAL);
|
|
|
+ // 生成的压缩文件
|
|
|
+ ZipFile zipFile;
|
|
|
if (Objects.nonNull(password)) {
|
|
|
parameters.setEncryptFiles(true);
|
|
|
- parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
|
|
|
- parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
|
|
|
- parameters.setPassword(password);
|
|
|
+ parameters.setEncryptionMethod(EncryptionMethod.AES);
|
|
|
+ parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
|
|
|
+ zipFile = new ZipFile(scrPath, password.toCharArray());
|
|
|
+ } else {
|
|
|
+ zipFile = new ZipFile(scrPath);
|
|
|
}
|
|
|
|
|
|
- File[] fs = null;
|
|
|
- if (!CollectionUtil.isEmpty(files)) {
|
|
|
- // 要打包的文件夹
|
|
|
- fs = files.toArray(new File[files.size()]);
|
|
|
- } else if (Objects.nonNull(destPath)) {
|
|
|
- File currentFile = new File(destPath);
|
|
|
- if (!currentFile.exists()) {
|
|
|
- throw ExceptionResultEnum.ERROR.exception("待压缩的路径不存在");
|
|
|
- }
|
|
|
- fs = currentFile.listFiles();
|
|
|
+ // 3. 添加文件夹到zip
|
|
|
+ File folderToAdd = new File(destPath);
|
|
|
+ if (folderToAdd.isDirectory()) {
|
|
|
+ zipFile.addFolder(folderToAdd, parameters);
|
|
|
+ } else {
|
|
|
+ throw new IllegalArgumentException("提供的路径不是文件夹: " + destPath);
|
|
|
}
|
|
|
|
|
|
- // 遍历test文件夹下所有的文件、文件夹
|
|
|
- for (File f : fs) {
|
|
|
- if (f.isDirectory()) {
|
|
|
- zipFile.addFolder(f.getPath(), parameters);
|
|
|
- } else {
|
|
|
- zipFile.addFile(f, parameters);
|
|
|
- }
|
|
|
- }
|
|
|
+// File[] fs = null;
|
|
|
+// if (!CollectionUtil.isEmpty(files)) {
|
|
|
+// // 要打包的文件夹
|
|
|
+// fs = files.toArray(new File[files.size()]);
|
|
|
+// } 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());
|