publicclassZipUtils{
privatestaticfinalintBUFF_SIZE=1024*1024;//1MByte
/**
*批量压缩文件(夹)
*
*@paramresFileList
*要压缩的文件(夹)列表
*@paramzipFile
*生成的压缩文件
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticvoidzipFiles(Collection
throwsIOException{
ZipOutputStreamzipout=newZipOutputStream(newBufferedOutputStream(
newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
zipout.close();
}
/**
*批量压缩文件(夹)
*
*@paramresFileList
*要压缩的文件(夹)列表
*@paramzipFile
*生成的压缩文件
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticbooleanzipPhotoFiles(Collection
FilezipFile)throwsIOException{
try{
ZipOutputStreamzipout=newZipOutputStream(
newBufferedOutputStream(newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
zipout.close();
returntrue;
}catch(IOExceptione){
Log.v("show","ZipUtils文件压缩异常");
returnfalse;
}
}
/**
*批量压缩文件(夹)
*
*@paramresFileList
*要压缩的文件(夹)列表
*@paramzipFile
*生成的压缩文件
*@paramcomment
*压缩文件的注释
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticvoidzipFiles(Collection
Stringcomment)throwsIOException{
ZipOutputStreamzipout=newZipOutputStream(newBufferedOutputStream(
newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
zipout.setComment(comment);
zipout.close();
}
/**
*解压缩一个文件
*
*@paramzipFile
*压缩文件
*@paramfolderPath
*解压缩的目标目录
*@throwsIOException
*当解压缩过程出错时抛出
*/
publicstaticvoidupZipFile(FilezipFile,StringfolderPath)
throwsZipException,IOException{
FiledesDir=newFile(folderPath);
if(!desDir.exists()){
desDir.mkdirs();
}
ZipFilezf=newZipFile(zipFile);
for(Enumeration>entries=zf.entries();entries.hasMoreElements();){
ZipEntryentry=((ZipEntry)entries.nextElement());
InputStreamin=zf.getInputStream(entry);
Stringstr=folderPath+File.separator+entry.getName();
str=newString(str.getBytes("8859_1"),"GB2312");
FiledesFile=newFile(str);
if(!desFile.exists()){
FilefileParentDir=desFile.getParentFile();
if(!fileParentDir.exists()){
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStreamout=newFileOutputStream(desFile);
bytebuffer[]=newbyte[BUFF_SIZE];
intrealLength;
while((realLength=in.read(buffer))>0){
out.write(buffer,0,realLength);
}
in.close();
out.close();
}
zf.close();
}
publicstaticvoidmain(String[]args){
try{
Stringzippath="e:\\zip.zip";
Stringfond="e:\\zip";
Filefile=newFile(zippath);
upZipFile(file,fond);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*解压文件名包含传入文字的文件
*
*@paramzipFile
*压缩文件
*@paramfolderPath
*目标文件夹
*@paramnameContains
*传入的文件匹配名
*@throwsZipException
*压缩格式有误时抛出
*@throwsIOException
*IO错误时抛出
*/
publicstaticArrayList
StringfolderPath,StringnameContains)throwsZipException,
IOException{
ArrayList
FiledesDir=newFile(folderPath);
if(!desDir.exists()){
desDir.mkdir();
}
ZipFilezf=newZipFile(zipFile);
for(Enumeration>entries=zf.entries();entries.hasMoreElements();){
ZipEntryentry=((ZipEntry)entries.nextElement());
if(entry.getName().contains(nameContains)){
InputStreamin=zf.getInputStream(entry);
Stringstr=folderPath+File.separator+entry.getName();
str=newString(str.getBytes("8859_1"),"GB2312");
//str.getBytes("GB2312"),"8859_1"输出
//str.getBytes("8859_1"),"GB2312"输入
FiledesFile=newFile(str);
if(!desFile.exists()){
FilefileParentDir=desFile.getParentFile();
if(!fileParentDir.exists()){
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStreamout=newFileOutputStream(desFile);
bytebuffer[]=newbyte[BUFF_SIZE];
intrealLength;
while((realLength=in.read(buffer))>0){
out.write(buffer,0,realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
returnfileList;
}
/**
*获得压缩文件内文件列表
*
*@paramzipFile
*压缩文件
*@return压缩文件内文件名称
*@throwsZipException
*压缩文件格式有误时抛出
*@throwsIOException
*当解压缩过程出错时抛出
*/
publicstaticArrayList
throwsZipException,IOException{
ArrayList
Enumeration>entries=getEntriesEnumeration(zipFile);
while(entries.hasMoreElements()){
ZipEntryentry=((ZipEntry)entries.nextElement());
entryNames.add(newString(getEntryName(entry).getBytes("GB2312"),
"8859_1"));
}
returnentryNames;
}
/**
*获得压缩文件内压缩文件对象以取得其属性
*
*@paramzipFile
*压缩文件
*@return返回一个压缩文件列表
*@throwsZipException
*压缩文件格式有误时抛出
*@throwsIOException
*IO操作有误时抛出
*/
publicstaticEnumeration>getEntriesEnumeration(FilezipFile)
throwsZipException,IOException{
ZipFilezf=newZipFile(zipFile);
returnzf.entries();
}
/**
*取得压缩文件对象的注释
*
*@paramentry
*压缩文件对象
*@return压缩文件对象的注释
*@throwsUnsupportedEncodingException
*/
publicstaticStringgetEntryComment(ZipEntryentry)
throwsUnsupportedEncodingException{
returnnewString(entry.getComment().getBytes("GB2312"),"8859_1");
}
/**
*取得压缩文件对象的名称
*
*@paramentry
*压缩文件对象
*@return压缩文件对象的名称
*@throwsUnsupportedEncodingException
*/
publicstaticStringgetEntryName(ZipEntryentry)
throwsUnsupportedEncodingException{
returnnewString(entry.getName().getBytes("GB2312"),"8859_1");
}
/**
*压缩文件
*
*@paramresFile
*需要压缩的文件(夹)
*@paramzipout
*压缩的目的文件
*@paramrootpath
*压缩的文件路径
*@throwsFileNotFoundException
*找不到文件时抛出
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticvoidzipFile(FileresFile,ZipOutputStreamzipout,
Stringrootpath)throwsFileNotFoundException,IOException{
rootpath=rootpath
+(rootpath.trim().length()==0?"":File.separator)
+resFile.getName();
rootpath=newString(rootpath.getBytes("8859_1"),"GB2312");
if(resFile.isDirectory()){
File[]fileList=resFile.listFiles();
for(Filefile:fileList){
zipFile(file,zipout,rootpath);
}
}else{
bytebuffer[]=newbyte[BUFF_SIZE];
BufferedInputStreamin=newBufferedInputStream(
newFileInputStream(resFile),BUFF_SIZE);
zipout.putNextEntry(newZipEntry(rootpath));
intrealLength;
while((realLength=in.read(buffer))!=-1){
zipout.write(buffer,0,realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
}