/**
* copyright 2002-2010 the original author is huanghe.
*/
package com.ucap.web.cm.webapp.util;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.util.arraylist;
import java.util.enumeration;
import java.util.list;
import org.apache.commons.io.fileutils;
import org.apache.commons.io.ioutils;
import org.apache.tools.zip.zipentry;
import org.apache.tools.zip.zipfile;
import org.apache.tools.zip.zipoutputstream;
import com.ucap.template.constants;
import com.ucap.utils.uuidgenerator;
import com.ucap.utils.formatstring.formatstring;
import com.ucap.utils.formatstring.validator;
* 压缩和解压缩工具类
@suppresswarnings("unchecked")
public class ziputil {
private static int bufsize = 4096;
private static byte[] buf = new byte[bufsize];
private static string os_type;
static {
if (system.getproperty("os.name").equals("linux")) {
os_type = "linux";
} else if (system.getproperty("os.name").indexof("windows") != -1) {
os_type = "windows";
}
}
public ziputil() {
/**
* 压缩文件夹内的文件
*
* @param zipdirectory
* 需要压缩的文件夹名
* @return file 压缩文件对象
*/
public static file dozip(string zipdirectory) {
zipoutputstream zipout;
file zipdir = new file(zipdirectory);
string zipfilename = zipdir.getname() + ".zip";// 压缩后生成的zip文件名
if (system.getproperty("os.name").startswith("windows")) {
if (!zipdirectory.endswith("\\"))
zipdirectory = zipdirectory + "\\";
} else {
if (!zipdirectory.endswith("/"))
zipdirectory = zipdirectory + "/";
//判断压缩文件是否已经存在,如果存在则删除
file prezip = new file(zipdirectory + "/" + zipfilename);
if (prezip.exists()) {
try {
fileutils.forcedelete(prezip);
} catch (ioexception e) {
e.printstacktrace();
}
//创建临时目录
file tempfolder = createtempfolder();
string temppath = tempfolder.getabsolutepath();
file zipfile = new file(temppath + "/" + zipfilename);
if (!zipfile.getparentfile().exists())
zipfile.getparentfile().mkdirs();
if (zipfile.exists() && zipfile.canwrite())
zipfile.delete();// 如果文件存在就删除原来的文件
try {
zipout = new zipoutputstream(new bufferedoutputstream(new fileoutputstream(zipfile)));
handledir(zipout, zipdir, "");
zipout.close();
fileutils.copyfiletodirectory(zipfile, zipdir);
} catch (ioexception ioe) {
ioe.printstacktrace();
} finally {
//删除临时文件夹
if (tempfolder.exists()) {
try {
fileutils.deletedirectory(tempfolder);
} catch (ioexception e) {
e.printstacktrace();
}
file zip = new file(zipdir + "/" + zipfilename);
return zip;
* 由dozip调用,递归完成目录文件读取
private static void handledir(zipoutputstream out, file f, string base) throws ioexception {
if (f.isdirectory()) {
file[] fl = f.listfiles();
if (system.getproperty("os.name").startswith("windows")) {
base = base.length() == 0 ? "" : base + "\\";
//out.putnextentry(new org.apache.tools.zip.zipentry(base));
} else {
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
handledir(out, fl[i], base + fl[i].getname());
out.putnextentry(new org.apache.tools.zip.zipentry(base));
fileinputstream in = new fileinputstream(f);
byte b[] = new byte[512];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.closeentry();
in.close();
* 解压指定zip文件
* @param unzipfilename
* 需要解压的zip文件
* @param destpath
* 目录文件夹,如果目标文件夹为null ,则解压到当前目录下
* @param isdeletesrc
* 是否删除原压缩文件
* @throws exception
public static list<string> unzip(file zipfilename, string destpath, boolean isdeletesrc)
throws exception {
list<string> ret = new arraylist<string>();
if (zipfilename == null)
return ret;
if (destpath == null)
destpath = zipfilename.getabsolutepath().substring(0,
zipfilename.getabsolutepath().lastindexof("\\"))
+ "\\";
fileoutputstream fileout;
file file;
inputstream inputstream;
zipfile zipfile;
int readedbytes;
if (system.getproperty("os.name").equals("linux"))
zipfile = new org.apache.tools.zip.zipfile(zipfilename,"gbk");
else
zipfile = new org.apache.tools.zip.zipfile(zipfilename);
for (enumeration entries = zipfile.getentries(); entries.hasmoreelements();) {
zipentry entry = (zipentry) entries.nextelement();
if (system.getproperty("os.name").equals("linux"))
entry.setunixmode(644);//解决linux乱码
file = new file(temppath + "/" + entry.getname());
if (entry.isdirectory()) {
if (!file.exists())
fileutils.forcemkdir(file);
} else {
// 如果指定文件的目录不存在,则创建之.
file parent = file.getparentfile();
if (!parent.exists()) {
fileutils.forcemkdir(parent);
}
ret.add(entry.getname());
inputstream = zipfile.getinputstream(entry);
if (isrequiredsuffix(file.getabsolutepath(), constants.required_encode_suffixs)) {
string content = ioutils.tostring(inputstream, "utf-8");
fileutils.writestringtofile(file, content, "utf-8");
} else {
fileout = new fileoutputstream(file);
while ((readedbytes = inputstream.read(buf)) > 0) {
fileout.write(buf, 0, readedbytes);
}
fileout.close();
inputstream.close();
zipfile.close();
file destfolder = new file(destpath);
if (!destfolder.exists()) {
destfolder.mkdir();
fileutils.copydirectory(tempfolder, destfolder);
//删除上传的压缩文件
if (isdeletesrc)
zipfilename.delete();
return ret;
private static file createtempfolder() {
file tempfolder = null;
string temppath = "";
try{
string tempfilename = uuidgenerator.generate();
if (os_type.equals("window"))
temppath = "c:/" + tempfilename;
else
temppath = "/tmp/" + tempfilename;
tempfolder = new file(temppath);
if (!tempfolder.exists()) {
tempfolder.mkdir();
}
}catch (exception e) {
system.out.println("createtempfolder:"+temppath +" exception:" + e.getmessage());
}
return tempfolder;
// 设置缓冲区大小
public void setbufsize(int bufsize) {
this.bufsize = bufsize;
// 测试antzip类
public static void main(string[] args) throws exception {
ziputil m_zip = new ziputil();
string filepath = "c:\\template\\template_upload/site/";
m_zip.dozip(filepath);
} catch (exception ex) {
ex.printstacktrace();
* 判断文件的后缀名是否包含在是否以suffixs中
* @param filename
* @param suffixs
* @return
public static boolean isrequiredsuffix(string filename, string... suffixs) {
if (validator.isempty(filename)) {
return false;
if (suffixs == null || suffixs.length < 1) {
for (string str : suffixs) {
if (filename.indexof("." + str) == filename.length() - ("." + str).length()) {
return true;
return false;
/**
* 判断解压的文件是否包含汉字。
*
* @param zipfilename 要解压的文件
* @return 返回判断结果,true 含有 ;false 不含有
public static boolean ishavechinese(file zipfilename) {
zipfile zipfile = null;
zipfile = new zipfile(zipfilename);
zipentry zipentry = null;
enumeration e = zipfile.getentries();
while (e.hasmoreelements()) {
zipentry = (zipentry) e.nextelement();
if (formatstring.ishavechinese(zipentry.getname())) {
return true;
} catch (ioexception e1) {
e1.printstacktrace();
zipfile.close();
}