Android Zip文件解压缩代码

时间:2023-03-08 23:20:56
Android Zip文件解压缩代码

2011-04-01 17:58:52|  分类: Android |举报 |字号 订阅

在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这样的类型。