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

下面是相关代码:

package run.utils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; public class ZipCompressor {
static final int BUFFER = 8192; private File zipFile; public ZipCompressor(String pathName) {
zipFile = new File(pathName);
}
public void compress(String... pathName) {
ZipOutputStream out = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
out = new ZipOutputStream(cos);
String basedir = "";
for (int i=0;i<pathName.length;i++){
compress(new File(pathName[i]), out, basedir);
}
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void compress(String srcPathName) {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "不存在!");
try {
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,
new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
} private void compress(File file, ZipOutputStream out, String basedir) {
/* 判断是目录还是文件 */
if (file.isDirectory()) {
System.out.println("压缩:" + basedir + file.getName());
this.compressDirectory(file, out, basedir);
} else {
System.out.println("压缩:" + basedir + file.getName());
this.compressFile(file, out, basedir);
}
} /** 压缩一个目录 */
private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
if (!dir.exists())
return; File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
/* 递归 */
compress(files[i], out, basedir + dir.getName() + "/");
}
} /** 压缩一个文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
if (!file.exists()) {
return;
}
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
ZipCompressor zc = new ZipCompressor("C:/Users/wj/Desktop/aaa.zip");
zc.compress("C:/Users/wj/Desktop/water.js","C:/Users/wj/Desktop/index.jsp");
}
}

  

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. Maven安装与配置

    下载: 1.从官网http://maven.apache.org中下载,下载下来的是一个压缩包,解压即可.因为Maven本身也是用Java实现的.2.Maven的目录结构   /bin; maven的 ...

  2. kangle 默认支持ETag,如果是用kangle做源不会识别,但是做cdn或反向代理会自动识别

    kangle  默认支持ETag,如果是用kangle做源不会识别,但是做cdn或反向代理会自动识别

  3. 有关STL 标准模板库

    1.vector  本质:对数组的封装  特点:读取能在常数时间内完成

  4. Struts2理解--动态方法和method属性及通配符_默认Action

    众所周知,默认条件下,在浏览器输入indexAction!execute.action,便会执行indexAction类里的execute方法,这样虽然方便,但可能带来安全隐患,通过url可以执行Ac ...

  5. man curl_easy_init(原创)

    curl_easy_init(3) libcurl 手册 curl_easy_init(3) 名字 curl_easy_init - 开始一个简单的libcurl会话 梗概 #include < ...

  6. flexbox布局

    一.侧轴对齐伸缩项目--align-items 它充许调整伸缩项目在侧轴(也就是y轴)的对齐方式,主要包括以下几个值: flex-start/baseline:伸缩项目在侧轴起点边的外边距紧靠住该行在 ...

  7. shell编程之运算符

    declare声明变量类型 declare    [+ / -] [选项]  变量名 - :给变量设定类型属性 + :取消变量的类型属性 -a :将变量声明为数组型 -i :将变量声明为整数型 -x ...

  8. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  9. java程序故障排查脚本之——CPU占用高

    root@ubuntu-B85M-D3H:~/tmp# cat java_Analy.sh #!/bin/bash T=`ps -mp $1 -o THREAD,tid,time|sort -k 2 ...

  10. js中的一些容易混淆的方法!

    数组的一些方法: 1.join()和split()方法  与之相反的是split()方法:用于把一个字符串分割成字符串数组.  注意返回的数组中不包括separator本身: 提示和注释注释:如果把空 ...