Android解压ZIP文件

Android 解压 ZIP 文件 —— 由 吧主 分享

AndroidManifest.xml里添加权限:

 <uses-permissionandroid:name="Android.permission.WRITE_EXTERNAL_STORAGE"/> 
 <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

工具类:

[html] view plain copy
  1. public class ZIP {  
  2.     public ZIP(){  
  3.           
  4.     }  
  5.       
  6.      /**   
  7.      * DeCompress the ZIP to the path   
  8.      * @param zipFileString  name of ZIP   
  9.      * @param outPathString   path to be unZIP  
  10.      * @throws Exception   
  11.      */    
  12.     public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {    
  13.         ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));    
  14.         ZipEntry zipEntry;    
  15.         String szName = "";    
  16.         while ((zipEntry = inZip.getNextEntry()) != null) {    
  17.             szName = zipEntry.getName();    
  18.             if (zipEntry.isDirectory()) {    
  19.                 // get the folder name of the widget    
  20.                 szName = szName.substring(0, szName.length() - 1);    
  21.                 File folder = new File(outPathString + File.separator + szName);    
  22.                 folder.mkdirs();    
  23.             } else {    
  24.             
  25.                 File file = new File(outPathString + File.separator + szName);    
  26.                 file.createNewFile();    
  27.                 // get the output stream of the file    
  28.                 FileOutputStream out = new FileOutputStream(file);    
  29.                 int len;    
  30.                 byte[] buffer = new byte[1024];    
  31.                 // read (len) bytes into buffer    
  32.                 while ((len = inZip.read(buffer)) != -1) {    
  33.                     // write (len) byte from buffer at the position 0    
  34.                     out.write(buffer, 0, len);    
  35.                     out.flush();    
  36.                 }    
  37.                 out.close();    
  38.             }    
  39.         }   
  40.         inZip.close();    
  41.     }  
  42.         
  43.     /**   
  44.      * Compress file and folder  
  45.      * @param srcFileString   file or folder to be Compress  
  46.      * @param zipFileString   the path name of result ZIP  
  47.      * @throws Exception   
  48.      */    
  49.     public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {    
  50.         //create ZIP   
  51.         ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));    
  52.         //create the file   
  53.         File file = new File(srcFileString);    
  54.         //compress  
  55.         ZipFiles(file.getParent()+File.separator, file.getName(), outZip);    
  56.         //finish and close  
  57.         outZip.finish();    
  58.         outZip.close();    
  59.     }  
  60.       
  61.     /**   
  62.      * compress files  
  63.      * @param folderString   
  64.      * @param fileString   
  65.      * @param zipOutputSteam   
  66.      * @throws Exception   
  67.      */    
  68.     private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{    
  69.         if(zipOutputSteam == null)    
  70.         return;    
  71.         File file = new File(folderString+fileString);    
  72.         if (file.isFile()) {    
  73.             ZipEntry zipEntry =  new ZipEntry(fileString);    
  74.             FileInputStream inputStream = new FileInputStream(file);    
  75.             zipOutputSteam.putNextEntry(zipEntry);    
  76.             int len;    
  77.             byte[] buffer = new byte[4096];     
  78.             while((len=inputStream.read(buffer)) != -1)    
  79.             {    
  80.                 zipOutputSteam.write(buffer, 0, len);    
  81.             }    
  82.             zipOutputSteam.closeEntry();    
  83.         }    
  84.         else {    
  85.             //folder  
  86.             String fileList[] = file.list();    
  87.             //no child file and compress    
  88.             if (fileList.length <= 0) {    
  89.                 ZipEntry zipEntry =  new ZipEntry(fileString+File.separator);    
  90.                 zipOutputSteam.putNextEntry(zipEntry);    
  91.                 zipOutputSteam.closeEntry();                    
  92.             }    
  93.             //child files and recursion    
  94.             for (int i = 0; i < fileList.length; i++) {    
  95.                 ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);    
  96.             }//end of for    
  97.         }      
  98.     }  
  99.       
  100.     /**   
  101.      * return the InputStream of file in the ZIP  
  102.      * @param zipFileString  name of ZIP   
  103.      * @param fileString     name of file in the ZIP   
  104.      * @return InputStream   
  105.      * @throws Exception   
  106.      */    
  107.     public static InputStream UpZip(String zipFileString, String fileString)throws Exception {    
  108.         ZipFile zipFile = new ZipFile(zipFileString);    
  109.         ZipEntry zipEntry = zipFile.getEntry(fileString);    
  110.         return zipFile.getInputStream(zipEntry);    
  111.     }    
  112.       
  113.     /**   
  114.      * return files list(file and folder) in the ZIP  
  115.      * @param zipFileString     ZIP name  
  116.      * @param bContainFolder    contain folder or not  
  117.      * @param bContainFile      contain file or not  
  118.      * @return   
  119.      * @throws Exception   
  120.      */    
  121.     public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {    
  122.         List<File> fileList = new ArrayList<File>();    
  123.         ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));    
  124.         ZipEntry zipEntry;    
  125.         String szName = "";    
  126.         while ((zipEntry = inZip.getNextEntry()) != null) {    
  127.             szName = zipEntry.getName();    
  128.             if (zipEntry.isDirectory()) {    
  129.                 // get the folder name of the widget    
  130.                 szName = szName.substring(0, szName.length() - 1);    
  131.                 File folder = new File(szName);    
  132.                 if (bContainFolder) {    
  133.                     fileList.add(folder);    
  134.                 }    
  135.             
  136.             } else {    
  137.                 File file = new File(szName);    
  138.                 if (bContainFile) {    
  139.                     fileList.add(file);    
  140.                 }    
  141.             }    
  142.         }  
  143.         inZip.close();    
  144.         return fileList;    
  145.     }    
  146. }  

推荐文章

1、Android面试经验大解密

2、Android的viewHolder模式解剖

3、Android中必须学习的八大开源项目(开发项目必看)

4、如何自学Android, 教大家玩爆Android(成为大神必看)

5、2016 Google hosts 持续更新【更新 于:2016-08-27】(免费翻墙必备)

6、Android面试经验总结(面试成功必备)

7、Android Studio 个性化设置(装逼必备)

8、Android Studio 2.2 正式起航(玩爆Android Studio 2.2必备)

9、Android Studio 2.3 正式起航(玩爆Android Studio 2.3必备)

Android Studio 2.2 新功能实例代码:

Android Studio 2.2新功能实例源码(玩爆Android Studio 2.2必备)

Android Studio 2.2新功能介绍:

What's new in Android development tools - Google I/O 2016(YouTube视频需要自备梯子)

【GitHub】https://github.com/xiaole0310

【****博客】http://blog.****.net/xiaole0313

【新浪微博】http://weibo.com/xiaole0313

【知乎】http://www.zhihu.com/people/yang-shou-le

【简书】http://www.jianshu.com/users/1a47e8afa34a

【技术群】279126311 [满]

【技术群】484572225 [未]

【Email】[email protected]

Android Studio 2.2 新功能实例代码:

Android Studio 2.2新功能实例源码(玩爆Android Studio 2.2必备)

如果你有好的文章想和大家分享,欢迎投稿,直接向我投递文章链接即可。

欢迎扫描关注我们的微信公众号(ysle_0313),不要错过每一篇干货~

Android解压ZIP文件

一键关注我们微信公众号ysle_0313