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

apache ant进行zip解压缩操作示例分享

程序员文章站 2024-02-23 22:52:34
需要导入ant.jar包,apache网站(http://ant.apache.org/bindownload.cgi)下载即可。 复制代码 代码如下:import ja...

需要导入ant.jar包,apache网站(http://ant.apache.org/bindownload.cgi)下载即可。

复制代码 代码如下:

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.util.zip.zipoutputstream;

import org.apache.tools.ant.project;
import org.apache.tools.ant.taskdefs.expand;
import org.apache.tools.zip.zipentry;

import com.xyq.io.util.closeioutil;

public class ziputil {

 private static final string encode = "utf-8";

 public static void zip(string inputfilepath, string zipfilename) {

  file inputfile = new file(inputfilepath);
  if (!inputfile.exists())
   throw new runtimeexception("原始文件不存在!!!");
  file basetarzipfile = new file(zipfilename).getparentfile();
  if (!basetarzipfile.exists() && !basetarzipfile.mkdirs())
   throw new runtimeexception("目标文件无法创建!!!");
  bufferedoutputstream bos = null;
  fileoutputstream out = null;
  zipoutputstream zout = null;
  try {
   // 创建文件输出对象out,提示:注意中文支持
   out = new fileoutputstream(new string(zipfilename.getbytes(encode)));
   bos = new bufferedoutputstream(out);
   // 將文件輸出zip输出流接起来
   zout = new zipoutputstream(bos);
   zip(zout, inputfile, inputfile.getname());
   closeioutil.closeall(zout, bos, out);

  } catch (exception e) {
   e.printstacktrace();
  }
 }

 private static void zip(zipoutputstream zout, file file, string base) {

  try {
   // 如果文件句柄是目录
   if (file.isdirectory()) {
    // 获取目录下的文件
    file[] listfiles = file.listfiles();
    // 建立zip条目
    zout.putnextentry(new zipentry(base + "/"));
    base = (base.length() == 0 ? "" : base + "/");
    if (listfiles != null && listfiles.length > 0)
     // 遍历目录下文件
     for (file f : listfiles)
      // 递归进入本方法
      zip(zout, f, base + f.getname());
   }
   // 如果文件句柄是文件
   else {
    if (base == "") {
     base = file.getname();
    }
    // 填入文件句柄
    zout.putnextentry(new zipentry(base));
    // 开始压缩
    // 从文件入流读,写入zip 出流
    writefile(zout, file);
   }

  } catch (exception e) {
   e.printstacktrace();
  }
 }

 private static void writefile(zipoutputstream zout, file file)
   throws ioexception {

  fileinputstream in = null;
  bufferedinputstream bis = null;
  in = new fileinputstream(file);
  bis = new bufferedinputstream(in);
  int len = 0;
  byte[] buff = new byte[2048];
  while ((len = bis.read(buff)) != -1)
   zout.write(buff, 0, len);
  zout.flush();
  closeioutil.closeall(bis, in);
 }

 /****
  * 解压
  *
  * @param zippath
  *            zip文件路径
  * @param destinationpath
  *            解压的目的地点
  * @param ecode
  *            文件名的编码字符集
  */
 public static void unzip(string zippath, string destinationpath) {

  file zipfile = new file(zippath);
  if (!zipfile.exists())
   throw new runtimeexception("zip file " + zippath
     + " does not exist.");

  project proj = new project();
  expand expand = new expand();
  expand.setproject(proj);
  expand.settasktype("unzip");
  expand.settaskname("unzip");
  expand.setsrc(zipfile);
  expand.setdest(new file(destinationpath));
  expand.setencoding(encode);
  expand.execute();
  system.out.println("unzip done!!!");
 }

 public static void main(string[] args) {

  string dir = new string("f:\\我的备份\\文档\\myeclipse+9.0正式版破解与激活(亲测可用)");
  dir = new string("f:/111.jpg");
  zip(dir, "f:/bzbxb/zipant.zip");
  unzip("f:/bzbxb/zipant.zip", "f:/xx/xx/");
 }
}