From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001
From: sws <15810472099@163.com>
Date: 星期六, 26 十一月 2022 16:12:02 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/helper/Zip4jHelper.java |  115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/Zip4jHelper.java b/src/main/java/com/lf/server/helper/Zip4jHelper.java
new file mode 100644
index 0000000..2cf0b41
--- /dev/null
+++ b/src/main/java/com/lf/server/helper/Zip4jHelper.java
@@ -0,0 +1,115 @@
+package com.lf.server.helper;
+
+import com.google.common.base.Strings;
+import net.lingala.zip4j.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+import net.lingala.zip4j.model.ZipParameters;
+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.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.File;
+import java.nio.charset.Charset;
+
+/**
+ * Zip4j甯姪绫�
+ * @author WWW
+ */
+public class Zip4jHelper {
+    private final static Log log = LogFactory.getLog(Zip4jHelper.class);
+
+    /**
+     * Zip鍘嬬缉
+     *
+     * @param zipFilePath zip鏂囦欢
+     * @param sourcePath  婧愯矾寰�
+     * @param password    瀵嗙爜
+     * @return 鎴愬姛鏄惁
+     */
+    public static boolean zip(String zipFilePath, String sourcePath, String password) {
+        try {
+            ZipFile zip = new ZipFile(zipFilePath);
+            zip.setCharset(Charset.forName("UTF-8"));
+
+            File f = zip.getFile();
+            if (!f.getParentFile().exists()) {
+                f.getParentFile().mkdirs();
+            }
+            if (f.exists()) {
+                f.delete();
+            }
+
+            // 璁剧疆鍘嬬缉鏂囦欢鍙傛暟
+            ZipParameters params = new ZipParameters();
+            // 鍘嬬缉鏂瑰紡
+            params.setCompressionMethod(CompressionMethod.DEFLATE);
+            // 鍘嬬缉绾у埆
+            params.setCompressionLevel(CompressionLevel.NORMAL);
+            // 鏄惁璁剧疆鍔犲瘑鏂囦欢
+            params.setEncryptFiles(true);
+            // 璁剧疆AES鍔犲瘑寮哄害锛欿EY_STRENGTH_256
+            params.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
+            // 璁剧疆鍔犲瘑绠楁硶
+            params.setEncryptionMethod(EncryptionMethod.AES);
+            // 璁剧疆瀵嗙爜
+            if (!Strings.isNullOrEmpty(password)) {
+                zip.setPassword(password.toCharArray());
+            }
+
+            // 瑕佹墦鍖呯殑鏂囦欢鎴栨枃浠跺す
+            File currentFile = new File(sourcePath);
+            if (currentFile.isDirectory()) {
+                zip.addFolder(currentFile, params);
+            } else {
+                zip.addFile(currentFile, params);
+            }
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return false;
+        }
+    }
+
+    private static void addZipFile(ZipFile zip, ZipParameters params, File file) throws ZipException {
+        if (file.isDirectory()) {
+            File[] files = file.listFiles();
+            for (File f : files) {
+                addZipFile(zip, params, f);
+            }
+        } else {
+            zip.addFile(file, params);
+        }
+    }
+
+    /**
+     * Zip瑙e帇
+     *
+     * @param zipFile  zip鏂囦欢
+     * @param toPath   瑙e帇璺緞
+     * @param password 瀵嗙爜
+     * @return 鎴愬姛鏄�/鍚�
+     */
+    public static boolean unzip(String zipFile, String toPath, String password) {
+        try {
+            // 鐢熸垚鐨勫帇缂╂枃浠�
+            ZipFile zip = new ZipFile(zipFile);
+
+            // 璁剧疆瀵嗙爜
+            if (!StringHelper.isEmpty(password)) {
+                zip.setPassword(password.toCharArray());
+            }
+
+            // 瑙e帇缂╂墍鏈夋枃浠朵互鍙婃枃浠跺す
+            zip.extractAll(toPath);
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return false;
+        }
+    }
+}

--
Gitblit v1.9.3