JAVA实现把指定文件夹下的所有文件压缩成zip包

来源:互联网 发布:剑网三更新网络中断 编辑:程序博客网 时间:2024/06/10 09:35

1.代码如下:

[java] view plain copy
  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. }  
  101. </span>  


2.结果如下:

文件打包成功!


3.到D:/tmp下查看,你会发现生成了一个zip压缩包.

阅读全文
0 0