From 3249d41536899bbacf88af45f290db3efdbdc790 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 20 十月 2022 17:28:32 +0800
Subject: [PATCH] zip4j解压

---
 src/main/java/com/lf/server/config/InitConfig.java  |    5 +
 src/main/java/com/lf/server/helper/ZipHelper.java   |   10 +-
 src/main/java/com/lf/server/helper/Zip4jHelper.java |  115 ++++++++++++++++++++++++++++++++++++++
 pom.xml                                             |    6 ++
 4 files changed, 130 insertions(+), 6 deletions(-)

diff --git a/pom.xml b/pom.xml
index eee50ee..e276782 100644
--- a/pom.xml
+++ b/pom.xml
@@ -192,6 +192,12 @@
             <artifactId>gdal</artifactId>
             <version>3.2.0</version>
         </dependency>
+        <!--zip4j-->
+        <dependency>
+            <groupId>net.lingala.zip4j</groupId>
+            <artifactId>zip4j</artifactId>
+            <version>2.6.4</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/com/lf/server/config/InitConfig.java b/src/main/java/com/lf/server/config/InitConfig.java
index 66f5f24..961742c 100644
--- a/src/main/java/com/lf/server/config/InitConfig.java
+++ b/src/main/java/com/lf/server/config/InitConfig.java
@@ -6,6 +6,7 @@
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.lf.server.entity.bd.DlgAgnpEntity;
 import com.lf.server.helper.PathHelper;
+import com.lf.server.helper.Zip4jHelper;
 import com.lf.server.helper.ZipHelper;
 import com.lf.server.mapper.bd.DlgAgnpMapper;
 import com.lf.server.service.sys.ArgsService;
@@ -51,7 +52,9 @@
             //testMybatisPlus();
 
             //boolean f1 = ZipHelper.unzip("D:\\LF\\data\\resources.zip", "D:\\LF\\data\\unzip");
-            boolean f2 = ZipHelper.zip("D:\\LF\\data\\res.zip", "D:\\LF\\data\\unzip\\resources");
+            //boolean f2 = ZipHelper.zip("D:\\LF\\data\\res.zip", "D:\\LF\\data\\unzip\\resources");
+            //boolean f3 = Zip4jHelper.zip("D:\\LF\\data\\zip\\resources.zip", "D:\\LF\\data\\zip\\resources", "12345");
+            //boolean f4 = Zip4jHelper.unzip("D:\\LF\\data\\zip\\resources.zip", "D:\\LF\\data\\zip\\res", "12345");
 
             // 鍒濆鍖�
             pathHelper.init();
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..ebd7f80
--- /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.getStackTrace() + "\n");
+            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 (!Strings.isNullOrEmpty(password)) {
+                zip.setPassword(password.toCharArray());
+            }
+
+            // 瑙e帇缂╂墍鏈夋枃浠朵互鍙婃枃浠跺す
+            zip.extractAll(toPath);
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
+            return false;
+        }
+    }
+}
diff --git a/src/main/java/com/lf/server/helper/ZipHelper.java b/src/main/java/com/lf/server/helper/ZipHelper.java
index 0b21fe2..09d4af5 100644
--- a/src/main/java/com/lf/server/helper/ZipHelper.java
+++ b/src/main/java/com/lf/server/helper/ZipHelper.java
@@ -21,7 +21,7 @@
     private final static Log log = LogFactory.getLog(ZipHelper.class);
 
     /**
-     * 瑙e帇缂�
+     * Zip瑙e帇
      *
      * @param filePath zip鏂囦欢
      * @param zipDir   瑙e帇璺緞
@@ -65,7 +65,7 @@
     /**
      * Zip鍘嬬缉
      *
-     * @param zipFile   Zip婧愭枃浠�
+     * @param zipFile   zip婧愭枃浠�
      * @param sourceDir 婧愭枃浠跺す
      * @return 鎴愬姛鏄�/鍚�
      */
@@ -96,9 +96,9 @@
     /**
      * Zip鍘嬬缉
      *
-     * @param sourceFile Zip婧愭枃浠�
-     * @param zos        Zip杈撳嚭娴�
-     * @param name       Zip鏂囦欢鍚嶇О
+     * @param sourceFile zip婧愭枃浠�
+     * @param zos        zip杈撳嚭娴�
+     * @param name       zip鏂囦欢鍚嶇О
      * @throws Exception
      */
     public static void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception {

--
Gitblit v1.9.3