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解压缩工具类的更多相关文章

  1. RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2 新增解压缩工具类ZipHelper

    在项目对文件进行解压缩是非常常用的功能,对文件进行压缩存储或传输可以节省流量与空间.压缩文件的格式与方法都比较多,比较常用的国际标准是zip格式.压缩与解压缩的方法也很多,在.NET 2.0开始,在S ...

  2. GZIP压缩、解压缩工具类

    GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...

  3. AntZipUtils【基于Ant的Zip压缩解压缩工具类】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android 压缩解压zip文件一般分为两种方式: 基于JDK的Zip压缩工具类 该版本存在问题:压缩时如果目录或文件名含有中文, ...

  4. C# 解压缩工具类GZip

    using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using ...

  5. 免费rar/zip解压缩工具BandZip

    今天为大家推荐一款解压缩类软件--BandZip bandzip是我认为的最好用的解压缩软件,速度快没广告 能够秒杀其他的压缩类软件 下载地址 bandzip点我 1 BandZip简介 BandZi ...

  6. ZIP解压缩文件的工具类【支持多级文件夹|全】

    ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...

  7. ZIP解压缩文件的工具类【支持多级目录|全】

    ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...

  8. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  9. java解压缩zip和rar的工具类

    package decompress; import java.io.File; import java.io.FileOutputStream; import org.apache.tools.an ...

随机推荐

  1. Java 始终要覆盖toString

    java.lang.Object提供了toString方法的一个实现,它包含类的名称,以及一个“@”符号,接着是散列码的无符号十六进制表示法,例如“PhoneNumber@163b91”.在实际应用中 ...

  2. Redis、Memcache、MongoDb的优缺点

    Redis.Memcache.MongoDb的优缺点 Redis优点 支持多种数据结构,如 string(字符串). list(双向链表).dict(hash表).set(集合).zset(排序set ...

  3. Jsp俩大内置对象学习

    https://www.cnblogs.com/smyhvae/p/4065790.html post与get的区别 最直观的区别就是GET把参数包含在URL中,POST通过request body传 ...

  4. 让公式在网页传播——mathJAX

    让公式在网页传播--mathJAX 对于学生党而言,写公式最好的工具是LaTeX,但LaTeX把公式展示到互联网上就有些困难,而使用截图又不太雅观.幸运的是,mathJAX引擎可以在浏览器中解析渲染数 ...

  5. git https 请求报错 504

    git https 请求报错 504 原因可能是因为设置了代理,ubuntu/deepin 系统可以检查 /etc/profile ~/.bashrc 内有没有设置 https 的代理. 有的话,去掉 ...

  6. idea其他人把jar更新之后更新不到

    昨天下午开始就发现这个问题,其他同事把jar更新了之后,我一直获取不到更新之后的内容.尝试了很多方法,删除具体的更新不到的jar,一直不停的mvn clean install -U -Dmaven.t ...

  7. CentOS 7配置成网关服务器

    其实在Linux下配置网关服务器很简单,如果配置好之后出现无法访问外网的情况,那么可以排查以下情况: 1.防火墙和iptables的服务关掉(firewalld.iptables) 2.清空iptab ...

  8. pygame-KidsCanCode系列jumpy-part0-使用sprite

    油管(youtube)上有一个号称"史上最好的pygame教程"(传送门:https://www.youtube.com/watch?v=VO8rTszcW4s&list= ...

  9. WPF之几何图形Geometry

    在WPF的DrawingContext对象中,提供了基本的绘制椭圆和矩形的API:DrawEllipse和DrawRectangle.但是,这些是远远不够用的,我们在日常应用中,更多的是使用DrawG ...

  10. 简易RPC框架-SPI

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...