From 7e25c513e41cad0c5ced0cdd34c7eb9a6814f968 Mon Sep 17 00:00:00 2001 From: wangbing Date: Thu, 19 Mar 2020 16:08:16 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81Zip=E8=A7=A3=E5=8E=8B=E6=89=93?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: d11c0274d05b3fba3448b7f9262180a04fc72a90 --- .../SpringBoot/java/frame/utils/ZipUtil.java | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/main/resources/modules/SpringBoot/java/frame/utils/ZipUtil.java diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/ZipUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/ZipUtil.java new file mode 100644 index 00000000..fcdb8dca --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/ZipUtil.java @@ -0,0 +1,145 @@ +package ${basePackage}.frame.utils; + +import java.io.*; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; + +/** + * ZipUtil - Zip解压打包工具 + * + * @author wangbing + * @version 0.0.1 + * @since 2017-01-01 + */ +public class ZipUtil { + + /** + * 解压文件 + * + * @param zipFile 指定解压Zip文件 + * @param descDir 指定解压目录 + */ + public static void unZip(File zipFile, File descDir) { + if (!descDir.exists()) { + descDir.mkdirs(); + } + ZipFile zip = null; + try { + zip = new ZipFile(zipFile); + Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = (ZipEntry) entries.nextElement(); + // 如果是文件夹,就创建个文件夹 + if (entry.isDirectory()) { + File dir = new File(descDir, entry.getName()); + dir.mkdirs(); + } else { + // 如果是文件,就先创建一个文件,然后用io流把内容copy过去 + File targetFile = new File(descDir + "/" + entry.getName()); + // 保证这个文件的父文件夹必须要存在 + if (!targetFile.getParentFile().exists()) { + targetFile.getParentFile().mkdirs(); + } + targetFile.createNewFile(); + // 将压缩文件内容写入到这个文件中 + InputStream is = zip.getInputStream(entry); + FileOutputStream fos = new FileOutputStream(targetFile); + int len; + byte[] buf = new byte[1024]; + while ((len = is.read(buf)) != -1) { + fos.write(buf, 0, len); + } + // 关流顺序,先打开的后关闭 + fos.close(); + is.close(); + } + } + System.out.println("解压文件" + zipFile.getAbsolutePath() + "成功"); + } catch (Exception e) { + throw new RuntimeException("unzip error from ZipUtil", e); + } finally { + if (zip != null) { + try { + zip.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * 压缩文件或文件夹 + * + * @param src 指定待压缩目录或文件 + * @param zip 指定压缩Zip文件 + */ + public static void toZip(File src, File zip) { + ZipOutputStream zipOutputStream = null; + try { + if (!src.exists()) { + System.err.println("压缩文件或目录不存在"); + return; + } + + if (!zip.exists()) { + zip.createNewFile(); + } + + zipOutputStream = new ZipOutputStream(new FileOutputStream(zip)); + compress(src, zipOutputStream, null); + zipOutputStream.close(); + System.out.println("压缩文件" + zip.getAbsolutePath() + "成功"); + } catch (IOException e) { + System.err.println("压缩失败"); + e.printStackTrace(); + } finally { + if (zipOutputStream != null) { + try { + zipOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * @param sourceFile 待压缩文件或文件夹 + * @param zos Zip输出流 + * @param parentPath 父目录结构 + * @throws IOException + */ + private static void compress(File sourceFile, ZipOutputStream zos, String parentPath) throws IOException { + byte[] buff = new byte[1024]; + + if (sourceFile.isDirectory()) { + //确保空文件夹也可以压缩进去 + zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName() + "/")); + zos.closeEntry(); + //循环压缩子文件或文件夹 + for (File file : sourceFile.listFiles()) { + compress(file, zos, (parentPath != null ? parentPath : "") + sourceFile.getName() + "/"); + } + } else { + //压缩文件 + zos.putNextEntry(new ZipEntry((parentPath != null ? parentPath : "") + sourceFile.getName())); + int len; + FileInputStream in = new FileInputStream(sourceFile); + while ((len = in.read(buff)) != -1) { + zos.write(buff, 0, len); + } + zos.closeEntry(); + in.close(); + } + } + + public static void main(String[] args) { + //解压 + unZip(new File("E:\\AAA.zip"), new File("E:\\AAA")); + //压缩 + toZip(new File("E:\\AAA"), new File("E:\\AAA.zip")); + } +}