Android如何实现压缩和解压缩文件
废话不多说了,直接给大家贴java代码了,具体代码如下所示:
Java代码
packagecom.maidong.utils;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.UnsupportedEncodingException;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Enumeration;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipException;
importjava.util.zip.ZipFile;
importjava.util.zip.ZipOutputStream;
importorg.apache.http.protocol.HTTP;
publicclassZipUtils{
privatestaticfinalintBUFF_SIZE=1024*1024;//1MByte
/**
*批量压缩文件(夹)
*
*@paramresFileList
*要压缩的文件(夹)列表
*@paramzipFile
*生成的压缩文件
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticvoidzipFiles(Collection<File>resFileList,FilezipFile)throwsIOException{
ZipOutputStreamzipout=null;
try{
zipout=newZipOutputStream(newBufferedOutputStream(newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
}finally{
if(zipout!=null)
zipout.close();
}
}
/**
*批量压缩文件(夹)
*
*@paramresFileList
*要压缩的文件(夹)列表
*@paramzipFile
*生成的压缩文件
*@paramcomment
*压缩文件的注释
*@throwsIOException
*当压缩过程出错时抛出
*/
publicstaticvoidzipFiles(Collection<File>resFileList,FilezipFile,Stringcomment)throwsIOException{
ZipOutputStreamzipout=null;
try{
zipout=newZipOutputStream(newBufferedOutputStream(newFileOutputStream(zipFile),BUFF_SIZE));
for(FileresFile:resFileList){
zipFile(resFile,zipout,"");
}
zipout.setComment(comment);
}finally{
if(zipout!=null)
zipout.close();
}
}
/**
*解压缩一个文件
*
*@paramzipFile
*压缩文件
*@paramfolderPath
*解压缩的目标目录
*@throwsIOException
*当解压缩过程出错时抛出
*/
publicstaticvoidupZipFile(FilezipFile,StringfolderPath)throwsZipException,IOException{
FiledesDir=newFile(folderPath);
if(!desDir.exists()){
desDir.mkdirs();
}
ZipFilezf=newZipFile(zipFile);
InputStreamin=null;
OutputStreamout=null;
try{
for(Enumeration<?>entries=zf.entries();entries.hasMoreElements();){
ZipEntryentry=((ZipEntry)entries.nextElement());
in=zf.getInputStream(entry);
Stringstr=folderPath+File.separator+entry.getName();
str=newString(str.getBytes("8859_1"),HTTP.UTF_8);
FiledesFile=newFile(str);
if(!desFile.exists()){
FilefileParentDir=desFile.getParentFile();
if(!fileParentDir.exists()){
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
out=newFileOutputStream(desFile);
bytebuffer[]=newbyte[BUFF_SIZE];
intrealLength;
while((realLength=in.read(buffer))>0){
out.write(buffer,0,realLength);
}
}
}finally{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
}
/**
*解压文件名包含传入文字的文件
*
*@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);
InputStreamin=null;
OutputStreamout=null;
try{
for(Enumeration<?>entries=zf.entries();entries.hasMoreElements();){
ZipEntryentry=((ZipEntry)entries.nextElement());
if(entry.getName().contains(nameContains)){
in=zf.getInputStream(entry);
Stringstr=folderPath+File.separator+entry.getName();
str=newString(str.getBytes("8859_1"),HTTP.UTF_8);
//str.getBytes(AppConstans.UTF_8),"8859_1"输出
//str.getBytes("8859_1"),AppConstans.UTF_8输入
FiledesFile=newFile(str);
if(!desFile.exists()){
FilefileParentDir=desFile.getParentFile();
if(!fileParentDir.exists()){
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
out=newFileOutputStream(desFile);
bytebuffer[]=newbyte[BUFF_SIZE];
intrealLength;
while((realLength=in.read(buffer))>0){
out.write(buffer,0,realLength);
}
fileList.add(desFile);
}
}
}finally{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
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(HTTP.UTF_8),"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(HTTP.UTF_8),"8859_1");
}
/**
*取得压缩文件对象的名称
*
*@paramentry
*压缩文件对象
*@return压缩文件对象的名称
*@throwsUnsupportedEncodingException
*/
publicstaticStringgetEntryName(ZipEntryentry)throwsUnsupportedEncodingException{
returnnewString(entry.getName().getBytes(HTTP.UTF_8),"8859_1");
}
/**
*压缩文件
*
*@paramresFile
*需要压缩的文件(夹)
*@paramzipout
*压缩的目的文件
*@paramrootpath
*压缩的文件路径
*@throwsFileNotFoundException
*找不到文件时抛出
*@throwsIOException
*当压缩过程出错时抛出
*/
privatestaticvoidzipFile(FileresFile,ZipOutputStreamzipout,Stringrootpath)throwsFileNotFoundException,IOException{
rootpath=rootpath+(rootpath.trim().length()==0?"":File.separator)+resFile.getName();
rootpath=newString(rootpath.getBytes("8859_1"),HTTP.UTF_8);
BufferedInputStreamin=null;
try{
if(resFile.isDirectory()){
File[]fileList=resFile.listFiles();
for(Filefile:fileList){
zipFile(file,zipout,rootpath);
}
}else{
bytebuffer[]=newbyte[BUFF_SIZE];
in=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();
}
}finally{
if(in!=null)
in.close();
//if(zipout!=null);
//zipout.close();
}
}
}
代码到此结束,关于Android实现压缩和解压缩文件的全内容就给大家介绍这么多,希望能够帮助到大家!