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 ...
随机推荐
- WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: <NSInvalidArgumentException> -[__NSArrayM objectForKey:]: unrecognized s
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...
- 100405之python程序安装
Python的安装 注意安装的path和.exe可执行文件的命名方式 例如py2或者py3 sublime的安装 一.Sublime Text 3安装(64位Windows操作系统): 1.双击 Su ...
- plsql 工具怎样导出 oracle 表结构
一.双击 plsql 工具,输入登陆用户.登陆密码以及登陆数据库名称,如下图: 二.菜单 Tools --> Export User Objects...,如下图: 三.进入导出界面后,可以选择 ...
- JAVA自学笔记10
JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...
- reduce方法简单实现数组中对象去重
js数组的reduce方法,接收一个函数(必须)和指定的初始值(非必须)作为参数,函数有三个参数,分别为初始值,当前项,当前数组,进行累加或者累积操作,初始值为每次累加或者累计后的结果 注意:在ie9 ...
- 首席技术官应该考虑的网络安全问题 IT大咖说 - 大咖干货,不再错过
首席技术官应该考虑的网络安全问题 IT大咖说 - 大咖干货,不再错过 http://www.itdks.com/dakalive/detail/5523
- 使用Qemu运行Ubuntu文件系统(1)
参考 https://blog.csdn.net/mountzf/article/details/51707853 https://blog.csdn.net/stephen_lu_fahai/art ...
- 国际化之Android设备支持的语种
昨天发了关于iOS支持的语种,文章最后也补了安卓支持语种列表.但最后发现安卓设备支持跟它列的有出入,我重新完全手工整理了一遍. 我将对应的语种在安卓的语言列表里的显示,也全部逐一列出来了,方便大家到时 ...
- Data type conversion in MongoDB
[问题] I have a collection called Document in MongoDB. Documents in this collection have a field calle ...
- idea插件actiBPM源码
actiBPM https://github.com/Activiti/Activiti