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 ...
随机推荐
- click()和onclick()的区别
click()和onclick()的区别: 1.onclick是绑定事件,告诉浏览器在鼠标点击时候要做什么 click本身是方法作用是触发onclick事件,只要执行了元素的click()方法,就会触 ...
- react-native Execution failed for task ':app:prepareRnReduxReactNativeUpdateUnspecifiedLibrary'报错
详细报错 Could not copy zip entry E:\项目目录\node_modules\react-native-update\android\build\outputs\aar\rea ...
- JDBC(14)—对DAO进行改进修改
结构: DAO2_7< T >(接口)->DAOTestImpl< T >(实现类)->CustomerDAO(继承的子类)->CustomerDAOTest ...
- seq_file学习(2)—— seq_file
li {list-style-type:decimal;}ol.wiz-list-level2 > li {list-style-type:lower-latin;}ol.wiz-list-le ...
- 深度残差网络(DRN)ResNet网络原理
一说起“深度学习”,自然就联想到它非常显著的特点“深.深.深”(重要的事说三遍),通过很深层次的网络实现准确率非常高的图像识别.语音识别等能力.因此,我们自然很容易就想到:深的网络一般会比浅的网络效果 ...
- Golang LicenseServer授权服务器的设计 与 RSA 密钥对的应用
//TODO 待写文章 目录: 1.为什么要写授权服务器 LicenseServer 2.授权服务器的设计思路 3.授权服务器所使用到的加密技术 1.为什么要写授权服务器 为了防止别人拿到二进制后, ...
- javaweb中使用百度、谷歌地图进行定位
第一种 百度 直接上代码: <!DOCTYPE> <html> <head> <meta name="viewport" content ...
- redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error(转)
redis启动出错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error windows下安装Redis第一次启动报 ...
- ConcurrentHashMap代码解析
ConcurrentHashMap (JDK 1.7)的继承关系如下: 1. ConcurrentHashMap是线程安全的hash map.ConcurrentHashMap的数据结构是一个Segm ...
- JAVA中的ZoneId常用值备注
一.获取代码 @Test public void zonesTest() { for (String availableZoneId : ZoneId.getAvailableZoneIds()) { ...