现在的位置: 首页 > 综合 > 正文

Android平台中实现Zip文件的解压缩功能

2014年01月04日 ⁄ 综合 ⁄ 共 1265字 ⁄ 字号 评论关闭

在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

抱歉!评论已关闭.