From ed8c7a5effd0d423ce1118b680ecdca6fe732609 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 02 七月 2025 16:43:13 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.11.205:9000/r/P2022036_Service

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

diff --git a/src/main/java/com/lf/server/helper/ZipHelper.java b/src/main/java/com/lf/server/helper/ZipHelper.java
index 0b21fe2..42ddd86 100644
--- a/src/main/java/com/lf/server/helper/ZipHelper.java
+++ b/src/main/java/com/lf/server/helper/ZipHelper.java
@@ -4,6 +4,7 @@
 import org.apache.commons.logging.LogFactory;
 
 import java.io.*;
+import java.nio.charset.Charset;
 import java.util.Enumeration;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
@@ -21,74 +22,118 @@
     private final static Log log = LogFactory.getLog(ZipHelper.class);
 
     /**
-     * 瑙e帇缂�
+     * Zip瑙e帇
      *
      * @param filePath zip鏂囦欢
      * @param zipDir   瑙e帇璺緞
      * @return 鎴愬姛鏄�/鍚�
      */
     public static boolean unzip(String filePath, String zipDir) {
+        ZipFile zipFile = null;
         try {
-            int count;
-            ZipFile zipfile = new ZipFile(filePath);
-
-            Enumeration e = zipfile.entries();
-            while (e.hasMoreElements()) {
-                ZipEntry entry = (ZipEntry) e.nextElement();
-                if (entry.isDirectory()) {
-                    File f = new File(zipDir + File.separator + entry.getName());
-                    if (!f.exists()) {
-                        f.mkdirs();
-                    }
-                    continue;
-                }
-
-                BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry));
-                FileOutputStream fos = new FileOutputStream(zipDir + File.separator + entry.getName());
-                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
-
-                while ((count = is.read(BUFFER, 0, BUFFER_SIZE)) != -1) {
-                    dest.write(BUFFER, 0, count);
-                }
-                dest.flush();
-                dest.close();
-                is.close();
+            File dir = new File(zipDir);
+            if (!dir.exists() || !dir.isDirectory()) {
+                dir.mkdirs();
             }
+
+            zipFile = new ZipFile(filePath, Charset.forName("GBK"));
+            createDirs(zipFile, zipDir);
+            writeFiles(zipFile, zipDir);
 
             return true;
         } catch (Exception ex) {
-            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
+            log.error(ex.getMessage(), ex);
             return false;
+        } finally {
+            try {
+                if (null != zipFile) {
+                    zipFile.close();
+                }
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+            }
         }
+    }
+
+    /**
+     * 鍒涘缓鐩綍
+     */
+    private static void createDirs(ZipFile zipFile, String zipDir) {
+        Enumeration<?> e = zipFile.entries();
+        while (e.hasMoreElements()) {
+            ZipEntry entry = (ZipEntry) e.nextElement();
+            if (entry.isDirectory()) {
+                File f = new File(zipDir + File.separator + entry.getName());
+                if (!f.exists()) {
+                    f.mkdirs();
+                }
+            }
+        }
+    }
+
+    /**
+     * 鍐欐枃浠�
+     */
+    private static void writeFiles(ZipFile zipFile, String zipDir) throws IOException {
+        Enumeration<?> e = zipFile.entries();
+        while (e.hasMoreElements()) {
+            ZipEntry entry = (ZipEntry) e.nextElement();
+            if (!entry.isDirectory()) {
+                writeFile(zipFile, entry, zipDir);
+            }
+        }
+    }
+
+    /**
+     * 鍐欐枃浠�
+     */
+    private static void writeFile(ZipFile zipFile, ZipEntry entry, String zipDir) throws IOException {
+        int count;
+
+        BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
+        FileOutputStream fos = new FileOutputStream(zipDir + File.separator + entry.getName());
+        BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);
+        while ((count = is.read(BUFFER, 0, BUFFER_SIZE)) != -1) {
+            dest.write(BUFFER, 0, count);
+        }
+
+        dest.flush();
+        dest.close();
+        fos.close();
+        is.close();
     }
 
     /**
      * Zip鍘嬬缉
      *
-     * @param zipFile   Zip婧愭枃浠�
+     * @param zipFile   zip婧愭枃浠�
      * @param sourceDir 婧愭枃浠跺す
      * @return 鎴愬姛鏄�/鍚�
      */
     public static boolean zip(String zipFile, String sourceDir) {
+        FileOutputStream fos = null;
         ZipOutputStream zos = null;
         try {
-            FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
-            zos = new ZipOutputStream(fileOutputStream);
+            fos = new FileOutputStream(zipFile);
+            zos = new ZipOutputStream(fos);
 
             File sourceFile = new File(sourceDir);
             compress(sourceFile, zos, sourceFile.getName());
 
             return true;
         } catch (Exception ex) {
-            log.error(ex.getMessage() + ex.getStackTrace() + "\n");
+            log.error(ex.getMessage(), ex);
             return false;
         } finally {
             try {
-                if (zos != null) {
+                if (null != zos) {
                     zos.close();
                 }
+                if (null != fos) {
+                    fos.close();
+                }
             } catch (Exception e) {
-                log.error(e.getMessage() + e.getStackTrace() + "\n");
+                log.error(e.getMessage(), e);
             }
         }
     }
@@ -96,9 +141,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