有兩種方式:
(1)使用jdk 自帶的zip工具
(2)使用apache旗下的commons-compress
我下面要講解的zip解壓縮助手使用的是apache旗下的commons-compress.
工具運作界面如下:
核心代碼:
解壓:
/***
* 解壓zip
*
* @param zipfile
* @param decompressloc
* :解壓之後的檔案所在目錄
* @throws archiveexception
* @throws ioexception
*/
public static boolean decompressrecursion(string zipfile,
file decompressloc, string charset) throws archiveexception,
ioexception {
fileinputstream fin = new fileinputstream(zipfile);
archiveinputstream archins = new archivestreamfactory()
.createarchiveinputstream(archivestreamfactory.zip, fin);
ziparchiveinputstream zipin = (ziparchiveinputstream) archins;
boolean issuccess = decompressrecursion(zipin, decompressloc, charset);
zipin.close();
return issuccess;
}
* 遞歸解壓縮.
* @param zipin
* @return
private static boolean decompressrecursion(ziparchiveinputstream zipin,
file decompressloc, string charset) throws ioexception {
ziparchiveentry zipentry;
if (valuewidget.isnullorempty(charset)) {
charset = systemhwutil.charset_utf;
}
while (!valuewidget.isnullorempty(zipentry = zipin.getnextzipentry())) {
byte[] rawname = zipentry.getrawname();
string filename = new string(rawname, charset);
// system.out.println(filename);
if (zipentry.isdirectory()) {// 是目錄
file newfolder = new file(decompressloc, filename);// 若子目錄不存在,則建立之
system.out.println(newfolder.getabsolutepath());
if (!newfolder.exists()) {
newfolder.mkdir();
}
// decompressrecursion(zipin, decompressloc,charset);
} else {// 是普通檔案
file singfile = new file(decompressloc, filename);
system.out.println(singfile.getabsolutepath());
if (singfile.exists()) {// 若解壓後的檔案已經存在,則直接退出
guiutil23.warningdialog("file \""
+ singfile.getabsolutepath() + "\" does exist.");
return false;
/**
* 以下四行代碼是後來添加的,為了解決父目錄不存在的問題
*/
file fatherfolder = singfile.getparentfile();
if (!fatherfolder.exists()) {
fatherfolder.mkdirs();
fileutils.writein2output(zipin, new fileoutputstream(singfile),
true, false);
}
return true;
壓縮:
* 壓縮檔案.
* @param folderpaths
public static boolean compressziprecursion(string zipfile,
string folderpaths) throws archiveexception, ioexception {
fileoutputstream fou = new fileoutputstream(zipfile);
archiveoutputstream archouts = new archivestreamfactory()
.createarchiveoutputstream(archivestreamfactory.zip, fou);
if (archouts instanceof ziparchiveoutputstream) {
ziparchiveoutputstream zipout = (ziparchiveoutputstream) archouts;
list<ziparchiveentry> zipentrys = getzipfilelistrecursion(new file(
folderpaths), null);
for (int i = 0; i < zipentrys.size(); i++) {
ziparchiveentry zipentry2 = zipentrys.get(i);
zipout.putarchiveentry(zipentry2);
file file = new file(folderpaths, zipentry2.getname());
if (!file.exists()) {
if (!file.isdirectory()) {
fileinputstream fin = new fileinputstream(file);
// 不要關閉zipout,關閉之前要執行closearchiveentry()
fileutils.writein2output(fin, zipout, false, true);
closezip(zipout, true);
* 壓縮之後的收尾操作.
* @param zipout
private static void closezip(ziparchiveoutputstream zipout,
boolean isclosearchiveentry) throws ioexception {
if (isclosearchiveentry) {
zipout.closearchiveentry();// it is necessary
zipout.flush();
zipout.finish();
zipout.close();
上述代碼見類:com.common.util.compressziputil
項目名:zip_mgmt
項目源代碼見附件:zip_mgmt.zip
項目使用maven 建構
ide:eclipse
依賴的jar包:(1)io0007-find_progess-0.0.8-snapshot.jar
(2)is_chinese
學習筆記見附件java zip壓縮.zip
http://www.cnblogs.com/un4sure/archive/2011/09/27/2193298.html,
http://hw1287789687.iteye.com/blog/1976309