打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android中读取zip文件
  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. }  

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C#生成ZIP压缩包
.net 压缩文件夹
C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件
Asp.net中文件的压缩与解压
Java读写Zip文件
如何将文件打成zip包并下载
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服