package com.aaa.test;
import java.io.File;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
/**
* ZIP压缩/解压
*
* @author aaa
*/
public class Zipper {
public final static String encoding = "GBK";
// 压缩
public static void zip(String srcPathname, String zipFilepath)
throws BuildException, RuntimeException {
File file = new File(srcPathname);
if (!file.exists())
throw new RuntimeException("文件或文件夹" + srcPathname + "不存在...");
// 删除目标路径中已存在的同名文件(根据实际情况需要)
File zipFile = new File(zipFilepath);
if (zipFile.exists())
zipFile.delete();
Project proj = new Project();
FileSet fileSet = new FileSet();
fileSet.setProject(proj);
// 判断是目录还是文件
if (file.isDirectory()) {
fileSet.setDir(file);
} else {
fileSet.setFile(file);
}
Zip zip = new Zip();
zip.setProject(proj);
zip.setDestFile(new File(zipFilepath));
zip.addFileset(fileSet);
zip.setEncoding(encoding);
zip.execute();
}
// 压缩(可设定过滤条件)
public static void zip(String srcPathname, String zipFilepath, String[] Includes, String[] Excludes)
throws BuildException, RuntimeException {
File file = new File(srcPathname);
if (!file.exists())
throw new RuntimeException("文件或文件夹" + srcPathname + "不存在...");
// 删除目标路径中已存在的同名文件(根据实际情况需要)
File zipFile = new File(zipFilepath);
if (zipFile.exists())
zipFile.delete();
Project proj = new Project();
FileSet fileSet = new FileSet();
fileSet.setProject(proj);
// 判断是目录还是文件
if (file.isDirectory()) {
fileSet.setDir(file);
// 设定过滤条件
if(Includes != null && Includes.length > 0){
for(String Include : Includes){
fileSet.setIncludes(Include); // 只包括...
}
}
if(Excludes != null && Excludes.length > 0){
for(String Exclude : Excludes){
fileSet.setExcludes(Exclude); // 除..之外
}
}
} else {
fileSet.setFile(file);
}
Zip zip = new Zip();
zip.setProject(proj);
zip.setDestFile(new File(zipFilepath));
zip.addFileset(fileSet);
zip.setEncoding(encoding);
zip.execute();
}
// 解压缩
public static void unzip(String zipFilepath, String destDir)
throws BuildException, RuntimeException {
if (!new File(zipFilepath).exists())
throw new RuntimeException("zip文件" + zipFilepath + "不存在...");
Project proj = new Project();
Expand expand = new Expand();
expand.setProject(proj);
expand.setEncoding(encoding);
// expand.setOverwrite(false); // 不设置的话默认为true
expand.setSrc(new File(zipFilepath));
expand.setDest(new File(destDir));
expand.execute();
}
}
MyTest_ZIP.zip_brightaj3_java zip_zip
版权申诉
104 浏览量
2022-09-21
00:52:56
上传
评论
收藏 1.72MB ZIP 举报
小贝德罗
- 粉丝: 68
- 资源: 1万+
最新资源
- C语言基础-C语言编程基础之Leetcode编程题解之第39题组合总和.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第38题外观数列.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第37题解数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第36题有效的数独.zip
- C语言基础-C语言编程基础之Leetcode编程题解之第35题搜索插入位置.zip
- index.wxml
- C语言基础-C语言编程基础之Leetcode编程题解之第33题搜索旋转排序数组.zip
- 基于Python实现的手写数字识别系统源码.zip
- 从网页提取禁止转载的文字
- C语言基础-C语言编程基础之Leetcode编程题解之第32题最长有效括号.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈