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

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

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

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

本文实例讲述了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;
  
  public void zip(String zipFileName, String filePath, boolean isDelete) throws Exception {
    zip(zipFileName, new File(filePath), isDelete);
  }
  
  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();
  }
  
  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();
    }
  }
  
  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 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算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

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

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

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