Android解压ZIP文件

78 篇文章 0 订阅
57 篇文章 0 订阅
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

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

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

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

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

【技术群】279126311 [满]

【技术群】484572225 [未]

【Email】ysle0313@gmail.com

Android Studio 2.2 新功能实例代码:

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

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

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


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


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 平台上解压 ZIP 文件,你可以使用以下代码示例: ```java import android.util.Log; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipUtils { private static final int BUFFER_SIZE = 4096; public static void unzip(String zipFilePath, String destDirectory) { try { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } catch (Exception e) { Log.e("ZipUtils", "Error while unzipping file: " + e.getMessage()); } } private static void extractFile(ZipInputStream zipIn, String filePath) { try { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } catch (Exception e) { Log.e("ZipUtils", "Error while extracting file: " + e.getMessage()); } } } ``` 使用上述代码,你只需调用 `unzip` 方法,传入 ZIP 文件的路径和解压目标目录的路径即可。注意确保你的应用已经获取了文件读写权限。 示例用法: ```java String zipFilePath = "/path/to/your/zip/file.zip"; String destDirectory = "/path/to/your/destination/directory"; ZipUtils.unzip(zipFilePath, destDirectory); ``` 将上述路径替换为你实际的文件路径和目标目录路径。 希望对你有所帮助!如有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值