JAVA實現把指定文件夾下的所有文件壓縮成zip包

  1. <span style="font-size:18px;background-color: rgb(204, 204, 204);">package cn.gov.csrc.base.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.FileNotFoundException;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.util.zip.ZipEntry;  
  11. import java.util.zip.ZipOutputStream;  
  12. /** 
  13.  * 將文件夾下面的文件 
  14.  * 打包成zip壓縮文件 
  15.  *  
  16.  * @author admin 
  17.  * 
  18.  */  
  19. public final class FileToZip {  
  20.   
  21.     private FileToZip(){}  
  22.       
  23.     /** 
  24.      * 將存放在sourceFilePath目錄下的源文件,打包成fileName名稱的zip文件,並存放到zipFilePath路徑下 
  25.      * @param sourceFilePath :待壓縮的文件路徑 
  26.      * @param zipFilePath :壓縮後存放路徑 
  27.      * @param fileName :壓縮後文件的名稱 
  28.      * @return 
  29.      */  
  30.     public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){  
  31.         boolean flag = false;  
  32.         File sourceFile = new File(sourceFilePath);  
  33.         FileInputStream fis = null;  
  34.         BufferedInputStream bis = null;  
  35.         FileOutputStream fos = null;  
  36.         ZipOutputStream zos = null;  
  37.           
  38.         if(sourceFile.exists() == false){  
  39.             System.out.println("待壓縮的文件目錄:"+sourceFilePath+"不存在.");  
  40.         }else{  
  41.             try {  
  42.                 File zipFile = new File(zipFilePath + "/" + fileName +".zip");  
  43.                 if(zipFile.exists()){  
  44.                     System.out.println(zipFilePath + "目錄下存在名字爲:" + fileName +".zip" +"打包文件.");  
  45.                 }else{  
  46.                     File[] sourceFiles = sourceFile.listFiles();  
  47.                     if(null == sourceFiles || sourceFiles.length<1){  
  48.                         System.out.println("待壓縮的文件目錄:" + sourceFilePath + "裏面不存在文件,無需壓縮.");  
  49.                     }else{  
  50.                         fos = new FileOutputStream(zipFile);  
  51.                         zos = new ZipOutputStream(new BufferedOutputStream(fos));  
  52.                         byte[] bufs = new byte[1024*10];  
  53.                         for(int i=0;i<sourceFiles.length;i++){  
  54.                             //創建ZIP實體,並添加進壓縮包  
  55.                             ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
  56.                             zos.putNextEntry(zipEntry);  
  57.                             //讀取待壓縮的文件並寫進壓縮包裏  
  58.                             fis = new FileInputStream(sourceFiles[i]);  
  59.                             bis = new BufferedInputStream(fis, 1024*10);  
  60.                             int read = 0;  
  61.                             while((read=bis.read(bufs, 01024*10)) != -1){  
  62.                                 zos.write(bufs,0,read);  
  63.                             }  
  64.                         }  
  65.                         flag = true;  
  66.                     }  
  67.                 }  
  68.             } catch (FileNotFoundException e) {  
  69.                 e.printStackTrace();  
  70.                 throw new RuntimeException(e);  
  71.             } catch (IOException e) {  
  72.                 e.printStackTrace();  
  73.                 throw new RuntimeException(e);  
  74.             } finally{  
  75.                 //關閉流  
  76.                 try {  
  77.                     if(null != bis) bis.close();  
  78.                     if(null != zos) zos.close();  
  79.                 } catch (IOException e) {  
  80.                     e.printStackTrace();  
  81.                     throw new RuntimeException(e);  
  82.                 }  
  83.             }  
  84.         }  
  85.         return flag;  
  86.     }  
  87.       
  88.     public static void main(String[] args){  
  89.         String sourceFilePath = "D:\\TestFile";  
  90.         String zipFilePath = "D:\\tmp";  
  91.         String fileName = "12700153file";  
  92.         boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);  
  93.         if(flag){  
  94.             System.out.println("文件打包成功!");  
  95.         }else{  
  96.             System.out.println("文件打包失敗!");  
  97.         }  
  98.     }  
  99.       
  100. }  

  1. </span>  

2.結果如下:

文件打包成功!


3.到D:/tmp下查看,你會發現生成了一個zip壓縮包.


發佈了151 篇原創文章 · 獲贊 31 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章