真實無語了,絕大多數是不能用的。這可能跟我的開發環境有關吧。
我用的是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.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();
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()) {
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)
org.apache.tools.zip.zipfile zipfile = new org.apache.tools.zip.zipfile(
unzipfilename, "gbk");
unzipfilebyopache(zipfile, unzippath);
system.out.println("unzip ok");
最新内容請見作者的github頁:http://qaseven.github.io/