import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipUtil {
/**
* 功能:压缩成zip
* @author 吴晓聪 添加于 2008-5-19
* @param zipFilep 压缩后的zip文件名
* @param path 压缩路径
* @throws java.lang.Exception
*/
public static void zip(String zipFilep, String path) throws Exception {
System.out.println("压缩文件开始.....................");
File zipFile = new File(zipFilep);
if (!zipFile.exists()) {
zipFile.createNewFile();
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
write(out, path, "");
out.close();
System.out.println("压缩文件结束!");
}
/**
* 功能:写压缩流
* @author 吴晓聪 添加于 2008-5-19
* @param out 压缩输出流
* @param path 压缩路径
* @param base 压缩式的基础目录
* @throws java.lang.Exception
*/
private static void write(ZipOutputStream out, String path, String base)
throws Exception {
File file = new File(path);
if (file.isDirectory()) {//文件夹,递归
base = base.length() == 0 ? "" : base + File.separator;
File[] tempFiles = file.listFiles();
for (int i = 0; i < tempFiles.length; i++) {
write(out, tempFiles[i].getPath(), base+tempFiles[i].getName());
}
} else {//文件,压缩
byte[] buff = new byte[2048];
int bytesRead = -1;
ZipEntry entry = new ZipEntry(base);
out.putNextEntry(entry);
InputStream in = new BufferedInputStream(new FileInputStream(file));
while (-1 != (bytesRead = in.read(buff, 0, buff.length))) {
out.write(buff, 0, bytesRead);
}
in.close();
out.flush();
}
}
/**
* 功能:解压缩
* @author 吴晓聪 添加于 2008-5-19
* @param zipFilename zip文件
* @param destPath 解压缩路径
* @throws java.lang.Exception
*/
public static void unZip(String zipFilename, String destPath) throws Exception {
System.out.println("解压文件开始.....................");
ZipFile zip = new ZipFile(zipFilename, "GB2312");
Enumeration enu = zip.getEntries();//得到压缩文件夹中的所有文件
while (enu.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enu.nextElement();
String file = destPath + entry.getName();
write(zip, entry, file);
}
System.out.println("解压文件结束!");
}
/**
* 功能:将解压缩之后的文件写入目录
* @author 吴晓聪 添加于 2008-5-19
* @param zip 压缩文件
* @param entry 压缩文件实体——压缩文件中的文件
* @param fileLocation 解压缩之后的文件路径
* @throws Exception
*/
private static void write(ZipFile zip, ZipEntry entry, String file) throws Exception {
if (entry.isDirectory()) {
File f = new File(file);
f.mkdirs();
} else {
File f = new File(file);
createDir(f);
FileOutputStream fos = new FileOutputStream(f);
byte[] buffer = new byte[8196];
InputStream is = zip.getInputStream(entry);
for (int len = is.read(buffer, 0, buffer.length); len != -1; len = is.read(buffer, 0, 8196)) {
fos.write(buffer, 0, len);
}
fos.close();
}
}
/**
* 功能:创建目录
* @author 吴晓聪 添加于 2008-5-19
* @param file 文件或目录
*/
private static void createDir(File file){
if(file.isDirectory() && !file.exists()){
file.mkdirs();
}else{
String path = file.getPath();
int i = path.lastIndexOf(File.separator);
path = path.substring(0,i);
new File(path).mkdirs();
}
}
/**
* 功能:测试样例
* @author 吴晓聪 添加于 2008-5-19
* @param args
*/
public static void main(String[] args) {
try {
zip("f://cong.zip", "F://资料");
unZip("f://cong.zip", "f://cong//");
} catch (Exception e) {
e.printStackTrace();
}
}
}
此源文件直接可以使用,关键点如下:
1 要解决中文乱码问题,需用ant下的tool工具包,只要将ant.jar放到类路径下即可。
2 解压缩时,得到的压缩实体为文件,创建文件时,需要先创建文件夹。
ant.jar包下载地址: