Android Zip压缩解压缩

来源:互联网 发布:大肚照艺术照软件 编辑:程序博客网 时间:2024/06/04 18:33

研究了一下Android上Zip的用法,写了个类把常用的几种方法写了出来

 

 

公共方法列表static List<File>

GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)

 

取得压缩包中的文件列表(文件夹,文件自选)

zipFileString      : 压缩包文件

bContainFolder : 是否包括文件夹

bContainFile      : 是否包括文件

static InputStream

UpZip(String zipFileString, String fileString)

 

返回压缩包中的文件InputStream

zipFileString : 压缩包文件

fileString      : 要解压的文件名

static void

UnZipFolder(String zipFileString, String outPathString)

 

解压一个压缩文档 到指定位置

zipFileString    : 压缩包文件

outPathString : 要输出的路径

static void

ZipFolder(String srcFileString, String zipFileString)

 

压缩文件,文件夹

srcFileString : 要压缩的文件,文件夹的路径

zipFileString : 输出压缩包的路径

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Java代码  收藏代码
  1. /** 
  2.  * Android Zip压缩解压缩 
  3.  * @author Ren.xia 
  4.  * @version 1.0 
  5.  * @updated 26-七月-2010 13:04:27 
  6.  */  
  7. public class XZip {  
  8.   
  9.     public XZip(){  
  10.   
  11.     }  
  12.       
  13.     /** 
  14.      * 取得压缩包中的 文件列表(文件夹,文件自选) 
  15.      * @param zipFileString     压缩包名字 
  16.      * @param bContainFolder    是否包括 文件夹 
  17.      * @param bContainFile      是否包括 文件 
  18.      * @return 
  19.      * @throws Exception 
  20.      */  
  21.     public static java.util.List<java.io.File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {  
  22.           
  23.         android.util.Log.v("XZip""GetFileList(String)");  
  24.           
  25.         java.util.List<java.io.File> fileList = new java.util.ArrayList<java.io.File>();  
  26.         java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));  
  27.         java.util.zip.ZipEntry zipEntry;  
  28.         String szName = "";  
  29.           
  30.         while ((zipEntry = inZip.getNextEntry()) != null) {  
  31.             szName = zipEntry.getName();  
  32.           
  33.             if (zipEntry.isDirectory()) {  
  34.           
  35.                 // get the folder name of the widget  
  36.                 szName = szName.substring(0, szName.length() - 1);  
  37.                 java.io.File folder = new java.io.File(szName);  
  38.                 if (bContainFolder) {  
  39.                     fileList.add(folder);  
  40.                 }  
  41.           
  42.             } else {  
  43.                 java.io.File file = new java.io.File(szName);  
  44.                 if (bContainFile) {  
  45.                     fileList.add(file);  
  46.                 }  
  47.             }  
  48.         }//end of while  
  49.           
  50.         inZip.close();  
  51.           
  52.         return fileList;  
  53.     }  
  54.   
  55.     /** 
  56.      * 返回压缩包中的文件InputStream 
  57.      * @param zipFileString     压缩文件的名字 
  58.      * @param fileString    解压文件的名字 
  59.      * @return InputStream 
  60.      * @throws Exception 
  61.      */  
  62.     public static java.io.InputStream UpZip(String zipFileString, String fileString)throws Exception {  
  63.         android.util.Log.v("XZip""UpZip(String, String)");  
  64.         java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(zipFileString);  
  65.         java.util.zip.ZipEntry zipEntry = zipFile.getEntry(fileString);  
  66.           
  67.         return zipFile.getInputStream(zipEntry);  
  68.   
  69.     }  
  70.       
  71.       
  72.     /** 
  73.      * 解压一个压缩文档 到指定位置 
  74.      * @param zipFileString 压缩包的名字 
  75.      * @param outPathString 指定的路径 
  76.      * @throws Exception 
  77.      */  
  78.     public static void UnZipFolder(String zipFileString, String outPathString)throws Exception {  
  79.         android.util.Log.v("XZip""UnZipFolder(String, String)");  
  80.         java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));  
  81.         java.util.zip.ZipEntry zipEntry;  
  82.         String szName = "";  
  83.           
  84.         while ((zipEntry = inZip.getNextEntry()) != null) {  
  85.             szName = zipEntry.getName();  
  86.           
  87.             if (zipEntry.isDirectory()) {  
  88.           
  89.                 // get the folder name of the widget  
  90.                 szName = szName.substring(0, szName.length() - 1);  
  91.                 java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);  
  92.                 folder.mkdirs();  
  93.           
  94.             } else {  
  95.           
  96.                 java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);  
  97.                 file.createNewFile();  
  98.                 // get the output stream of the file  
  99.                 java.io.FileOutputStream out = new java.io.FileOutputStream(file);  
  100.                 int len;  
  101.                 byte[] buffer = new byte[1024];  
  102.                 // read (len) bytes into buffer  
  103.                 while ((len = inZip.read(buffer)) != -1) {  
  104.                     // write (len) byte from buffer at the position 0  
  105.                     out.write(buffer, 0, len);  
  106.                     out.flush();  
  107.                 }  
  108.                 out.close();  
  109.             }  
  110.         }//end of while  
  111.           
  112.         inZip.close();  
  113.       
  114.     }//end of func  
  115.       
  116.   
  117.     /** 
  118.      * 压缩文件,文件夹 
  119.      * @param srcFileString 要压缩的文件/文件夹名字 
  120.      * @param zipFileString 指定压缩的目的和名字 
  121.      * @throws Exception 
  122.      */  
  123.     public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {  
  124.         android.util.Log.v("XZip""ZipFolder(String, String)");  
  125.           
  126.         //创建Zip包  
  127.         java.util.zip.ZipOutputStream outZip = new java.util.zip.ZipOutputStream(new java.io.FileOutputStream(zipFileString));  
  128.           
  129.         //打开要输出的文件  
  130.         java.io.File file = new java.io.File(srcFileString);  
  131.   
  132.         //压缩  
  133.         ZipFiles(file.getParent()+java.io.File.separator, file.getName(), outZip);  
  134.           
  135.         //完成,关闭  
  136.         outZip.finish();  
  137.         outZip.close();  
  138.       
  139.     }//end of func  
  140.       
  141.     /** 
  142.      * 压缩文件 
  143.      * @param folderString 
  144.      * @param fileString 
  145.      * @param zipOutputSteam 
  146.      * @throws Exception 
  147.      */  
  148.     private static void ZipFiles(String folderString, String fileString, java.util.zip.ZipOutputStream zipOutputSteam)throws Exception{  
  149.         android.util.Log.v("XZip""ZipFiles(String, String, ZipOutputStream)");  
  150.           
  151.         if(zipOutputSteam == null)  
  152.             return;  
  153.           
  154.         java.io.File file = new java.io.File(folderString+fileString);  
  155.           
  156.         //判断是不是文件  
  157.         if (file.isFile()) {  
  158.   
  159.             java.util.zip.ZipEntry zipEntry =  new java.util.zip.ZipEntry(fileString);  
  160.             java.io.FileInputStream inputStream = new java.io.FileInputStream(file);  
  161.             zipOutputSteam.putNextEntry(zipEntry);  
  162.               
  163.             int len;  
  164.             byte[] buffer = new byte[4096];  
  165.               
  166.             while((len=inputStream.read(buffer)) != -1)  
  167.             {  
  168.                 zipOutputSteam.write(buffer, 0, len);  
  169.             }  
  170.               
  171.             zipOutputSteam.closeEntry();  
  172.         }  
  173.         else {  
  174.               
  175.             //文件夹的方式,获取文件夹下的子文件  
  176.             String fileList[] = file.list();  
  177.               
  178.             //如果没有子文件, 则添加进去即可  
  179.             if (fileList.length <= 0) {  
  180.                 java.util.zip.ZipEntry zipEntry =  new java.util.zip.ZipEntry(fileString+java.io.File.separator);  
  181.                 zipOutputSteam.putNextEntry(zipEntry);  
  182.                 zipOutputSteam.closeEntry();                  
  183.             }  
  184.               
  185.             //如果有子文件, 遍历子文件  
  186.             for (int i = 0; i < fileList.length; i++) {  
  187.                 ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);  
  188.             }//end of for  
  189.       
  190.         }//end of if  
  191.           
  192.     }//end of func  
  193.       
  194.     public void finalize() throws Throwable {  
  195.           
  196.     }  
  197.   
  198. }  
原创粉丝点击