转载:https://www.cnblogs.com/zhangfei/p/3296627.html
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());
}
}
/**
*
* @param zipFilePath 待解压的zip文件
* @param outputDirectory 解压后存放路径
* @return
*/
@SuppressWarnings("unchecked")//抑制编辑器的警告
public boolean unZip(String zipFilePath, String outputDirectory) {
boolean flag = false;
try {
//连接待压缩文件
ZipFile zipFile = new ZipFile(zipFilePath);
//得到zip包中所有的元素
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);
}
//Windows下File.separator是"\",这个根据不同的操作系统来决定的,Linux下是"/"
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");
}
}