Android中压缩文件学习-1

这里用到的包是java.util. 不支持中文路径 (文件名为乱码,但内容不受影响,在JDK7中已经解决了中文路径为乱码,我参考了各位大神的资料: http://blog.csdn.net/zhanghw0917/article/details/5628039);

使用ApacheZipUtils org.apache.tools.zip.ZipOutputStrea 也可以解决乱码


private static void zipFile(File resFile, ZipOutputStream zipout,
String rootpath) throws FileNotFoundException, IOException {
/**
* 1. 创建一个JarOutputStream或ZipOutputStream对象,分别用来生成jar或zip文件。

* ZipOutputStream zipout = new ZipOutputStream(new
* BufferedOutputStream( new FileOutputStream(zipFile), BUFF_SIZE));
*/


/**
* 2.为每一个要压缩的文件创建一个JarEntry或ZipEntry对象。每一个(jar|zip)Entry对象代表一个被压缩的文件,并通过
* (jar|zip)Entry对象指定被压缩文件在压缩包中的文件名和路径。

* rootpath = resFile.getName();
*/
rootpath = rootpath
+ (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
if (resFile.isDirectory()) {
// 子目录
File[] fileList = resFile.listFiles();
for (File file : fileList) {
// 迭代
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(resFile), BUFF_SIZE);
/**
* 3、调用(jar|zip)OutputStream.putNextEntry方法设置当前打开的(jar|zip)Entry对象。

*/
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
/**
* 4、向(jar|zip)OutputStream对象写入数据。

*/
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
/**
* 5、调用(jar|zip)OutputStream.closeEntry方法关闭当前打开的(jar|zip)Entry对象。
* 如果还有待压缩的文件,回到第二步
*/
in.close();
zipout.flush();
zipout.closeEntry();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值