Ant压缩与解压缩
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压缩与解压缩的更多相关文章
- 利用Java进行zip文件压缩与解压缩
摘自: https://www.cnblogs.com/alphajuns/p/12442315.html 工具类: package com.alphajuns.util; import java.i ...
- Java 基础【12】 压缩与解压缩
Java.util.zip 提供用于读写标准 ZIP 和 GZIP 文件格式的类. 还包括使用 DEFLATE 压缩算法(用于 ZIP 和 GZIP 文件格式)对数据进行压缩和解压缩的类. 依赖 Jd ...
- 菜鸟学Linux命令:tar命令 压缩与解压缩
tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件. tar最初被用来在磁带上创建档案,现在,用户可以 ...
- Linux系统之压缩、解压缩,vi编辑器,系统初始化服务和系统监控
一.正文处理,压缩与解压缩 1.内容重定向>与>> >:覆盖,将>号左边的结果覆盖到>号右边的文件中,如果文件不存在,则先创建一个新的空文件并覆盖 >> ...
- gzip [选项] 压缩(解压缩)
减少文件大小有两个明显的好处,一是可以减少存储空间,二是通过网络传输文件时,可以减少传输的时间.gzip是在Linux系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用. 语法:gzip ...
- 使用ICSharpCode.SharpZipLib.Zip实现压缩与解压缩
使用开源类库ICSharpCode.SharpZipLib.Zip可以实现压缩与解压缩功能,源代码和DLL可以从http://www.icsharpcode.net/OpenSource/SharpZ ...
- PclZip:强大的PHP压缩与解压缩zip类
PclZip简介PclZip是一个很强大的压缩与解压缩zip文件的PHP类,PclZip library能够压缩与解压缩Zip格式的压缩档(WinZip.PKZIP):且能对此类类档案进行处理,包括产 ...
- zip格式压缩、解压缩(C#)
压缩方法 #region 压缩 /// <summary> /// 压缩 /// </summary> /// <param name="bytes" ...
- Tar打包、压缩与解压缩到指定目录的方法
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
随机推荐
- R 分组计算描述性统计量
统计学区内各个小区的房价均值 数据格式 id|community_name|house_area|house_structure|house_total|house_avg|agency_name|h ...
- tail -f 然后grep,处理缓存的问题
学习了:http://www.quwenqing.com/read-134.html 对日志记录做多次grep过滤输出,格式如下: tail -f log | grep xxx | grep yyy ...
- (文档)Shader.Find (在编译时,只包含那些使用中的shader或位置在"Resources"文件夹中shader)
Shader.Find 查找 static function Find (name : string) : Shader Description描述 Finds a shader with the g ...
- Linux 查看服务器配置
//CPU cat /proc/cpuinfo |grep processor; //内存 free -g; //硬盘 df -h;
- Masonry应用【美图秀秀首页界面自动布局】
Masonry在此实现时候,并没有比NSLayoutConstraint简单,相反我觉得还不如NSLayoutConstraint. [self.topView mas_makeConstraints ...
- OpenGL ES 3.0顶点着色器(二)
#version es uniform mat4 u_mvpMatrix; in vec4 a_position; in vec4 a_color;out vec4 v_color;void main ...
- PHP如何发送邮件
例如使用163邮箱 SMTP,Simple Mail Transfer Protocol,简单邮件传输协议 保证我们的邮件服务器开启了SMTP服务 设置授权码 使用PHPMailer类发送邮件 1 ...
- 玩转Bootstrap
一:bootstrap基本模版 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- 〖Linux〗gun screen 配置文件
screen 默认使用 bash shell,一般情况下screen只用于android build,tmux则是我的日常使用多终端管理工具. # Default Shell shell " ...
- QTP Test ,VAPI-XP Test,LR Test 和ALM 集成远程分布式执行遇到的“access is denied ” “unspecified error”问题
大家都知道QTP与ALM (QC的升级版)集成是最好的一个分布式执行的结合.因为毕竟QTP是一个商业软件,HP当然不会让你去跟其他的open source的工具去集成,要不他到哪里去挣钱. 有时候服务 ...