在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();
}
}

上面是Android开发网总结的zip文件解压缩代码,希望你大家有用,需要注意的是参数均填写完整的路径,比如/mnt/sdcard/xxx.zip这样的类型。

更多相关文章

  1. android调试工具adb命令大全
  2. Android(安卓)开发 调用图库选择图片实现和参数详解
  3. android 自定义控件学习之三 控件布局常用知识总结
  4. Android中Uri的使用
  5. mybatisplus的坑 insert标签insert into select无参数问题的解决
  6. Pycharm安装PyQt5的详细教程
  7. NPM 和webpack 的基础使用
  8. Python技巧匿名函数、回调函数和高阶函数
  9. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程

随机推荐

  1. 在Android Studio中使用Lambda表达式
  2. Android面试系列文章2018之Android部分Co
  3. 4.Android 打包时出现的Android Export a
  4. 【动态加载】Android动态加载:简单加载模
  5. 【Android Training - 00】Google Androi
  6. IPC—进程间通信的基础概念--《Android开
  7. android中wifi属性的介绍以及连接到指定
  8. Xamarin.Android(安卓)的照相機使用
  9. android dom方式创建xml
  10. Android消息传递机制