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

 更新时间:2014年02月10日 09:12:11   作者:  
本文主要介绍了使用apache ant进行zip解压缩操作的方法,可以解决中文编码和首层父类无法创建问题,需要的朋友可以参考下

需要导入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/");
 }
}

相关文章

  • java 工作流引擎设计实现解析流程定义文件

    java 工作流引擎设计实现解析流程定义文件

    这篇文章主要为大家介绍了java 工作流引擎设计与实现及流程定义文件解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • Maven jar包下载失败问题的处理方法

    Maven jar包下载失败问题的处理方法

    很多同学在Maven里下载一些依赖的时候,即下载jar包的时候总是会出现一些问题,本文将就这个问题给大家详细的讲解一下,需要的朋友可以参考下
    2023-06-06
  • mybatis返回key value map集合方式

    mybatis返回key value map集合方式

    这篇文章主要介绍了mybatis返回key value map集合方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 关于Spring中的@Configuration中的proxyBeanMethods属性

    关于Spring中的@Configuration中的proxyBeanMethods属性

    这篇文章主要介绍了关于Spring中的@Configuration中的proxyBeanMethods属性,需要的朋友可以参考下
    2023-07-07
  • Spring注解开发生命周期原理解析

    Spring注解开发生命周期原理解析

    这篇文章主要介绍了Spring注解开发生命周期原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • 教你如何使用idea管理docker

    教你如何使用idea管理docker

    其实idea也提供了docker的管理功能,比如查看容器列表,启动容器,停止容器等,本文来看下如何管理本地的docker daemon和远程的dockers daemon,感兴趣的朋友跟随小编一起看看吧
    2024-05-05
  • Java通过反射将 Excel 解析成对象集合实例

    Java通过反射将 Excel 解析成对象集合实例

    这篇文章主要介绍了Java通过反射将 Excel 解析成对象集合实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • 基于Java设计一个短链接生成系统

    基于Java设计一个短链接生成系统

    相信大家在生活中会收到很多短信,而这些短信都有一个特点是链接很短。这些链接背后的原理是什么呢?怎么实现的?小编今天就带你们详细了解一下
    2021-12-12
  • Springboot系列之kafka操作使用详解

    Springboot系列之kafka操作使用详解

    这篇文章主要为大家介绍了Springboot系列之kafka操作使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • java实现监控rtsp流转flv方法实例(前端播放,前后端代码都有)

    java实现监控rtsp流转flv方法实例(前端播放,前后端代码都有)

    这篇文章主要给大家介绍了关于java实现监控rtsp流转flv的相关资料,文中介绍的是前端播放,前后端代码都有,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06

最新评论