Android Zip文件解压缩代码

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

01 private static void Unzip(String zipFile, String targetDir) {
02   int BUFFER = 4096; //这里缓冲区咱们使用4KB,
03   String strEntry; //保存每一个zip的条目名称
04   try {
05   BufferedOutputStream dest = null; //缓冲输出流
06   FileInputStream fis = new FileInputStream(zipFile);
07   ZipInputStream zis = new ZipInputStream(newBufferedInputStream(fis));
08   ZipEntry entry; //每一个zip条目的实例
09   while ((entry = zis.getNextEntry()) != null) {
10   try {
11   Log.i("Unzip: ""="+ entry);
12   int count;
13   byte data[] = new byte[BUFFER];
14   strEntry = entry.getName();
15   File entryFile = new File(targetDir + strEntry);
16   File entryDir = new File(entryFile.getParent());
17   if (!entryDir.exists()) {
18   entryDir.mkdirs();
19   }
20   FileOutputStream fos = new FileOutputStream(entryFile);
21   dest = new BufferedOutputStream(fos, BUFFER);
22   while ((count = zis.read(data, 0, BUFFER)) != -1) {
23   dest.write(data, 0, count);
24   }
25   dest.flush();
26   dest.close();
27   } catch (Exception ex) {
28   ex.printStackTrace();
29   }
30   }
31   zis.close();
32   } catch (Exception cwj) {
33   cwj.printStackTrace();
34   }
35   }


         上面是总结的zip文件解压缩代码,但愿你你们有用,须要注意的是参数均填写完整的路径,好比/mnt/sdcard/xxx.zip这样的类型。
相关文章
相关标签/搜索