package com.uujava.mbfy.test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.tools.zip.ZipOutputStream; /** * @author k * @date 2014年10月7日 上午8:04:51 * @Description: TODO(用于压缩和解压缩zip文件) * 尚须解决bug: * 这里采用硬编码这定文件编码,是否有办法读取压缩文件时判断起内部文件名编码。 */ public final class ZipUtils { public static void main(String[] args) throws Exception { // ZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip", "/home/k/Documents/testzip/index.html"); unZipFile("/home/k/Documents/testzip/宽屏透明html5产品展示.zip", "/home/k/Documents/testzip/zip"); } public static void zip(ZipOutputStream out, File f, String base, boolean first) throws Exception { if (first) { if (f.isDirectory()) { out.putNextEntry(new org.apache.tools.zip.ZipEntry("/")); base = base + f.getName(); first = false; } else base = f.getName(); } if (f.isDirectory()) { File[] fl = f.listFiles(); base = base + "/"; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName(), first); } } else { out.putNextEntry(new org.apache.tools.zip.ZipEntry(base)); FileInputStream in = new FileInputStream(f); int b; System.out.println(base); while ((b = in.read()) != -1) { out.write(b); } in.close(); } } |
@SuppressWarnings("unchecked")
public static void unZipFileByOpache(org.apache.tools.zip.ZipFile zipFile,
String unZipRoot) throws Exception, IOException {
java.util.Enumeration e = zipFile.getEntries();
System.out.println(zipFile.getEncoding());
org.apache.tools.zip.ZipEntry zipEntry;
while (e.hasMoreElements()) {
zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
InputStream fis = zipFile.getInputStream(zipEntry);
if (zipEntry.isDirectory()) {
} 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 zipFileName, String inputFileName)
throws Exception {
org.apache.tools.zip.ZipOutputStream out = new org.apache.tools.zip.ZipOutputStream(
new FileOutputStream(zipFileName));
out.setEncoding("gbk");// 设置的和文件名字格式一样或开发环境编码设置一样的话就能正常显示了
File inputFile = new File(inputFileName);
zip(out, inputFile, "", true);
System.out.println("zip done");
out.close();
}
public static void unZipFile(String unZipFileName, String unZipPath)
throws Exception {
org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
unZipFileName, "gbk");
unZipFileByOpache(zipFile, unZipPath);
System.out.println("unZip Ok");
}
}