Java Zip文件解壓縮
真實無語了,絕大多數是不能用的。這可能跟我的開發環境有關吧。
我用的是Ubuntu14.04,eclipse 用的是STS3.5,jdk81.8.0_20
經過兩天的努力檢驗了無數的code終于讓我找到一個還能用的可以解決中文亂碼問題。
這個項目用maven構建的依賴jar坐標如下
<!-- 用于zip文件解壓縮 -->
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.7.0</version>
</dependency>
代碼如下:
package com.uujava.mbfy.test; 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.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");
}
}
posted on 2014-10-09 10:28 順其自然EVO 閱讀(181) 評論(0) 編輯 收藏 所屬分類: 測試學習專欄