From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001 From: sws <15810472099@163.com> Date: 星期六, 26 十一月 2022 16:12:02 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/ZipHelper.java | 145 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 145 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/lf/server/helper/ZipHelper.java b/src/main/java/com/lf/server/helper/ZipHelper.java new file mode 100644 index 0000000..3f1f8fa --- /dev/null +++ b/src/main/java/com/lf/server/helper/ZipHelper.java @@ -0,0 +1,145 @@ +package com.lf.server.helper; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.io.*; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; + +/** + * Zip甯姪绫� + * @author WWW + */ +public class ZipHelper { + private final static int BUFFER_SIZE = 1024; + + private final static byte[] BUFFER = new byte[1024]; + + private final static Log log = LogFactory.getLog(ZipHelper.class); + + /** + * Zip瑙e帇 + * + * @param filePath zip鏂囦欢 + * @param zipDir 瑙e帇璺緞 + * @return 鎴愬姛鏄�/鍚� + */ + public static boolean unzip(String filePath, String zipDir) { + ZipFile zipFile = null; + try { + int count; + 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(); + fos.close(); + is.close(); + } + + return true; + } 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); + } + } + } + + /** + * Zip鍘嬬缉 + * + * @param zipFile zip婧愭枃浠� + * @param sourceDir 婧愭枃浠跺す + * @return 鎴愬姛鏄�/鍚� + */ + public static boolean zip(String zipFile, String sourceDir) { + FileOutputStream fos = null; + ZipOutputStream zos = null; + try { + 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); + return false; + } finally { + try { + if (null != zos) { + zos.close(); + } + if (null != fos) { + fos.close(); + } + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + } + + /** + * Zip鍘嬬缉 + * + * @param sourceFile zip婧愭枃浠� + * @param zos zip杈撳嚭娴� + * @param name zip鏂囦欢鍚嶇О + * @throws Exception + */ + public static void compress(File sourceFile, ZipOutputStream zos, String name) throws Exception { + if (sourceFile.isFile()) { + // 鍚憐ip杈撳嚭娴佷腑娣诲姞涓�涓獄ip瀹炰綋锛屾瀯閫犲櫒涓璶ame涓簔ip瀹炰綋鐨勬枃浠剁殑鍚嶅瓧 + zos.putNextEntry(new ZipEntry(name)); + + // copy鏂囦欢鍒皕ip杈撳嚭娴佷腑 + int len; + FileInputStream in = new FileInputStream(sourceFile); + while ((len = in.read(BUFFER)) != -1) { + zos.write(BUFFER, 0, len); + } + zos.closeEntry(); + in.close(); + } else { + File[] listFiles = sourceFile.listFiles(); + if (listFiles == null || listFiles.length == 0) { + // 绌烘枃浠跺す鐨勫鐞嗭細娌℃湁鏂囦欢锛屼笉闇�瑕佹枃浠剁殑copy + zos.putNextEntry(new ZipEntry(name + "/")); + zos.closeEntry(); + } else { + for (File file : listFiles) { + compress(file, zos, name + "/" + file.getName()); + } + } + } + } +} -- Gitblit v1.9.3