java调用ant进行压缩和解压



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
* zip工具
*
* @author yuanhuiwu
*
*/
public class ZipUtils {
private static Logger log = Logger.getLogger(ZipUtils.class);
private static final int BUFFER_SIZE = 8 * 1024;

/**
* @param source
* zip源文件
* @param dest
* 解压保存的目录
* @throws IOException
* IO异常
*/
public static void unzip(File source, File dest) throws IOException {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(source);
unzip(zipFile, dest);
} finally {
if (zipFile != null) {
zipFile.close(); // 关闭文件
}
}
}

/**
* 解压zip文件
*
* @param source
* zip 源文件
* @param dest
* 输出目录
* @throws IOException
* IO异常
*/
@SuppressWarnings("unchecked")
private static void unzip(ZipFile source, File dest) throws IOException {

// 创建目录
if (!dest.exists()) { // 不存在
dest.mkdirs(); // 创建
}
Enumeration en = source.getEntries(); // 返回所有条目 Returns all

while (en.hasMoreElements()) {
ZipEntry zipEntry = en.nextElement();

if (zipEntry.isDirectory()) {// 目录,则创建
File dir = new File(dest, zipEntry.getName());
if (!dir.exists()) {
dir.mkdirs();
dir = null;
}
continue;
}
// 创建文件
File file = new File(dest, zipEntry.getName());
if (file.exists()) {
file.delete();
} else if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// end 创建文件
// 写入
InputStream in = null;
FileOutputStream out = null;
try {
in = source.getInputStream(zipEntry);
out = new FileOutputStream(file);
copy(in, out);
} finally {// 每次写入后都要关闭流,防止占用资源,导致不能其它操作(如删除该文件)!!!!!!
file = null;
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}

/**
* @param in
* 输入流
* @param out
* 输出流
* @throws IOException
* IO异常
*/
private static void copy(InputStream in, OutputStream out)
throws IOException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
do {
if (count != 0) {
out.write(buffer, 0, count);
}
count = in.read(buffer, 0, buffer.length);
} while (count != -1);
} finally {
if (out != null) {
out.flush();
}
}
}

/**
* 压缩成zip格式
*
* @param source
* 源文件或文件夹
* @param dest
* 输出的zip文件
* @throws IOException
* IO异常
*/
public static void zip(File source,File dest) throws IOException {
ZipOutputStream out = null;
FileOutputStream fos = null;
try {
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
if(dest.exists()){
dest.delete();
}
dest.createNewFile();

fos = new FileOutputStream(dest);
out = new ZipOutputStream(fos);
// out.setMethod(ZipOutputStream.DEFLATED);
// out.setLevel(Deflater.BEST_COMPRESSION);
zip(out, source, null);
} finally {
if (out != null) {
out.close();
}
if (fos != null) {
fos.close();
}
}
}

/**
* 压缩成zip格式
*
* @param out
* ZipOutputStream
* @param source
* File
* @param vpath
* String
* @throws IOException
* IO异常
*/
private static void zip(ZipOutputStream out, File source, String vpath)
throws IOException {
if (source.isDirectory()) { // 目录
if(vpath == null){
vpath = source.getName()+"/";
}else{
vpath = vpath +"/";
}
out.putNextEntry(new ZipEntry(vpath)); // 格式如:dir/
File[] files = source.listFiles();
for (int i = 0; i < files.length; i++) {
zip(out, files[i], vpath + files[i].getName()); // 递归
}
} else {// 文件
if(vpath == null){
vpath = source.getName();
}
ZipEntry ze = new ZipEntry(vpath);
// ze.setMethod(ZipEntry.DEFLATED);
out.putNextEntry(ze);

FileInputStream in = null;
try {
in = new FileInputStream(source);
copy(in, out);
} finally {
if (in != null) {
in.close();
}
}
}
}
//
// public static void unzip2(File source, File dest) {
// Project prj = new Project();
// Expand expand = new Expand();
// expand.setProject(prj);
// expand.setSrc(source);
// expand.setDest(dest);
// expand.execute();
// }
//
// public static void zip2(File dest, File source) {
// Project prj = new Project();
// Zip zip = new Zip();
// zip.setProject(prj);
// zip.setDestFile(dest);
// FileSet fileSet = new FileSet();
// fileSet.setProject(prj);
// fileSet.setDir(source);
// fileSet.setDefaultexcludes(false);
// zip.addFileset(fileSet);
// zip.execute();
// }
}

你可能感兴趣的:(ant)