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/");
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java注解方式之防止重复请求
![](https://img.aspxhome.com/file/2023/1/82981_0s.jpg)
Java C++题解leetcode 1684统计一致字符串的数目示例
![](https://img.aspxhome.com/file/2023/2/71252_0s.png)
为什么不建议使用Java自定义Object作为HashMap的key
![](https://img.aspxhome.com/file/2023/6/72616_0s.webp)
Java实现简单LRU缓存机制的方法
![](https://img.aspxhome.com/file/2023/7/83057_0s.png)
Spring中@Scheduled和HttpClient的连环坑
SpringBoot配置Profile实现多环境支持
![](https://img.aspxhome.com/file/2023/9/85419_0s.png)
利用POI生成EXCEL文件的方法实例
![](https://img.aspxhome.com/file/2023/3/59723_0s.png)
Java web的读取Excel简单实例代码
![](https://img.aspxhome.com/file/2023/6/78946_0s.png)
Spring Cloud Gateway(读取、修改 Request Body)的操作
![](https://img.aspxhome.com/file/2023/0/59240_0s.jpg)
C#利用com操作excel释放进程的解决方法
Java数据结构与算法入门实例详解
![](https://img.aspxhome.com/file/2023/0/60280_0s.png)
Java集合遍历实现方法及泛型通配
![](https://img.aspxhome.com/file/2023/9/71189_0s.png)
Java中多媒体文件上传及页面回显的操作代码
![](https://img.aspxhome.com/file/2023/2/66202_0s.png)
一文彻底搞懂Java和JDK的版本命名问题
![](https://img.aspxhome.com/file/2023/6/59576_0s.jpg)
44条Java代码优化建议
![](https://img.aspxhome.com/file/2023/6/62776_0s.png)
Java数据结构之ArrayList从顺序表到实现
![](https://img.aspxhome.com/file/2023/1/68771_0s.png)
java开发BeanUtils类解决实体对象间赋值
![](https://img.aspxhome.com/file/2023/0/68150_0s.png)
Spring boot Mybatis 整合(完整版)
![](https://img.aspxhome.com/file/2023/3/87143_0s.png)
Java Selenium实现多窗口切换的示例代码
Java 在PDF中添加骑缝章示例解析
![](https://img.aspxhome.com/file/2023/5/59985_0s.png)