1. package com.tw.file.util;  
  2.  
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.util.ArrayList;  
  10. import java.util.Collection;  
  11. import java.util.zip.ZipEntry;  
  12. import java.util.zip.ZipOutputStream;  
  13.  
  14. /**  
  15.  * <p>实现文件压缩</p>  
  16.  * @author tangw 2010-12-27  
  17.  *  
  18.  */ 
  19. public class ZipManager {  
  20.     private static ZipManager instance;  
  21.     private static final int BUFF_SIZE = 1024 * 1024;     //1M Byte   
  22.       
  23.     /**  
  24.      * get ZipManager  
  25.      * @return ZipManager  
  26.      */ 
  27.     public synchronized static ZipManager getInstance(){  
  28.         if( instance == null ){  
  29.             instance = new ZipManager();  
  30.         }  
  31.         return instance;  
  32.     }  
  33.       
  34.     /**  
  35.      * <p>批量或单个压缩文件</p>  
  36.      * @author tangw 2010-12-27  
  37.      * @param resFileList 源文件列表  
  38.      * @param zipFile     压缩文件  
  39.      * @throws IOException  
  40.      */ 
  41.     public static void batchZipFiles(Collection<File> resFileList, File zipFile) throws IOException {  
  42.         //创建压缩文件  
  43.         ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));  
  44.         for (File resFile : resFileList) {   
  45.              byte buffer[] = new byte[BUFF_SIZE];  
  46.              BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);   
  47.              //新增zip输出流  
  48.              zipout.putNextEntry(new ZipEntry(resFile.getName()));   
  49.              int realLength;  
  50.              //写入  
  51.              while ((realLength = in.read(buffer)) != -1) {   
  52.                      zipout.write(buffer, 0, realLength);   
  53.              }   
  54.              in.close();   
  55.              zipout.flush();   
  56.              zipout.closeEntry();    
  57.         }   
  58.         zipout.close();   
  59.     }  
  60.       
  61.     /**  
  62.      * <p>测试方法</p>  
  63.      * @author tangw 2010-12-27  
  64.      * @param args  
  65.      */ 
  66.     public static void main(String[] args) {  
  67.         Collection<File> resFileList = new ArrayList<File>();   
  68.         resFileList.add(new File("E:\\im\\deps_last.xml"));   
  69.         //resFileList.add(new File("E:\\im\\user_last.xml"));   
  70.         //File zipFile = new File("e:\\im\\depuser.zip");  
  71.         File zipFile = new File("e:\\im\\deps_last.zip");   
  72.         try {  
  73.             batchZipFiles(resFileList,zipFile);  
  74.         } catch (IOException e) {  
  75.             // TODO Auto-generated catch block  
  76.             e.printStackTrace();  
  77.         }  
  78.     }  
  79.  
  80. }