码迷,mamicode.com
首页 > 移动开发 > 详细

Android 解压zip包的实现

时间:2014-08-23 15:23:31      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   os   io   文件   ar   div   

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     }

 

Android 解压zip包的实现

标签:android   style   blog   color   os   io   文件   ar   div   

原文地址:http://www.cnblogs.com/zhaimi/p/3931120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!