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. SpringSecurity csrf验证忽略某些请求

    前几天项目中遇到springSecurity问题,研究了大半天,掉进了csrf的坑,先认识一下csrf CSRF概念:CSRF跨站点请求伪造(Cross—Site Request Forgery),跟 ...

  2. 大话C#中能使用foreach的集合的实现

    大家都知道foreach的语法:foreach(var item in items){ Console.Writeln(item);} 通过这样一个简单的语句,就能实现遍历集合items中的所有元素. ...

  3. 第5月第27天 cocos2d

    1. 流程是这样的: 在CCApplication的run函数中,显示设备链调用相应的场景显示函数drawScene来绘制场景,然后调用了CCScheduler的update函数,在这个函数里,对所有 ...

  4. 第8月第22天 python scrapy

    1. cd /Users/temp/Downloads/LagouSpider-master ls ls ls lagou/settings.py cat lagou/settings.py ls p ...

  5. 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复

    来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...

  6. flask基础之LocalProxy代理对象(八)

    前言 flask框架自带的代理对象有四个,分别是request,session,g和current_app,各自的含义我们在前面已经详细分析过.使用代理而不是显式的对象的主要目的在于这四个对象使用太过 ...

  7. OE中的bitbake使用

    OpenEmbedded是一些脚本(shell和python脚本)和数据构成的自动构建系统.     脚本实现构建过程,包括下载(fetch).解包(unpack).打补丁(patch).config ...

  8. SpringMVC_HelloWorld_01

    通过配置文件的方式实现一个简单的HelloWorld. 源码 一.新建项目 1.新建动态web项目 2.命名工程springmvc-01 3.勾选"Generate web.xml depl ...

  9. JAVA学习(二) String使用equals方法和==分别比较的是什么?(转)

    String使用的equals方法和==的区别 equals方法和==的区别   首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指 ...

  10. 常用的 Windows 键

    常用的 Windows 键 Windows 键 + 空格键 功能:透明化所有窗口,快速查看桌面.使用此快捷键可以立即将目前所有打开的窗口透明化,以便查看桌面. Windows 键 + 字母键“D”  ...