From 024e90554d19c2342f27a26f91bbea378f84da82 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 13 十一月 2024 17:25:18 +0800 Subject: [PATCH] 1 --- src/main/java/com/moon/server/helper/ZipHelper.java | 95 +++++++++++++++++++++-------------------------- 1 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/main/java/com/moon/server/helper/ZipHelper.java b/src/main/java/com/moon/server/helper/ZipHelper.java index 9a56595..8e09629 100644 --- a/src/main/java/com/moon/server/helper/ZipHelper.java +++ b/src/main/java/com/moon/server/helper/ZipHelper.java @@ -10,10 +10,7 @@ import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; -/** - * Zip甯姪绫� - * @author WWW - */ +@SuppressWarnings("ALL") public class ZipHelper { private final static int BUFFER_SIZE = 1024; @@ -21,13 +18,6 @@ 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 { @@ -36,31 +26,9 @@ dir.mkdirs(); } - int count; zipFile = new ZipFile(filePath, Charset.forName("GBK")); - 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(); - } + createDirs(zipFile, zipDir); + writeFiles(zipFile, zipDir); return true; } catch (Exception ex) { @@ -77,13 +45,45 @@ } } - /** - * Zip鍘嬬缉 - * - * @param zipFile zip婧愭枃浠� - * @param sourceDir 婧愭枃浠跺す - * @return 鎴愬姛鏄�/鍚� - */ + 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(); + } + public static boolean zip(String zipFile, String sourceDir) { FileOutputStream fos = null; ZipOutputStream zos = null; @@ -112,20 +112,10 @@ } } - /** - * 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) { @@ -136,7 +126,6 @@ } else { File[] listFiles = sourceFile.listFiles(); if (listFiles == null || listFiles.length == 0) { - // 绌烘枃浠跺す鐨勫鐞嗭細娌℃湁鏂囦欢锛屼笉闇�瑕佹枃浠剁殑copy zos.putNextEntry(new ZipEntry(name + "/")); zos.closeEntry(); } else { -- Gitblit v1.9.3