apache ant进行zip解压缩操作示例分享
发布时间:2021-11-08 09:16:03
需要导入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/");
}
}


猜你喜欢
- OAuth 2.0 是一种工业级的授权协议。OAuth 2.0是从创建于2006年的OAuth 1.0继承而来的。OAuth 2.0致力于帮
- 前言在上一篇,我们谈到了jvm垃圾回收算法详细解析,并了解了JVM中针对堆区中不同的分代采用不同的垃圾回收算法在了解了垃圾回收算法之后,很多
- 使用 fragmentLayout 实现,可以把小红点添加到任意 view 上。效果 添加小红点到 textview 上添加小红点到 ima
- 简介自从JDK中引入了stream之后,仿佛一切都变得很简单,根据stream提供的各种方法,如map,peek,flatmap等等,让我们
- 一、why(为什么要用Hibernate缓存?)Hibernate是一个持久层框架,经常访问物理数据库。为了降低应用程序对物理数据源访问的频
- 微信支付最近公司要在微信公众号上做一个活动预报名,活动的门票等需要在微信中支付。微信支付前的准备微信支付需要一个微信支付商务号(https:
- Android选择图片的两种方式:第一种:单张选取通过隐式启动activity,跳转到相册选择一张返回结果关键代码如下:发送请求:priva
- List查询JAVA中从数据库中取数据,根据MyBits返回结果主要有两种类型的List,一种是List<Entity>,还一种
- 本文实例讲述了C#简单创建和删除目录的方法。分享给大家供大家参考。具体如下:using System;using System.IO;cla
- 本文实例讲述了Java常用内置注解。分享给大家供大家参考,具体如下:一 通过@SuppressWarnings关闭警告信息1 代码publi
- Maven搭建springboot项目本文是基于Windows 10系统环境,使用Maven搭建springboot项目Windows 10
- 1.@RequestMapping注解1.1@RequestMapping注解的功能从注解名称上我们可以看到,@RequestMapping
- 本文实例讲述了C#实现图形位置组合转换的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.C
- 方法一:<uses-permission android:name="android.permission.WAKE_LOC
- 昨天直接在机器上配置了Maven环境,今天顺便把Eclipse等IDE环境配置好。安装IDE Plugins的方法有很多。其一:在线安装,通
- 删除本地仓库未下载完成的缓存文件(删除像图片显示这样以.lastUpdated结尾的文件)执行mvn -v确保maven命令可以正常执行执行
- 员工管理系统要求如下:通过面向对象的编程思想,实现员工信息的增删改查,存储结构为数组。限定数组长度为100。实现页面:添加员工查找员工修改员
- 软件工程由于需要不断迭代开发,因此要对源代码进行版本管理。Android源代码工程(AOSP)也不例外,它采用Git来进行版本管理。AOSP
- 2015年Google IO大会分布了DataBinding库,能够更快捷便利的实现MVVM结构模式。但是,通过对DataBinding的学
- 效果图片重写DataGridView的OnRowPostPaint方法或者直接在DataGridView的RowPostPaint事件里写,