最近在项目中遇到多个文件的达成Zip包,由于对这块不熟,在网上找到一个,现在忘了找的谁的,如果您发现了,请告诉我你的链接,我指明出处

下面是相关代码:

  1. package run.utils;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.zip.CRC32;
  7. import java.util.zip.CheckedOutputStream;
  8. import java.util.zip.ZipEntry;
  9. import java.util.zip.ZipOutputStream;
  10.  
  11. public class ZipCompressor {
  12. static final int BUFFER = 8192;
  13.  
  14. private File zipFile;
  15.  
  16. public ZipCompressor(String pathName) {
  17. zipFile = new File(pathName);
  18. }
  19. public void compress(String... pathName) {
  20. ZipOutputStream out = null;
  21. try {
  22. FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
  23. CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
  24. new CRC32());
  25. out = new ZipOutputStream(cos);
  26. String basedir = "";
  27. for (int i=0;i<pathName.length;i++){
  28. compress(new File(pathName[i]), out, basedir);
  29. }
  30. out.close();
  31. } catch (Exception e) {
  32. throw new RuntimeException(e);
  33. }
  34. }
  35. public void compress(String srcPathName) {
  36. File file = new File(srcPathName);
  37. if (!file.exists())
  38. throw new RuntimeException(srcPathName + "不存在!");
  39. try {
  40. FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
  41. CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
  42. new CRC32());
  43. ZipOutputStream out = new ZipOutputStream(cos);
  44. String basedir = "";
  45. compress(file, out, basedir);
  46. out.close();
  47. } catch (Exception e) {
  48. throw new RuntimeException(e);
  49. }
  50. }
  51.  
  52. private void compress(File file, ZipOutputStream out, String basedir) {
  53. /* 判断是目录还是文件 */
  54. if (file.isDirectory()) {
  55. System.out.println("压缩:" + basedir + file.getName());
  56. this.compressDirectory(file, out, basedir);
  57. } else {
  58. System.out.println("压缩:" + basedir + file.getName());
  59. this.compressFile(file, out, basedir);
  60. }
  61. }
  62.  
  63. /** 压缩一个目录 */
  64. private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
  65. if (!dir.exists())
  66. return;
  67.  
  68. File[] files = dir.listFiles();
  69. for (int i = 0; i < files.length; i++) {
  70. /* 递归 */
  71. compress(files[i], out, basedir + dir.getName() + "/");
  72. }
  73. }
  74.  
  75. /** 压缩一个文件 */
  76. private void compressFile(File file, ZipOutputStream out, String basedir) {
  77. if (!file.exists()) {
  78. return;
  79. }
  80. try {
  81. BufferedInputStream bis = new BufferedInputStream(
  82. new FileInputStream(file));
  83. ZipEntry entry = new ZipEntry(basedir + file.getName());
  84. out.putNextEntry(entry);
  85. int count;
  86. byte data[] = new byte[BUFFER];
  87. while ((count = bis.read(data, 0, BUFFER)) != -1) {
  88. out.write(data, 0, count);
  89. }
  90. bis.close();
  91. } catch (Exception e) {
  92. throw new RuntimeException(e);
  93. }
  94. }
  95. public static void main(String[] args) {
  96. ZipCompressor zc = new ZipCompressor("C:/Users/wj/Desktop/aaa.zip");
  97. zc.compress("C:/Users/wj/Desktop/water.js","C:/Users/wj/Desktop/index.jsp");
  98. }
  99. }

  

java 实现多个文件的Zip包的生成的更多相关文章

  1. java批量下载文件为zip包

    批量下载文件为zip包的工具类 package com.meeno.trainsys.util; import javax.servlet.http.HttpServletRequest; impor ...

  2. mac终端命令加密压缩文件为zip包

    mac终端命令加密压缩文件为zip包,命令如下: zip -e ~/desktop/a.zip b.doc c.txt d.sql 注释:a.zip为加密后的文件 b.doc c.txt d.sql为 ...

  3. .bat文件和Jar包的生成及运行

    .bat文件和Jar包的生成及运行 1.Jar包简单介绍 Jar包是Java中所特有的一种压缩文档,有点类似于zip包,区别在于Jar包中有一个META-INF\MANIFEST.MF文件(在生成Ja ...

  4. JAVA 实现将多目录多层级文件打成ZIP包后保留层级目录下载 ZIP压缩 下载

    将文件夹保留目录打包为 ZIP 压缩包并下载 上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为ZIP文件,返回给前台:在数据库中保存的是html标签 ...

  5. java application maven项目打自定义zip包

    1.配置pom.xml文件,添加build节点 <build> <!-- 输出的包名 --> <finalName>p2p</finalName> &l ...

  6. java服务安装(一):使用java service wrapper及maven打zip包

    目录(?)[+] 1概述 1_1为什么要用服务形式运行 1_2如何让java程序以服务形式运行 1_3打包需求 2程序示例 3maven打zip包 3_1maven-assembly-plugin介绍 ...

  7. springboot jar文件打zip包运行linux环境中

    1.添加打包配置文件 1.1  assembly.xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly ...

  8. shell命令:给当前目录里一个文件压缩一份不包含.svn文件的zip包

    filepath=$(cd ")"; pwd) packagePath="$filepath"/package zipPath="$filepath& ...

  9. 可解压带中文名称文件的zip包

    package com.text.ziptest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; i ...

随机推荐

  1. reference

    z@z:~$ g++ -std=c++11 ref.cpp ref.cpp:7:10: error: invalid conversion from 'int' to 'int*' [-fpermis ...

  2. sql 事物以及回滚

    第一种: Begin Try    Begin Tran Tran1        insert into t1(Id, c1) values(1,'1')        insert into t1 ...

  3. UITextView 显示不全的问题

    //设置UITextView的内边距 textView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);

  4. 被忽略的js细节

    今天在写一个程序,出了一点小bug,找了许久许久,终于把问题给揪了出来,真相大白那一刻感觉好蛋疼--深刻体会到了语言之间的差异. <script type="text/javascri ...

  5. sap 常用表

    MM    表名 EBAN 采购申请

  6. 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类

    一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...

  7. nginx下开启pathinfo模式

    第一种方式是通过重写url来实现pathinfo模式: location / { if (!-e $request_filename){ rewrite ^/(.*)$ /index.php?s=/$ ...

  8. numberOfRowsInSection方法什么时候调用

    昨天在代码里遇到个问题,很简单的数组越界,但是真心觉得自己把数据处理的思路都理清了不应该会出现这种情况,而且打印出来出现了"灵异事件",那就是行数只有14行,但是cell加载到了1 ...

  9. Dev GridControl数据修改后实时更新数据源

      1:  /// <summary> 2:  /// 嵌入的ComboxEdit控件选择值变化事件 3:  /// </summary> 4: /// <param n ...

  10. ORACLE 数据库需要创建索引的规则

    1.表的主键.外键必须有索引: 2.数据量超过300的表应该有索引: 3.经常与其他表进行连接的表,在连接字段上应该建立索引: 4.经常出现在Where子句中的字段,特别是大表的字段,应该建立索引: ...