import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class ZipFileUtils {
/**
* 能支持中文的压缩
* 参数base 开始为""
* first 开始为true
*/
private static void doZip(ZipOutputStream out, File f, String base, boolean first)
throws Exception {
if (first) {
if (f.isDirectory()) {
base = base + f.getName();
} else {
base = f.getName();
}
}
if (f.isDirectory()) {
File[] fl = f.listFiles();
base = base + "/";
for (int i = 0; i < fl.length; i++) {
doZip(out, fl[i], base + fl[i].getName(), false);
}
} else {
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(f);
int b;
//System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* 解压文件
* unZip为解压路径
*/
private static void doUnZip(ZipFile zipFile, String unZipRoot) throws Exception, IOException {
java.util.Enumeration<?> e = zipFile.getEntries();
ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
// TODO
} else {
File file = new File(unZipRoot + File.separator + zipEntry.getName());
File parentFile = file.getParentFile();
parentFile.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b, 0, b.length)) != -1) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
}
}
public static void zipFile(String zipToFile, String toBeZip) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipToFile));
out.setEncoding("gbk");//TODO 只能写死GBK?
File inputFile = new File(toBeZip);
doZip(out, inputFile, "", true);
//System.out.println("zip done");
out.close();
}
public static void unZipFile(String zipFilePath, String unZipToDir) throws Exception {
ZipFile zipFile = new ZipFile(zipFilePath);
doUnZip(zipFile, unZipToDir);
//System.out.println("unZip Ok");
}
public static void unZipFileFlat(String zipFilePath, String unZipToDir) throws Exception {
ZipFile zipFile = new ZipFile(zipFilePath);
java.util.Enumeration<?> e = zipFile.getEntries();
ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
// TODO
} else {
String name = zipEntry.getName();
int index = zipEntry.getName().lastIndexOf(File.separator);
if (index != -1) {
name = zipEntry.getName().substring(index);
}
File file = new File(unZipToDir + name);
File parentFile = file.getParentFile();
parentFile.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b, 0, b.length)) != -1) {
fos.write(b, 0, len);
}
fos.close();
fis.close();
}
}
}
public static void main(String[] args) throws Exception {
zipFile("d:/工作常用.zip", "D:\\temp");
// unZipFile("d:/javaFile.zip","e:/");
}
|