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 ...
随机推荐
- php 公共方法Util
总结了一个公共类方法类: class Util extends ArrayHelper { /** * 判断是否为空数组 * @param mixed $arr * @return boolean * ...
- Yii2 数据搜索类 PostSearch
数据搜索类 PostSearch /** * @Purpose : 添加 authorName 属性,使属性和搜索表单相对应 * @return array */ public function at ...
- eclipse中安装pydev插件出现duplicate location
eclipse中安装pydev插件出现duplicate location,主要是因为之前已经填写了该地址并且已经加载了,具体的解决办法见下链接: http://jingyan.baidu.com/a ...
- python每日笔记
9.28 查看python包路径: sys.path 9.4 sorted高级用法: >>> class Student: def __init__(self, name, gra ...
- nvidia-docker2配置与NVIDIA驱动安装
要运行高版本的GPU版TensorFlow,需要更新宿主机的显卡驱动(本文以NVIDIA390为例) 一.更新驱动 禁用nouveau驱动: 添加/etc/modprobe.d/blacklist.c ...
- [Linux] - 网速测试命令
Windows网速测试方法有很多,或者可以直接使用浏览器去一些网站上搜索测试即可. 在Linux中其实更容易,只需要一行命令即可搞定: curl -s https://raw.githubuserco ...
- FileClassify文件日期分类工具
FileClassify是一款免费的文件按日期分类工具,能够根据文件修改日期,将文件移动或复制到对应的目录中 如果对您有较大的帮助,欢迎捐赠我们,我们对您表示衷心的感谢! 1.输入文件夹和输出文件可以 ...
- 雅克比迭代算法(Jacobi Iterative Methods) -- [ mpi , c++]
雅克比迭代,一般用来对线性方程组,进行求解.形如: \(a_{11}*x_{1} + a_{12}*x_{2} + a_{13}*x_{3} = b_{1}\) \(a_{21}*x_{1} + a_ ...
- 关于 Docker Hub 上不能注册 Docker ID 的问题
1. 引言 我们中国大陆访问dockerhub的时候,想要注册一个dockerID,发现sign up按钮是灰色的,不能点击进行注册.这个时候通过点击右键"查看网页源代码"和&qu ...
- 【Vegas原创】VirtualBox扩容、分割的整体方案
背景:在低压i7上跑vmware,卡顿无极限,决定转战virtual Box. 但是VirtualBox最讨厌的一点,就是vdi文件无法分割,平时习惯备份的我,百度网盘最大也就20G的上传.咋整? v ...