欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 17:56:46
本文实例讲述了java实现的zip压缩及解压缩工具类。分享给大家供大家参考,具体如下: import java.io.bufferedinputstream;...

本文实例讲述了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算法相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作dom节点技巧总结》和《java缓存操作技巧汇总

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

上一篇:

下一篇: