From 1d53dd8f501a98ddcce8146443b51b357ef5f9b1 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 29 十二月 2022 16:55:46 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/helper/ZipHelper.java |   34 ++++++++++++++++++++++++++--------
 1 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/ZipHelper.java b/src/main/java/com/lf/server/helper/ZipHelper.java
index 8416af1..9522053 100644
--- a/src/main/java/com/lf/server/helper/ZipHelper.java
+++ b/src/main/java/com/lf/server/helper/ZipHelper.java
@@ -28,11 +28,16 @@
      * @return 鎴愬姛鏄�/鍚�
      */
     public static boolean unzip(String filePath, String zipDir) {
+        ZipFile zipFile = null;
         try {
-            int count;
-            ZipFile zipfile = new ZipFile(filePath);
+            File dir = new File(zipDir);
+            if (!dir.exists() || !dir.isDirectory()) {
+                dir.mkdirs();
+            }
 
-            Enumeration e = zipfile.entries();
+            int count;
+            zipFile = new ZipFile(filePath);
+            Enumeration e = zipFile.entries();
             while (e.hasMoreElements()) {
                 ZipEntry entry = (ZipEntry) e.nextElement();
                 if (entry.isDirectory()) {
@@ -43,15 +48,16 @@
                     continue;
                 }
 
-                BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry));
+                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();
             }
 
@@ -59,6 +65,14 @@
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
             return false;
+        } finally {
+            try {
+                if (null != zipFile) {
+                    zipFile.close();
+                }
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+            }
         }
     }
 
@@ -70,10 +84,11 @@
      * @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());
@@ -84,9 +99,12 @@
             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);
             }

--
Gitblit v1.9.3