java将文件打包成ZIP压缩文件的工具类实例

http://blog.csdn.net/lanpiao_87/article/details/7310461
  1. package com.lanp;  
  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.  * @author LanP 
  16.  * @since 2012-3-1 15:47 
  17.  */  
  18. public final class FileToZip {  
  19.       
  20.     private FileToZip() {  
  21.           
  22.     }  
  23.       
  24.     /** 
  25.      * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。 
  26.      * @param sourceFilePath 待压缩的文件路径 
  27.      * @param zipFilePath    压缩后存放路径 
  28.      * @param fileName       压缩后文件的名称 
  29.      * @return flag 
  30.      */  
  31.     public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {  
  32.         boolean flag = false;  
  33.         File sourceFile = new File(sourceFilePath);  
  34.         FileInputStream fis = null;  
  35.         BufferedInputStream bis = null;  
  36.         FileOutputStream fos = null;  
  37.         ZipOutputStream zos = null;  
  38.           
  39.         if(sourceFile.exists() == false) {  
  40.             System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");  
  41.         } else {  
  42.             try {  
  43.                 File zipFile = new File(zipFilePath + "/" + fileName + ".zip");  
  44.                 if(zipFile.exists()) {  
  45.                     System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:" + fileName + ".zip" + " 打包文件. <<<<<<");  
  46.                 } else {  
  47.                     File[] sourceFiles = sourceFile.listFiles();  
  48.                     if(null == sourceFiles || sourceFiles.length < 1) {  
  49.                         System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 里面不存在文件,无需压缩. <<<<<<");  
  50.                     } else {  
  51.                         fos = new FileOutputStream(zipFile);  
  52.                         zos = new ZipOutputStream(new BufferedOutputStream(fos));  
  53.                         byte[] bufs = new byte[1024*10];  
  54.                         for(int i=0;i<sourceFiles.length;i++) {  
  55.                             // 创建ZIP实体,并添加进压缩包  
  56.                             ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());  
  57.                             zos.putNextEntry(zipEntry);  
  58.                             // 读取待压缩的文件并写进压缩包里  
  59.                             fis = new FileInputStream(sourceFiles[i]);  
  60.                             bis = new BufferedInputStream(fis,1024*10);  
  61.                             int read = 0;  
  62.                             while((read=bis.read(bufs, 0, 1024*10)) != -1) {  
  63.                                 zos.write(bufs, 0, read);  
  64.                             }  
  65.                         }  
  66.                         flag = true;  
  67.                     }  
  68.                 }  
  69.             } catch (FileNotFoundException e) {  
  70.                 e.printStackTrace();  
  71.                 throw new RuntimeException(e);  
  72.             } catch (IOException e) {  
  73.                 e.printStackTrace();  
  74.                 throw new RuntimeException(e);  
  75.             } finally {  
  76.                 // 关闭流  
  77.                 try {  
  78.                     if(null != bis) bis.close();  
  79.                     if(null != zos) zos.close();  
  80.                 } catch (IOException e) {  
  81.                     e.printStackTrace();  
  82.                     throw new RuntimeException(e);  
  83.                 }  
  84.             }  
  85.         }  
  86.           
  87.         return flag;  
  88.     }  
  89.       
  90.     /** 
  91.      * 将文件打包成ZIP压缩文件,main方法测试 
  92.      * @param args 
  93.      */  
  94.     public static void main(String[] args) {  
  95.         String sourceFilePath = "C:\\home\\lp20120301";  
  96.         String zipFilePath = "C:\\home";  
  97.         String fileName = "lp20120301";  
  98.         boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);  
  99.         if(flag) {  
  100.             System.out.println(">>>>>> 文件打包成功. <<<<<<");  
  101.         } else {  
  102.             System.out.println(">>>>>> 文件打包失败. <<<<<<");  
  103.         }  
  104.     }  
  105. }  
posted @   郑文亮  阅读(749)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2013-11-07 jQuery select操作控制方法小结
2012-11-07 pb调用vc写的动态链接库文件
2012-11-07 vs2008下MFC内存泄露问题一点经验
2012-11-07 左值的理解(给渴望学习的新手)
2012-11-07 C++指针学习心得
2012-11-07 c++ 指针精髓
2012-11-07 C/C++中printf和C++中cout的输出格式
点击右上角即可分享
微信分享提示