栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8 Zip 压缩与解压缩的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8 Zip 压缩与解压缩的实现

网上找过几个例子都有点小问题,还是谷歌找出来的靠谱。主要是增加了指定文件的功能,通过 Java8 的 Lambda 判断是否加入 ZIP 压缩,比较方便。函数表达式的签名是 Function,参数是待加入的 File 对象,返回值 true 表示允许,反之不行。

完整代码在:https://gitee.com/sp42_admin/ajaxjs/blob/master/ajaxjs-base/src/main/java/com/ajaxjs/util/io/FileHelper.java


package com.ajaxjs.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.function.Function;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import com.ajaxjs.util.logger.LogHelper;


public class ZipHelper {
 private static final LogHelper LOGGER = LogHelper.getLog(ZipHelper.class);

 
 public static void unzip(String save, String zipFile) {
 if (!new File(save).isDirectory())
  throw new IllegalArgumentException("保存的路径必须为目录路径");

 long start = System.currentTimeMillis();
 File folder = new File(save);
 if (!folder.exists())
  folder.mkdirs();

 try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));) {
  ZipEntry ze;
  while ((ze = zis.getNextEntry()) != null) {
  File newFile = new File(save + File.separator + ze.getName());
  System.out.println("file unzip : " + newFile.getAbsoluteFile());

  // 大部分网络上的源码,这里没有判断子目录
  if (ze.isDirectory()) {
   newFile.mkdirs();
  } else {
//   new File(newFile.getParent()).mkdirs();
   FileHelper.initFolder(newFile);
   FileOutputStream fos = new FileOutputStream(newFile);
   IoHelper.write(zis, fos, false);
   fos.close();
  }

//  ze = zis.getNextEntry();
  }
  zis.closeEntry();
 } catch (IOException e) {
  LOGGER.warning(e);
 }

 LOGGER.info("解压缩完成,耗时:{0}ms,保存在{1}", System.currentTimeMillis() - start, save);
 }

 
 public static void zip(String toZip, String saveZip) {
 zip(toZip, saveZip, null);
 }

 
 public static void zip(String toZip, String saveZip, Function everyFile) {
 long start = System.currentTimeMillis();
 File fileToZip = new File(toZip);

 FileHelper.initFolder(saveZip);

 try (FileOutputStream fos = new FileOutputStream(saveZip); ZipOutputStream zipOut = new ZipOutputStream(fos);) {
  zip(fileToZip, fileToZip.getName(), zipOut, everyFile);
 } catch (IOException e) {
  LOGGER.warning(e);
 }

 LOGGER.info("压缩完成,耗时:{0}ms,保存在{1}", System.currentTimeMillis() - start, saveZip);
 }

 
 private static void zip(File toZip, String fileName, ZipOutputStream zipOut, Function everyFile) {
 if (toZip.isHidden())
  return;

 if (everyFile != null && !everyFile.apply(toZip)) {
  return; // 跳过不要的
 }

 try {
  if (toZip.isDirectory()) {
  zipOut.putNextEntry(new ZipEntry(fileName.endsWith("/") ? fileName : fileName + "/"));
  zipOut.closeEntry();

  File[] children = toZip.listFiles();
  for (File childFile : children) {
   zip(childFile, fileName + "/" + childFile.getName(), zipOut, everyFile);
  }

  return;
  }

  zipOut.putNextEntry(new ZipEntry(fileName));

  try (FileInputStream in = new FileInputStream(toZip);) {
  IoHelper.write(in, zipOut, false);
  }
 } catch (IOException e) {
  LOGGER.warning(e);
 }
 }
}

到此这篇关于Java8 Zip 压缩与解压缩的实现的文章就介绍到这了,更多相关Java8 Zip 压缩与解压缩内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134698.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号