From cb9e5e684780d8e0abc5820ac285aa95baca1cc0 Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期二, 31 三月 2020 11:09:57 +0800
Subject: [PATCH] zip压缩

---
 app/src/main/java/safeluck/drive/evaluation/util/Utils.java |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/safeluck/drive/evaluation/util/Utils.java b/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
index 84f91c8..014230f 100644
--- a/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
+++ b/app/src/main/java/safeluck/drive/evaluation/util/Utils.java
@@ -10,6 +10,9 @@
 import com.anyun.exam.lib.util.ByteUtil;
 import com.safeluck.aykj.utils.BytesUtils;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.math.BigDecimal;
 import java.security.SecureRandom;
 import java.text.ParseException;
@@ -23,6 +26,9 @@
 import java.util.Random;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
 
 import javax.crypto.Cipher;
 import javax.crypto.SecretKey;
@@ -481,4 +487,58 @@
         long utcTime = date.getTime()+8*60*60*1000;
         return utcTime;
     }
+
+    public static void zipFolder(String srcFileString,String zipFileString) throws Exception{
+        //鍒涘缓zip
+        ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
+
+        //鍒涘缓鏂囦欢
+        File file = new File(srcFileString);
+        //鍘嬬缉
+        System.out.println("----->"+file.getParent()+"==="+file.getAbsolutePath());
+        zipFiles(file.getParent()+File.separator,file.getName(),outZip);
+        outZip.finish();
+        outZip.close();
+    }
+
+    /**
+     * 鍘嬬缉鏂囦欢
+     * @param folderString
+     * @param fileString
+     * @param outZip
+     */
+    private static void zipFiles(String folderString, String fileString, ZipOutputStream outZip)
+    throws Exception{
+        System.out.println("folderString="+folderString+ "\n" +
+                "fileString:" + fileString + "\n==========================");
+        if (outZip == null){
+            return;
+        }
+        File file = new File(folderString + fileString);
+        if (file.isFile()){
+            ZipEntry zipEntry = new ZipEntry(fileString);
+            FileInputStream fileInputStream = new FileInputStream(file);
+            outZip.putNextEntry(zipEntry);
+            int len = 0;
+            byte[] buffer = new byte[1024];
+            while ((len=fileInputStream.read(buffer))!=-1){
+                outZip.write(buffer,0,len);
+            }
+            outZip.closeEntry();
+        }else{
+            //鏂囦欢澶�
+            String []fileList = file.list();
+            //娌℃湁瀛愭枃浠剁殑鍘嬬缉
+            if (fileList.length<=0){
+                ZipEntry zipEntry = new ZipEntry(fileString+File.separator);
+                outZip.putNextEntry(zipEntry);
+                outZip.closeEntry();
+            }else{
+                for (int i = 0; i < fileList.length; i++) {
+                    zipFiles(folderString+fileString+"/",fileList[i],outZip);
+                }
+            }
+        }
+
+    }
 }

--
Gitblit v1.8.0