有两种方式:
(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