publicclassZipUtils{
privatestaticfinalintBUFF_SIZE=1024*1024;//1MByte
/**
*批量壓縮文件(夾)
*
*@paramresFileList
*要壓縮的文件(夾)列表
*@paramzipFile
*生成的壓縮文件
*@throwsIOException
*當壓縮過程出錯時拋出
*/
publicstaticvoidzipFiles(Collection<File>resFileList,FilezipFile)
throwsIOException{
ZipOutputStreamzipout=newZipOutputStream(newBufferedOutputStream(
newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
zipout.close();
}
/**
*批量壓縮文件(夾)
*
*@paramresFileList
*要壓縮的文件(夾)列表
*@paramzipFile
*生成的壓縮文件
*@throwsIOException
*當壓縮過程出錯時拋出
*/
publicstaticbooleanzipPhotoFiles(Collection<File>resFileList,
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<File>resFileList,FilezipFile,
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<File>upZipSelectedFile(FilezipFile,
StringfolderPath,StringnameContains)throwsZipException,
IOException{
ArrayList<File>fileList=newArrayList<File>();
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<String>getEntriesNames(FilezipFile)
throwsZipException,IOException{
ArrayList<String>entryNames=newArrayList<String>();
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();
}
}
}