java实现zip压缩文件(同一文件夹下的多个文件夹打成一个zip包)

参考:http://blog.163.com/shanqing_shuixiu@yeah/blog/static/165319229201192444434865/

 

Talk is cheap,show me the code!


同一文件夹下的多个文件夹打成一个zip包


  1. import java.io.BufferedInputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.util.zip.CRC32;  
  6. import java.util.zip.CheckedOutputStream;  
  7. import java.util.zip.ZipEntry;  
  8. import java.util.zip.ZipOutputStream;  
  9.   
  10. public class ZipCompressor {     
  11.     static final int BUFFER = 8192;     
  12.     
  13.     private File zipFile;     
  14.       
  15.     public ZipCompressor(String pathName) {     
  16.         zipFile = new File(pathName);     
  17.     }     
  18.     public void compress(String... pathName) {   
  19.         ZipOutputStream out = null;     
  20.         try {    
  21.             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);     
  22.             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
  23.                     new CRC32());     
  24.             out = new ZipOutputStream(cos);     
  25.             String basedir = "";   
  26.             for (int i=0;i
  27.                 compress(new File(pathName[i]), out, basedir);     
  28.             }  
  29.             out.close();    
  30.         } catch (Exception e) {     
  31.             throw new RuntimeException(e);     
  32.         }   
  33.     }     
  34.     public void compress(String srcPathName) {     
  35.         File file = new File(srcPathName);     
  36.         if (!file.exists())     
  37.             throw new RuntimeException(srcPathName + "不存在!");     
  38.         try {     
  39.             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);     
  40.             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
  41.                     new CRC32());     
  42.             ZipOutputStream out = new ZipOutputStream(cos);     
  43.             String basedir = "";     
  44.             compress(file, out, basedir);     
  45.             out.close();     
  46.         } catch (Exception e) {     
  47.             throw new RuntimeException(e);     
  48.         }     
  49.     }     
  50.     
  51.     private void compress(File file, ZipOutputStream out, String basedir) {     
  52.         /* 判断是目录还是文件 */    
  53.         if (file.isDirectory()) {     
  54.             System.out.println("压缩:" + basedir + file.getName());     
  55.             this.compressDirectory(file, out, basedir);     
  56.         } else {     
  57.             System.out.println("压缩:" + basedir + file.getName());     
  58.             this.compressFile(file, out, basedir);     
  59.         }     
  60.     }     
  61.     
  62.     /** 压缩一个目录 */    
  63.     private void compressDirectory(File dir, ZipOutputStream out, String basedir) {     
  64.         if (!dir.exists())     
  65.             return;     
  66.     
  67.         File[] files = dir.listFiles();     
  68.         for (int i = 0; i < files.length; i++) {     
  69.             /* 递归 */    
  70.             compress(files[i], out, basedir + dir.getName() + "/");     
  71.         }     
  72.     }     
  73.     
  74.     /** 压缩一个文件 */    
  75.     private void compressFile(File file, ZipOutputStream out, String basedir) {     
  76.         if (!file.exists()) {     
  77.             return;     
  78.         }     
  79.         try {     
  80.             BufferedInputStream bis = new BufferedInputStream(     
  81.                     new FileInputStream(file));     
  82.             ZipEntry entry = new ZipEntry(basedir + file.getName());     
  83.             out.putNextEntry(entry);     
  84.             int count;     
  85.             byte data[] = new byte[BUFFER];     
  86.             while ((count = bis.read(data, 0, BUFFER)) != -1) {     
  87.                 out.write(data, 0, count);     
  88.             }     
  89.             bis.close();     
  90.         } catch (Exception e) {     
  91.             throw new RuntimeException(e);     
  92.         }     
  93.     }     
  94.    public static void main(String[] args) {     
  95.         ZipCompressor zc = new ZipCompressor("E:/resource/resource.zip");     
  96.         zc.compress("E:/resource/js","E:/resource/css","E:/resource/images");    
  97.     }  
  98. }

你可能感兴趣的:(Java后台)