在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面Android123给大家一个解压缩zip的java代码,可以在Android上任何版本中使用,Unzip这个静态方法比较简单,参数一为源zip文件的完整路径,参数二为解压缩后存放的文件夹。

private static void Unzip(String zipFile, String targetDir) {
int BUFFER = 4096; //这里缓冲区我们使用4KB,
String strEntry; //保存每个zip的条目名称

try {
BufferedOutputStream dest = null; //缓冲输出流
FileInputStream fis = new FileInputStream(zipFile);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry; //每个zip条目的实例

while ((entry = zis.getNextEntry()) != null) {

try {
Log.i("Unzip: ","="+ entry);
int count;
byte data[] = new byte[BUFFER];
strEntry = entry.getName();

File entryFile = new File(targetDir + strEntry);
File entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}

FileOutputStream fos = new FileOutputStream(entryFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
zis.close();
} catch (Exception cwj) {
cwj.printStackTrace();
}
}

上面是zip文件解压缩代码,希望你大家有用,需要注意的是参数均填写完整的路径,比如/mnt/sdcard/xxx.zip这样的类型。
出处:http://www.android123.com.cn/androidkaifa/822.html

更多相关文章

  1. android 中一个项目工程引用另一个项目工程和jar
  2. Android(安卓)layout之优化:使用include和merge 标签
  3. Android(安卓)layout 使用include和merge 标签
  4. 用Android(安卓)Studio 运行ndk 程序
  5. mybatisplus的坑 insert标签insert into select无参数问题的解决
  6. Pycharm安装PyQt5的详细教程
  7. NPM 和webpack 的基础使用
  8. Python技巧匿名函数、回调函数和高阶函数
  9. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程

随机推荐

  1. Android 后台每10秒钟启动一次应用的demo
  2. Android的支持库 && app/apk包进系统 &&
  3. 如何在Android应用里对HTTP请求头部添加
  4. 解决android调用系统相机拍照保存时onAct
  5. 路径提供者文档目录是一个安全的位置吗?
  6. 如何在Android 7.0+中检索SD卡的序列号?
  7. Android多线程下载远程图片【转】
  8. 转:Android Studio Error:Connection time
  9. MD5加密,java工具类 String 转变成MD5 St
  10. 没有包lib32z1,lib32ncurses5,lib32stdc+