package com.test.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile; public class ZipFileHandler { private final int buffer = 1024; private void createDirectory(String directory, String subDirectory) {
String dir[];
File fl = new File(directory);
try {
if (subDirectory == "" && fl.exists() != true) {
fl.mkdir();
} else if (subDirectory != "") {
dir = subDirectory.replace('\\', '/').split("/");
for (int i = 0; i < dir.length; i++) {
File subFile = new File(directory + File.separator + dir[i]);
if (subFile.exists() == false) {
subFile.mkdir();
}
directory += File.separator + dir[i];
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
} @SuppressWarnings("unchecked")
public boolean unZip(String zipFilePath, String outputDirectory) {
boolean flag = false;
try {
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration e = zipFile.getEntries();
ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unzip " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName().trim();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
if (!f.exists()) {
f.mkdir();
} } else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName
.substring(fileName.lastIndexOf("/") + 1);
}
File f = new File(outputDirectory + File.separator
+ zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
byte[] by = new byte[buffer];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
in.close();
out.close();
}
}
flag = true;
System.out.println("unzip finished");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return flag;
} public boolean zip(String srcDirName, String zipFilePath) {
boolean flag = false;
try {
File srcdir = new File(srcDirName);
if (!srcdir.exists())
throw new RuntimeException(srcDirName + " is not exist!"); Project prj = new Project();
Zip zip_ = new Zip();
zip_.setProject(prj);
zip_.setDestFile(new File(zipFilePath)); FileSet fileSet = new FileSet();
fileSet.setProject(prj);
fileSet.setDir(srcdir);
zip_.addFileset(fileSet); zip_.execute();
flag = true;
System.out.println("zip finished");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
return flag;
} public static void main(String[] args) {
ZipFileHandler zh = new ZipFileHandler();
zh.unZip("C:\\test.zip", "C:\\test1");
zh.zip("C:\\test1", "C:\\test1.zip");
} }

Ant压缩与解压缩的更多相关文章

  1. 利用Java进行zip文件压缩与解压缩

    摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...

  2. Java 基础【12】 压缩与解压缩

    Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...

  3. 菜鸟学Linux命令:tar命令 压缩与解压缩

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件. tar最初被用来在磁带上创建档案,现在,用户可以 ...

  4. Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控

    一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...

  5. gzip [选项] 压缩(解压缩)

    减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...

  6. 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩

    使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...

  7. PclZip:强大的PHP压缩与解压缩zip类

    PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...

  8. zip格式压缩、解压缩(C#)

    压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...

  9. Tar打包、压缩与解压缩到指定目录的方法

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...

随机推荐

  1. Kendall's tau-b(肯德尔)等级相关系数

    Kendall's tau-b(肯德尔)等级相关系数:用于反映分类变量相关性的指标,适用于两个分类变量均为有序分类的情况.对相关的有序变量进行非参数相关检验:取值范围在-1-1之间,此检验适合于正方形 ...

  2. w3cscholl的在线代码编辑工具2

    https://www.w3cschool.cn/tryrun/runcode?lang=c

  3. virtualBox导入虚拟机启动报错

    今天使用Oracle VMbox在导入虚拟机后,启动时报了如下错误: A new node couldn't be inserted because one with the same name ex ...

  4. $#65279导致页面body会出现一个空白行

    模板文件生成html文件之后会在body开头处加入一个可见的控制符&#65279,导致页面头部会出现一个空白行.原因是页面的编码是UTF-8 + BOM. 直接notopad++ 保存为无BO ...

  5. 【python】bytes与字符串的相互转化

    代码: # bytes转字符串方式一 b=b'\xe9\x80\x86\xe7\x81\xab' string=str(b,'utf-8') print(string) # bytes转字符串方式二 ...

  6. 恶性循环中的永生bug,可以说是相当写实了

    恶性循环中的永生bug,可以说是相当写实了

  7. SpringBoot报错 : Whitelabel Error Page

    添加了一个Controller类,本来想试下Spring MVC是否可以正常运行,结果报错,Controller类的内容: @RestController public class Test1Cont ...

  8. asp.net 定时执行任务代码 定时采集数据

    using System; using System.Data; using System.Configuration; using System.Collections; using System. ...

  9. 基于FPGA的超声波测距(一)

    硬件平台:CycloneII EP2C5Q208C8+DYP-ME007 1:超声波原理 DYP-ME007超声波测距模块可提供3cm--3.5m的非接触式距离感测功能,图1为DYP-ME007外观, ...

  10. 微信小程序 - async/await

    下面只是做一些介绍以及使用的原因,详情介绍还请移步博主:https://www.cnblogs.com/SamWeb/p/8417940.html regenerator-runtime下载:http ...