From 104c35a0cf0dfec4461a3496a371a572e6b48555 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 18 Feb 2017 11:17:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0GZIP=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E5=92=8CZIP=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/kit/ZipKit.java | 140 +++++++++++++++++- 1 file changed, 138 insertions(+), 2 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/kit/ZipKit.java b/src/main/java/pw/yumc/YumCore/kit/ZipKit.java index da9719e..b8b4a70 100644 --- a/src/main/java/pw/yumc/YumCore/kit/ZipKit.java +++ b/src/main/java/pw/yumc/YumCore/kit/ZipKit.java @@ -1,13 +1,21 @@ package pw.yumc.YumCore.kit; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Enumeration; +import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; /** * ZIP操作类 @@ -29,7 +37,7 @@ public class ZipKit { /** * 解压ZIP文件 - * + * * @param zipFile * zip文件 * @param destPath @@ -45,7 +53,7 @@ public class ZipKit { /** * 解压ZIP文件 - * + * * @param zipFile * zip文件 * @param destPath @@ -71,4 +79,132 @@ public class ZipKit { } zipObj.close(); } + + /** + * ZIP压缩 + * + * @param inputFile + * 输入文件 + * @param zipFileName + * 输出文件名称 + * @throws IOException + * IO异常 + */ + public static void zip(File inputFile, String zipFileName) throws IOException { + ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName)); + BufferedOutputStream bos = new BufferedOutputStream(zos); + zip(zos, inputFile, inputFile.getName(), bos); + bos.close(); + zos.close(); // 输出流关闭 + } + + /** + * ZIP压缩 + * + * @param zos + * Zip输出流 + * @param file + * 添加的文件 + * @param base + * 基础目录 + * @param bos + * 输出流 + * @throws IOException + * IO异常 + */ + private static void zip(ZipOutputStream zos, File file, String base, BufferedOutputStream bos) throws IOException { // 方法重载 + if (file.isDirectory()) { + File[] fl = file.listFiles(); + if (fl == null || fl.length == 0) { + zos.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base + } else { + for (File fl1 : fl) { + zip(zos, fl1, base + "/" + fl1.getName(), bos); // 递归遍历子文件夹 + } + } + } else { + zos.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base + FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + int b; + while ((b = bis.read()) != -1) { + bos.write(b); // 将字节流写入当前zip目录 + } + bos.flush(); + bis.close(); + fis.close(); // 输入流关闭 + } + } + + /** + * GZIP压缩文件 + * + * @param file + * 压缩文件 + * @throws IOException + * IO异常 + */ + public static void gzip(File file) throws IOException { + gzip(file, file.getName() + ".gz", true); + } + + /** + * GZIP压缩文件 + * + * @param file + * 压缩文件 + * @param out + * 输出名称 + * @throws IOException + * IO异常 + */ + public static void gzip(File file, String out) throws IOException { + gzip(file, out, true); + } + + /** + * GZIP压缩文件 + * + * @param file + * 压缩文件 + * @param out + * 输出名称 + * @param delete + * 是否删除 + * @throws IOException + * IO异常 + */ + public static void gzip(File file, String out, boolean delete) throws IOException { + FileInputStream fis = new FileInputStream(file); + FileOutputStream fos = new FileOutputStream(out); + gzip(fis, fos); + fis.close(); + fos.flush(); + fos.close(); + if (delete) { + file.delete(); + } + } + + /** + * GZIP压缩数据流 + * + * @param is + * 输入流 + * @param os + * 输出流 + * @throws IOException + * IO异常 + */ + public static void gzip(InputStream is, OutputStream os) throws IOException { + GZIPOutputStream gos = new GZIPOutputStream(os); + int count; + byte data[] = new byte[1024]; + while ((count = is.read(data, 0, 1024)) != -1) { + gos.write(data, 0, count); + } + gos.finish(); + gos.flush(); + gos.close(); + } }