温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java实现的zip压缩及解压缩工具类示例

发布时间:2021-04-17 14:27:02 来源:亿速云 阅读:242 作者:小新 栏目:编程语言

小编给大家分享一下Java实现的zip压缩及解压缩工具类示例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

具体如下:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
  private static final int BUFFEREDSIZE = 1024;
  /**
   * 压缩文件
   *
   * @param zipFileName
   *      保存的压缩包文件路径
   * @param filePath
   *      需要压缩的文件夹或者文件路径
   * @param isDelete
   *      是否删除源文件
   * @throws Exception
   */
  public void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {
    zip(zipFileName, new File(filePath), isDelete);
  }
  /**
   * 压缩文件
   *
   * @param zipFileName
   *      保存的压缩包文件路径
   * @param inputFile
   *      需要压缩的文件夹或者文件
   * @param isDelete
   *      是否删除源文件
   * @throws Exception
   */
  public void zip(String zipFileName, File inputFile, boolean isDelete) throws Exception {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
    if (!inputFile.exists()) {
      throw new FileNotFoundException("在指定路径未找到需要压缩的文件!");
    }
    zip(out, inputFile, "", isDelete);
    out.close();
  }
  /**
   * 递归压缩方法
   *
   * @param out
   *      压缩包输出流
   * @param f
   *      需要压缩的文件
   * @param base
   *      压缩的路径
   * @param isDelete
   *      是否删除源文件
   * @throws Exception
   */
  private void zip(ZipOutputStream out, File inputFile, String base, boolean isDelete) throws Exception {
    if (inputFile.isDirectory()) { // 如果是目录
      File[] inputFiles = inputFile.listFiles();
      out.putNextEntry(new ZipEntry(base + "/"));
      base = base.length() == 0 ? "" : base + "/";
      for (int i = 0; i < inputFiles.length; i++) {
        zip(out, inputFiles[i], base + inputFiles[i].getName(), isDelete);
      }
    } else { // 如果是文件
      if (base.length() > 0) {
        out.putNextEntry(new ZipEntry(base));
      } else {
        out.putNextEntry(new ZipEntry(inputFile.getName()));
      }
      FileInputStream in = new FileInputStream(inputFile);
      try {
        int len;
        byte[] buff = new byte[BUFFEREDSIZE];
        while ((len = in.read(buff)) != -1) {
          out.write(buff, 0, len);
        }
      } catch (IOException e) {
        throw e;
      } finally {
        in.close();
      }
    }
    if (isDelete) {
      inputFile.delete();
    }
  }
  /**
   * 解压缩
   *
   * @param zipFilePath
   *      压缩包路径
   * @param fileSavePath
   *      解压路径
   * @param isDelete
   *      是否删除源文件
   * @throws Exception
   */
  public void unZip(String zipFilePath, String fileSavePath, boolean isDelete) throws Exception {
    try {
      (new File(fileSavePath)).mkdirs();
      File f = new File(zipFilePath);
      if ((!f.exists()) && (f.length() <= 0)) {
        throw new Exception("要解压的文件不存在!");
      }
      ZipFile zipFile = new ZipFile(f);
      String strPath, gbkPath, strtemp;
      File tempFile = new File(fileSavePath);// 从当前目录开始
      strPath = tempFile.getAbsolutePath();// 输出的绝对位置
      Enumeration<ZipEntry> e = zipFile.getEntries();
      while (e.hasMoreElements()) {
        org.apache.tools.zip.ZipEntry zipEnt = e.nextElement();
        gbkPath = zipEnt.getName();
        if (zipEnt.isDirectory()) {
          strtemp = strPath + File.separator + gbkPath;
          File dir = new File(strtemp);
          dir.mkdirs();
          continue;
        } else {
          // 读写文件
          InputStream is = zipFile.getInputStream(zipEnt);
          BufferedInputStream bis = new BufferedInputStream(is);
          gbkPath = zipEnt.getName();
          strtemp = strPath + File.separator + gbkPath;
          // 建目录
          String strsubdir = gbkPath;
          for (int i = 0; i < strsubdir.length(); i++) {
            if (strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) {
              String temp = strPath + File.separator + strsubdir.substring(0, i);
              File subdir = new File(temp);
              if (!subdir.exists())
                subdir.mkdir();
            }
          }
          FileOutputStream fos = new FileOutputStream(strtemp);
          BufferedOutputStream bos = new BufferedOutputStream(fos);
          int len;
          byte[] buff = new byte[BUFFEREDSIZE];
          while ((len = bis.read(buff)) != -1) {
            bos.write(buff, 0, len);
          }
          bos.close();
          fos.close();
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
    if (isDelete) {
      new File(zipFilePath).delete();
    }
  }
// public static void main(String[] args) {
//   ZipUtil cpr = new ZipUtil();
//   try {
//     cpr.zip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/新建文件夹", false);
//     cpr.unZip("C:/Users/Lenovo User/Desktop/test中文.zip", "C:/Users/Lenovo User/Desktop/新建文件夹2", false);
//   } catch (Exception e) {
//     e.printStackTrace();
//   }
//
// }
}

以上是“Java实现的zip压缩及解压缩工具类示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI