1. package com.lanp;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipOutputStream;
  11. /**
  12. * 将文件打包成ZIP压缩文件
  13. * @author LanP
  14. * @since 2012-3-1 15:47
  15. */
  16. public final class FileToZip {
  17. private FileToZip() {
  18. }
  19. /**
  20. * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。
  21. * @param sourceFilePath 待压缩的文件路径
  22. * @param zipFilePath    压缩后存放路径
  23. * @param fileName       压缩后文件的名称
  24. * @return flag
  25. */
  26. public static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName) {
  27. boolean flag = false;
  28. File sourceFile = new File(sourceFilePath);
  29. FileInputStream fis = null;
  30. BufferedInputStream bis = null;
  31. FileOutputStream fos = null;
  32. ZipOutputStream zos = null;
  33. if(sourceFile.exists() == false) {
  34. System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 不存在. <<<<<<");
  35. } else {
  36. try {
  37. File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
  38. if(zipFile.exists()) {
  39. System.out.println(">>>>>> " + zipFilePath + " 目录下存在名字为:" + fileName + ".zip" + " 打包文件. <<<<<<");
  40. } else {
  41. File[] sourceFiles = sourceFile.listFiles();
  42. if(null == sourceFiles || sourceFiles.length < 1) {
  43. System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath + " 里面不存在文件,无需压缩. <<<<<<");
  44. } else {
  45. fos = new FileOutputStream(zipFile);
  46. zos = new ZipOutputStream(new BufferedOutputStream(fos));
  47. byte[] bufs = new byte[1024*10];
  48. for(int i=0;i<sourceFiles.length;i++) {
  49. // 创建ZIP实体,并添加进压缩包
  50. ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
  51. zos.putNextEntry(zipEntry);
  52. // 读取待压缩的文件并写进压缩包里
  53. fis = new FileInputStream(sourceFiles[i]);
  54. bis = new BufferedInputStream(fis,1024*10);
  55. int read = 0;
  56. while((read=bis.read(bufs, 0, 1024*10)) != -1) {
  57. zos.write(bufs, 0, read);
  58. }
  59. }
  60. flag = true;
  61. }
  62. }
  63. } catch (FileNotFoundException e) {
  64. e.printStackTrace();
  65. throw new RuntimeException(e);
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. throw new RuntimeException(e);
  69. } finally {
  70. // 关闭流
  71. try {
  72. if(null != bis) bis.close();
  73. if(null != zos) zos.close();
  74. } catch (IOException e) {
  75. e.printStackTrace();
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. }
  80. return flag;
  81. }
  82. /**
  83. * 将文件打包成ZIP压缩文件,main方法测试
  84. * @param args
  85. */
  86. public static void main(String[] args) {
  87. String sourceFilePath = "C:\\home\\lp20120301";
  88. String zipFilePath = "C:\\home";
  89. String fileName = "lp20120301";
  90. boolean flag = FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName);
  91. if(flag) {
  92. System.out.println(">>>>>> 文件打包成功. <<<<<<");
  93. } else {
  94. System.out.println(">>>>>> 文件打包失败. <<<<<<");
  95. }
  96. }
  97. }

java将文件打包成ZIP压缩文件的工具类实例的更多相关文章

  1. java批量下载,将多文件打包成zip格式下载

    现在的需求的: 根据产品族.产品类型,下载该产品族.产品类型下面的pic包: pic包是zip压缩文件: t_product表: 这些包以blob形式存在另一张表中: t_imagefile表: 现在 ...

  2. 关于springmvc下服务器文件打包成zip格式下载功能

    关于springmvc下服务器文件打包成zip格式下载功能 2016年09月21日 11:22:14 toxic_guantou 阅读数:5731更多 个人分类: 技术点存储   版权声明:本文为博主 ...

  3. JAVA从服务器下载文件根据Url把多文件打包成ZIP下载

    注意: 1. String filename = new String(“xx.zip”.getBytes(“UTF-8”), “ISO8859-1”);包装zip文件名不发生乱码.  2.一定要注意 ...

  4. java批量将多文件打包成zip格式

    public void createzip(){ List<File> nFileList = new ArrayList<File>(); nFileList.add(new ...

  5. pyinstall python文件打包成二进制exe文件

    pycharm + python3 + win7 1 pip install pyinstall  (官网) 2 准备 .py 文件 3 具体例子 from PyQt5.QtWidgets impor ...

  6. 如何将编译后的文件打包成jar文件

    如果需要修改像spring和dubbo中的jar包源码,修改后怎么打包呢? 如下: 1.win+r进入命令行: 2.找到需要打包的class文件: 3.jar -cvf [jar包的名字] [需要打包 ...

  7. 将Java应用程序打包成可执行的Jar包

    可以将多个class文件打包为jar包,在指定程序入口点情况下,可以用 java –jar jar包名称 的方式调用jar包内主类的main函数. 程序源代码如下: //Math.java publi ...

  8. 把java文件打包成.jar (jar命令详解)

    把java文件打包成.jar (jar命令详解) 先打开命令提示符(win2000或在运行框里执行cmd命令,win98为DOS提示符),输入jar Chelp,然后回车(如果你盘上已经有了jdk1. ...

  9. 手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件

    1.背景: 学习java时,教材中关于如题问题,只有一小节说明,而且要自己写麻烦的配置文件,最终结果却只能转换为jar文件.实在是心有不爽.此篇博客教你如何方便快捷地把java代码,打包成jar文件以 ...

随机推荐

  1. java CountDownLatch的使用

    CountDownLatch能够使一个线程在等待另外一些线程完成各自工作之后,再继续执行.使用一个计数器进行实现.计数器初始值为线程的数量.当每一个线程完成自己任务后,计数器的值就会减一.当计数器的值 ...

  2. not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA

    Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AV ...

  3. ZYNQ. Interrupt(2)SPI.AXI TIMER

    Shared Peripheral Interrupts (SPI) SPI 可以接收来自PL的中断,这里使用PL模块 AXI Timer 的中断模式,并连接到CPU. AXI TIMER 定时器,内 ...

  4. networkManger介绍

    http://www.linuxidc.com/Linux/2013-08/88809.htm

  5. Oracle SQL部分练习题

    SQL练习题        注:查询列表不建议用 “*” 1.列出至少有一个雇员的所有部门: a. select * from dept where deptno in(select distinct ...

  6. php里获取第一个中文首字母并排序

    需求里结算首页需要按门店的首字母A-Z排序.我的数据结构原本是这样的: Array ( [0] => Array ( [sid] => 2885842 [recetcstoredpay] ...

  7. 兼容IE FF 获取鼠标位置

    由于Firefox和IE等浏览器之间对js解释的方式不一样,firefox下面获取鼠标位置不能够直接使用clientX来获取.网上说的一般都是触发mousemove事件才行.我这里有两段代码,思路都一 ...

  8. Luogu P1318 积水面积

    题目描述 一组正整数,分别表示由正方体迭起的柱子的高度.若某高度值为x,表示由x个正立方的方块迭起(如下图,0<=x<=5000).找出所有可能积水的地方(图中蓝色部分),统计它们可能积水 ...

  9. linux下如何查看命令的绝对路径

    在linux上经常使用ls,grep,vi等命令,如何查看这些命令的绝对路径呢? 通过whereis/which 就可以啦,但是这两个命令之间还是有一些区别.网上查了一下资料,解释如下: which ...

  10. 原生js返回顶部

    let backToTop = function() { let scrollToptimer = setInterval(function() { let top = document.body.s ...