Android 解压zip包的实现

android 自身提供了zip包的解压接口

 1  /**
 2      * 解压操作
 3      *
 4      * @param zipFileString 被解压的文件路径 sdcard/0/a/b/test.zip
 5      * @param outPathString 解压的目的路径 sdcard/0/a/b
 6      */
 7     public static void UnZipFolder(String zipFileString, String outPathString) {
 8         FileInputStream fis = null; //文件输入流
 9         ZipInputStream inZip = null; // android提供的zip包输入流
10         try {
11             fis = new FileInputStream(zipFileString); //先读取文件,
12             inZip = new ZipInputStream(fis);//将文件流变成zip输入流
13             ZipEntry zipEntry; //zip实体
14             String szName = "";
15             while ((zipEntry = inZip.getNextEntry()) != null) { //while(true)循环解析
16                 szName = zipEntry.getName();
17                 if (zipEntry.isDirectory()) {// 如果是文件夹
18                     szName = szName.substring(0, szName.length() - 1);
19                     File folder = new File(outPathString + File.separator + szName);
20                     folder.mkdirs();
21                 } else {//如果是文件
22                     File file = new File(outPathString + File.separator + szName);
23                     file.createNewFile();
24                     FileOutputStream out = new FileOutputStream(file);
25                     int length;
26                     byte[] buffer = new byte[1024];
27                     while ((length = inZip.read(buffer)) != -1) {
28                         out.write(buffer, 0, length);
29                         out.flush();
30                     }
31                     if (out != null) {
32                         out.close();
33                     }
34                 }
35             }
36         } catch (FileNotFoundException e) {
37             e.printStackTrace();
38         } catch (IOException e) {42             e.printStackTrace();
43         } catch (Exception e) {47             e.printStackTrace();
48         } finally {
49             if (fis != null) {
50                 try {
51                     fis.close();
52                 } catch (IOException e) {
53                     e.printStackTrace();
54                 }
55             }
56             if (inZip != null) {
57                 try {
58                     inZip.close();
59                 } catch (IOException e) {
60                     e.printStackTrace();
61                 }
62             }
63         }
64 
65     }

 

posted @ 2014-08-23 15:02  zhaimi要长肉  阅读(1091)  评论(0编辑  收藏  举报