Android实现zip文件压缩及解压缩的方法
本文实例讲述了Android实现zip文件压缩及解压缩的方法。分享给大家供大家参考。具体如下:
DirTraversal.java如下:
packagecom.once;
importjava.io.File;
importjava.util.ArrayList;
importjava.util.LinkedList;
/**
*文件夹遍历
*@authoronce
*
*/
publicclassDirTraversal{
//norecursion
publicstaticLinkedList<File>listLinkedFiles(StringstrPath){
LinkedList<File>list=newLinkedList<File>();
Filedir=newFile(strPath);
Filefile[]=dir.listFiles();
for(inti=0;i<file.length;i++){
if(file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
Filetmp;
while(!list.isEmpty()){
tmp=(File)list.removeFirst();
if(tmp.isDirectory()){
file=tmp.listFiles();
if(file==null)
continue;
for(inti=0;i<file.length;i++){
if(file[i].isDirectory())
list.add(file[i]);
else
System.out.println(file[i].getAbsolutePath());
}
}else{
System.out.println(tmp.getAbsolutePath());
}
}
returnlist;
}
//recursion
publicstaticArrayList<File>listFiles(StringstrPath){
returnrefreshFileList(strPath);
}
publicstaticArrayList<File>refreshFileList(StringstrPath){
ArrayList<File>filelist=newArrayList<File>();
Filedir=newFile(strPath);
File[]files=dir.listFiles();
if(files==null)
returnnull;
for(inti=0;i<files.length;i++){
if(files[i].isDirectory()){
refreshFileList(files[i].getAbsolutePath());
}else{
if(files[i].getName().toLowerCase().endsWith("zip"))
filelist.add(files[i]);
}
}
returnfilelist;
}
}
ZipUtils.java如下:
packagecom.once;
importjava.io.*;
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;
/**
*Javautils实现的Zip工具
*
*@authoronce
*/
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生成的压缩文件
*@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();
}
}
/**
*解压文件名包含传入文字的文件
*
*@paramzipFile压缩文件
*@paramfolderPath目标文件夹
*@paramnameContains传入的文件匹配名
*@throwsZipException压缩格式有误时抛出
*@throwsIOExceptionIO错误时抛出
*/
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压缩文件格式有误时抛出
*@throwsIOExceptionIO操作有误时抛出
*/
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当压缩过程出错时抛出
*/
privatestaticvoidzipFile(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();
}
}
}
希望本文所述对大家的Android程序设计有所帮助。