|
@@ -0,0 +1,240 @@
|
|
|
+package com.qmth.teachcloud.common.util;
|
|
|
+
|
|
|
+import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
|
+import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
+import net.lingala.zip4j.core.ZipFile;
|
|
|
+import net.lingala.zip4j.exception.ZipException;
|
|
|
+import net.lingala.zip4j.model.ZipParameters;
|
|
|
+import net.lingala.zip4j.util.Zip4jConstants;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: zip4j工具
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: wangliang
|
|
|
+ * @Date: 2022/8/12
|
|
|
+ */
|
|
|
+public class Zip4jUtil {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(HttpKit.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param files
|
|
|
+ */
|
|
|
+ public static void zipFile(String scrPath, List<File> files) {
|
|
|
+ try {
|
|
|
+ if (!scrPath.endsWith(".zip")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(files)) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("没有待压缩的文件");
|
|
|
+ }
|
|
|
+ // 生成的压缩文件
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ * @throws ZipException
|
|
|
+ */
|
|
|
+ public static void zipFile(String scrPath, String destPath) throws ZipException {
|
|
|
+ if (!scrPath.endsWith(".zip")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip带密码
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param files
|
|
|
+ * @param password
|
|
|
+ * @throws ZipException
|
|
|
+ */
|
|
|
+ public static void zipEncryptFile(String scrPath, List<File> files, String password) throws ZipException {
|
|
|
+ if (!scrPath.endsWith(".zip")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 压缩zip带密码
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ * @param password
|
|
|
+ * @throws ZipException
|
|
|
+ */
|
|
|
+ public static void zipEncryptFile(String scrPath, String destPath, String password) throws ZipException {
|
|
|
+ if (!scrPath.endsWith(".zip")) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("压缩文件必须为zip");
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ */
|
|
|
+ public static void unzip(String scrPath, String destPath) {
|
|
|
+ try {
|
|
|
+ ZipFile zipFile = new ZipFile(scrPath);
|
|
|
+ zipFile.extractAll(destPath);
|
|
|
+ } catch (ZipException e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解压zip带密码
|
|
|
+ *
|
|
|
+ * @param scrPath
|
|
|
+ * @param destPath
|
|
|
+ * @param password
|
|
|
+ */
|
|
|
+ public static void unzipEncryptFile(String scrPath, String destPath, String password) {
|
|
|
+ Optional.ofNullable(password).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("解压密码不能为空"));
|
|
|
+ try {
|
|
|
+ ZipFile zipFile = new ZipFile(scrPath);
|
|
|
+ if (zipFile.isEncrypted()) {
|
|
|
+ zipFile.setPassword(password);
|
|
|
+ }
|
|
|
+ zipFile.extractAll(destPath);
|
|
|
+ } catch (ZipException e) {
|
|
|
+ log.error(SystemConstant.LOG_ERROR, e);
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// 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");
|
|
|
+// }
|
|
|
+}
|