ZIP解压缩工具类
import java.io.File;
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解压缩工具类
*/
public class Zipper { // ZIP解压缩时
public final static String encoding = "GBK"; /**
* 压缩文件或文件夹
* ------------------------------------
* @param srcPathname 需要被压缩的文件或文件夹路径
* @param zipFilepath 将要生成的ZIP文件路径
*/
public static void zip(String srcPathname, String zipFilepath) throws Exception {
System.out.println(String.format("start compress ( %s ).", srcPathname)); // 检查文件是否存在
File file = new File(srcPathname);
if (!file.exists()) {
throw new RuntimeException(String.format("source file or directory ( %s ) does not exist.", srcPathname));
} // 创建项目
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(); System.out.println(String.format("compress successed of ( %s ). zip file is ( %s )", srcPathname, zipFilepath));
} /**
* 解压缩文件或文件夹
* ------------------------------------
* @param zipFilepath 需要被解压的ZIP文件路径
* @param destDir 将要被解压到的目标文件夹
*/
public static void unzip(String zipFilepath, String destDir) throws Exception {
System.out.println(String.format("start uncompress ( %s ).", zipFilepath)); // 判断要解压的ZIP包是否存在
File file = new File(zipFilepath);
if (!file.exists()) {
throw new RuntimeException(String.format("zip file ( %s ) does not exist.", zipFilepath));
} // 创建项目
Project proj = new Project(); // 解压设置
Expand expand = new Expand();
expand.setProject(proj);
expand.setTaskType("unzip");
expand.setTaskName("unzip");
expand.setEncoding(encoding); expand.setSrc(new File(zipFilepath));
expand.setDest(new File(destDir)); // 执行解压
expand.execute(); System.out.println(String.format("uncompress successed of ( %s ).", zipFilepath));
} /**
* 测试解压缩方法
*/
public static void main(String[] args) { /*try {
String srcPathname = "D:\\test\\test";
String zipFilepath = "D:\\test\\test.zip";
Zipper.zip(srcPathname, zipFilepath);
} catch (Exception e) {
e.printStackTrace();
}*/ try {
String zipFilepath = "D:\\test\\test.zip";
String destDir = "D:\\test\\upzip";
Zipper.unzip(zipFilepath, destDir);
} catch (Exception e) {
e.printStackTrace();
} }
}
依赖jar
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.9.7</version>
</dependency>
ZIP解压缩工具类的更多相关文章
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper
在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间.压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式.压缩与解压缩的方法也很多,在.NET 2.0开始,在S ...
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- AntZipUtils【基于Ant的Zip压缩解压缩工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...
- C# 解压缩工具类GZip
using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...
- 免费rar/zip解压缩工具BandZip
今天为大家推荐一款解压缩类软件--BandZip bandzip是我认为的最好用的解压缩软件,速度快没广告 能够秒杀其他的压缩类软件 下载地址 bandzip点我 1 BandZip简介 BandZi ...
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- ZIP解压缩文件的工具类【支持多级目录|全】
ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...
- C#工具类:使用SharpZipLib进行压缩、解压文件
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...
- java解压缩zip和rar的工具类
package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...
随机推荐
- 自己遇到过的出现java.lang.StackOverflowError的原因
public static JSONArray geth24Weather(String result) {//获取当天24小时以及第二天的天气结果对象 JSONObject fromO ...
- Flask蓝图
它的作用就是将 功能 与 主服务 分开怎么理解呢? 比如说,你有一个客户管理系统,最开始的时候,只有一个查看客户列表的功能,后来你又加入了一个添加客户的功能(add_user)模块, 然后又加入了一个 ...
- JS_高程5.引用类型(5)Array类型的操作方法
一.操作方法 1.concat()方法 基于当前数组中的所有项创建一个新数组.具体说,是先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组.在没有给concat() ...
- mvc中AntiForgeryToken的实现方式--看Mvc源码
通过 AntiForgeryWorker的GetHtml()方法生成html --input hide元素--value=要验证的值,并生成cookie--用于保存需要验证的值. 类中的AntiFor ...
- python标准库大全(转)
3. 清晰的标准库大全,带例子 2. 必会标准库 http://lizhenliang.blog.51cto.com/7876557/1872538 1. 标准库大全,链接版 http://blog. ...
- poj3617 Best Cow Line(贪心,字典序问题)
https://vjudge.net/problem/POJ-3617 这类字符串处理字典序问题经常用到贪心, 每决定输出一个字符之前,都要前后i++,j--逐个比大小,直至比出为止. #includ ...
- hihocoder1323 回文字符串(区间dp)
https://hihocoder.com/problemset/problem/1323 刚开始真没看出来这是一道dp题.. dp[i][j]表示i~j段修改成回文串所需的最少操作次数.然后根据s[ ...
- 如何移除HTML5 input在type="number"时的上下小箭头
在chrome下: input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{ -webkit-appearance ...
- SharePoint Excel Service - Couldn't Open the Workbook.
Error meesage: "Couldn't Open the Workbook. Wow, That's a big workbook. Unfortunately, we can't ...
- [Python设计模式] 第25章 联合国维护世界和平——中介者模式
github地址:https://github.com/cheesezh/python_design_patterns 题目背景 联合国在世界上就是中介者的角色,各国之间的关系复杂,类似不同的对象和对 ...