From b6b0cb226fcf184525ee7b36af3a09471e9c0057 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期一, 25 三月 2024 11:29:33 +0800
Subject: [PATCH] 修改数据统计的查询条件

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

diff --git a/src/main/java/com/lf/server/helper/Zip4jHelper.java b/src/main/java/com/lf/server/helper/Zip4jHelper.java
index ebd7f80..5440578 100644
--- a/src/main/java/com/lf/server/helper/Zip4jHelper.java
+++ b/src/main/java/com/lf/server/helper/Zip4jHelper.java
@@ -1,6 +1,5 @@
 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;
@@ -13,6 +12,7 @@
 
 import java.io.File;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 /**
  * Zip4j甯姪绫�
@@ -24,15 +24,15 @@
     /**
      * Zip鍘嬬缉
      *
-     * @param zipFilePath zip鏂囦欢
-     * @param sourcePath  婧愯矾寰�
-     * @param password    瀵嗙爜
+     * @param zipFile    zip鏂囦欢
+     * @param sourcePath 婧愯矾寰�
+     * @param pwd        瀵嗙爜
      * @return 鎴愬姛鏄惁
      */
-    public static boolean zip(String zipFilePath, String sourcePath, String password) {
+    public static boolean zip(String zipFile, String sourcePath, String pwd) {
         try {
-            ZipFile zip = new ZipFile(zipFilePath);
-            zip.setCharset(Charset.forName("UTF-8"));
+            ZipFile zip = StringHelper.isEmpty(pwd) ? new ZipFile(zipFile) : new ZipFile(zipFile, pwd.toCharArray());
+            zip.setCharset(StandardCharsets.UTF_8);
 
             File f = zip.getFile();
             if (!f.getParentFile().exists()) {
@@ -42,22 +42,7 @@
                 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());
-            }
+            ZipParameters params = getZipParams(!StringHelper.isEmpty(pwd));
 
             // 瑕佹墦鍖呯殑鏂囦欢鎴栨枃浠跺す
             File currentFile = new File(sourcePath);
@@ -69,11 +54,66 @@
 
             return true;
         } catch (Exception ex) {
-            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
+            log.error(ex.getMessage(), ex);
             return false;
         }
     }
 
+    /**
+     * 鍒涘缓ZipFile
+     */
+    public static ZipFile createZipFile(String zipFile, String pwd) {
+        try {
+            ZipFile zip = StringHelper.isEmpty(pwd) ? new ZipFile(zipFile) : new ZipFile(zipFile, pwd.toCharArray());
+            // zip.setCharset(Charset.forName("GBK"))
+            zip.setCharset(StandardCharsets.UTF_8);
+
+            File f = zip.getFile();
+            if (!f.getParentFile().exists()) {
+                f.getParentFile().mkdirs();
+            }
+            if (f.exists()) {
+                f.delete();
+            }
+
+            return zip;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return null;
+        }
+    }
+
+    /**
+     * 鑾峰彇ZipParameters
+     */
+    public static ZipParameters getZipParams(boolean hasPwd) {
+        // 璁剧疆鍘嬬缉鏂囦欢鍙傛暟
+        ZipParameters params = new ZipParameters();
+        // 鍘嬬缉鏂瑰紡
+        params.setCompressionMethod(CompressionMethod.DEFLATE);
+        // 鍘嬬缉绾у埆
+        params.setCompressionLevel(CompressionLevel.MAXIMUM);
+
+        if (hasPwd) {
+            // 鏄惁璁剧疆鍔犲瘑鏂囦欢
+            params.setEncryptFiles(true);
+            // 璁剧疆AES鍔犲瘑寮哄害锛欿EY_STRENGTH_256
+            params.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128);
+            // 璁剧疆鍔犲瘑绠楁硶
+            params.setEncryptionMethod(EncryptionMethod.AES);
+        }
+
+        return params;
+    }
+
+    /**
+     * 娣诲姞鏂囦欢鑷冲帇缂╁寘
+     *
+     * @param zip    ZipFile
+     * @param params ZipParameters
+     * @param file   File
+     * @throws ZipException
+     */
     private static void addZipFile(ZipFile zip, ZipParameters params, File file) throws ZipException {
         if (file.isDirectory()) {
             File[] files = file.listFiles();
@@ -99,7 +139,7 @@
             ZipFile zip = new ZipFile(zipFile);
 
             // 璁剧疆瀵嗙爜
-            if (!Strings.isNullOrEmpty(password)) {
+            if (!StringHelper.isEmpty(password)) {
                 zip.setPassword(password.toCharArray());
             }
 
@@ -108,7 +148,7 @@
 
             return true;
         } catch (Exception ex) {
-            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
+            log.error(ex.getMessage(), ex);
             return false;
         }
     }

--
Gitblit v1.9.3