当前位置: 移动技术网 > IT编程>移动开发>Android > Android如何实现压缩和解压缩文件

Android如何实现压缩和解压缩文件

2019年07月24日  | 移动技术网IT编程  | 我要评论

废话不多说了,直接给大家贴java代码了,具体代码如下所示:

java代码

package com.maidong.utils; 
import java.io.bufferedinputstream; 
import java.io.bufferedoutputstream; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.filenotfoundexception; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.io.unsupportedencodingexception; 
import java.util.arraylist; 
import java.util.collection; 
import java.util.enumeration; 
import java.util.zip.zipentry; 
import java.util.zip.zipexception; 
import java.util.zip.zipfile; 
import java.util.zip.zipoutputstream; 
import org.apache.http.protocol.http; 
public class ziputils { 
private static final int buff_size = 1024 * 1024; // 1m byte 
/**
* 批量压缩文件(夹)
* 
* @param resfilelist
* 要压缩的文件(夹)列表
* @param zipfile
* 生成的压缩文件
* @throws ioexception
* 当压缩过程出错时抛出
*/ 
public static void zipfiles(collection<file> resfilelist, file zipfile) throws ioexception { 
zipoutputstream zipout = null; 
try { 
zipout = new zipoutputstream(new bufferedoutputstream(new fileoutputstream(zipfile), buff_size)); 
for (file resfile : resfilelist) { 
zipfile(resfile, zipout, ""); 
} 
} finally { 
if (zipout != null) 
zipout.close(); 
} 
} 
/**
* 批量压缩文件(夹)
* 
* @param resfilelist
* 要压缩的文件(夹)列表
* @param zipfile
* 生成的压缩文件
* @param comment
* 压缩文件的注释
* @throws ioexception
* 当压缩过程出错时抛出
*/ 
public static void zipfiles(collection<file> resfilelist, file zipfile, string comment) throws ioexception { 
zipoutputstream zipout = null; 
try { 
zipout = new zipoutputstream(new bufferedoutputstream(new fileoutputstream(zipfile), buff_size)); 
for (file resfile : resfilelist) { 
zipfile(resfile, zipout, ""); 
} 
zipout.setcomment(comment); 
} finally { 
if (zipout != null) 
zipout.close(); 
} 
} 
/**
* 解压缩一个文件
* 
* @param zipfile
* 压缩文件
* @param folderpath
* 解压缩的目标目录
* @throws ioexception
* 当解压缩过程出错时抛出
*/ 
public static void upzipfile(file zipfile, string folderpath) throws zipexception, ioexception { 
file desdir = new file(folderpath); 
if (!desdir.exists()) { 
desdir.mkdirs(); 
} 
zipfile zf = new zipfile(zipfile); 
inputstream in = null; 
outputstream out = null; 
try { 
for (enumeration<?> entries = zf.entries(); entries.hasmoreelements();) { 
zipentry entry = ((zipentry) entries.nextelement()); 
in = zf.getinputstream(entry); 
string str = folderpath + file.separator + entry.getname(); 
str = new string(str.getbytes("8859_1"), http.utf_8); 
file desfile = new file(str); 
if (!desfile.exists()) { 
file fileparentdir = desfile.getparentfile(); 
if (!fileparentdir.exists()) { 
fileparentdir.mkdirs(); 
} 
desfile.createnewfile(); 
} 
out = new fileoutputstream(desfile); 
byte buffer[] = new byte[buff_size]; 
int reallength; 
while ((reallength = in.read(buffer)) > 0) { 
out.write(buffer, 0, reallength); 
} 
} 
} finally { 
if (in != null) 
in.close(); 
if (out != null) 
out.close(); 
} 
} 
/**
* 解压文件名包含传入文字的文件
* 
* @param zipfile
* 压缩文件
* @param folderpath
* 目标文件夹
* @param namecontains
* 传入的文件匹配名
* @throws zipexception
* 压缩格式有误时抛出
* @throws ioexception
* io错误时抛出
*/ 
public static arraylist<file> upzipselectedfile(file zipfile, string folderpath, string namecontains) throws zipexception, 
ioexception { 
arraylist<file> filelist = new arraylist<file>(); 
file desdir = new file(folderpath); 
if (!desdir.exists()) { 
desdir.mkdir(); 
} 
zipfile zf = new zipfile(zipfile); 
inputstream in = null; 
outputstream out = null; 
try { 
for (enumeration<?> entries = zf.entries(); entries.hasmoreelements();) { 
zipentry entry = ((zipentry) entries.nextelement()); 
if (entry.getname().contains(namecontains)) { 
in = zf.getinputstream(entry); 
string str = folderpath + file.separator + entry.getname(); 
str = new string(str.getbytes("8859_1"), http.utf_8); 
// str.getbytes(appconstans.utf_8),"8859_1" 输出 
// str.getbytes("8859_1"),appconstans.utf_8 输入 
file desfile = new file(str); 
if (!desfile.exists()) { 
file fileparentdir = desfile.getparentfile(); 
if (!fileparentdir.exists()) { 
fileparentdir.mkdirs(); 
} 
desfile.createnewfile(); 
} 
out = new fileoutputstream(desfile); 
byte buffer[] = new byte[buff_size]; 
int reallength; 
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(); 
} 
return filelist; 
} 
/**
* 获得压缩文件内文件列表
* 
* @param zipfile
* 压缩文件
* @return 压缩文件内文件名称
* @throws zipexception
* 压缩文件格式有误时抛出
* @throws ioexception
* 当解压缩过程出错时抛出
*/ 
public static arraylist<string> getentriesnames(file zipfile) throws zipexception, ioexception { 
arraylist<string> entrynames = new arraylist<string>(); 
enumeration<?> entries = getentriesenumeration(zipfile); 
while (entries.hasmoreelements()) { 
zipentry entry = ((zipentry) entries.nextelement()); 
entrynames.add(new string(getentryname(entry).getbytes(http.utf_8), "8859_1")); 
} 
return entrynames; 
} 
/**
* 获得压缩文件内压缩文件对象以取得其属性
* 
* @param zipfile
* 压缩文件
* @return 返回一个压缩文件列表
* @throws zipexception
* 压缩文件格式有误时抛出
* @throws ioexception
* io操作有误时抛出
*/ 
public static enumeration<?> getentriesenumeration(file zipfile) throws zipexception, ioexception { 
zipfile zf = new zipfile(zipfile); 
return zf.entries(); 
} 
/**
* 取得压缩文件对象的注释
* 
* @param entry
* 压缩文件对象
* @return 压缩文件对象的注释
* @throws unsupportedencodingexception
*/ 
public static string getentrycomment(zipentry entry) throws unsupportedencodingexception { 
return new string(entry.getcomment().getbytes(http.utf_8), "8859_1"); 
} 
/**
* 取得压缩文件对象的名称
* 
* @param entry
* 压缩文件对象
* @return 压缩文件对象的名称
* @throws unsupportedencodingexception
*/ 
public static string getentryname(zipentry entry) throws unsupportedencodingexception { 
return new string(entry.getname().getbytes(http.utf_8), "8859_1"); 
} 
/**
* 压缩文件
* 
* @param resfile
* 需要压缩的文件(夹)
* @param zipout
* 压缩的目的文件
* @param rootpath
* 压缩的文件路径
* @throws filenotfoundexception
* 找不到文件时抛出
* @throws ioexception
* 当压缩过程出错时抛出
*/ 
private static void zipfile(file resfile, zipoutputstream zipout, string rootpath) throws filenotfoundexception, ioexception { 
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : file.separator) + resfile.getname(); 
rootpath = new string(rootpath.getbytes("8859_1"), http.utf_8); 
bufferedinputstream in = null; 
try { 
if (resfile.isdirectory()) { 
file[] filelist = resfile.listfiles(); 
for (file file : filelist) { 
zipfile(file, zipout, rootpath); 
} 
} else { 
byte buffer[] = new byte[buff_size]; 
in = new bufferedinputstream(new fileinputstream(resfile), buff_size); 
zipout.putnextentry(new zipentry(rootpath)); 
int reallength; 
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实现压缩和解压缩文件的全内容就给大家介绍这么多,希望能够帮助到大家!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网