java 实现多个文件的Zip包的生成
最近在项目中遇到多个文件的达成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包的生成的更多相关文章
- java批量下载文件为zip包
批量下载文件为zip包的工具类 package com.meeno.trainsys.util; import javax.servlet.http.HttpServletRequest; impor ...
- mac终端命令加密压缩文件为zip包
mac终端命令加密压缩文件为zip包,命令如下: zip -e ~/desktop/a.zip b.doc c.txt d.sql 注释:a.zip为加密后的文件 b.doc c.txt d.sql为 ...
- .bat文件和Jar包的生成及运行
.bat文件和Jar包的生成及运行 1.Jar包简单介绍 Jar包是Java中所特有的一种压缩文档,有点类似于zip包,区别在于Jar包中有一个META-INF\MANIFEST.MF文件(在生成Ja ...
- JAVA 实现将多目录多层级文件打成ZIP包后保留层级目录下载 ZIP压缩 下载
将文件夹保留目录打包为 ZIP 压缩包并下载 上周做了一个需求,要求将数据库保存的 html 界面取出后将服务器下的css和js文件一起打包压缩为ZIP文件,返回给前台:在数据库中保存的是html标签 ...
- java application maven项目打自定义zip包
1.配置pom.xml文件,添加build节点 <build> <!-- 输出的包名 --> <finalName>p2p</finalName> &l ...
- java服务安装(一):使用java service wrapper及maven打zip包
目录(?)[+] 1概述 1_1为什么要用服务形式运行 1_2如何让java程序以服务形式运行 1_3打包需求 2程序示例 3maven打zip包 3_1maven-assembly-plugin介绍 ...
- springboot jar文件打zip包运行linux环境中
1.添加打包配置文件 1.1 assembly.xml <assembly xmlns="http://maven.apache.org/plugins/maven-assembly ...
- shell命令:给当前目录里一个文件压缩一份不包含.svn文件的zip包
filepath=$(cd ")"; pwd) packagePath="$filepath"/package zipPath="$filepath& ...
- 可解压带中文名称文件的zip包
package com.text.ziptest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; i ...
随机推荐
- 在Action 中访问web资源
1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...
- android 设置状态栏与标题背景颜色一致
必须在Android4.4以上版本才能设置状态栏颜色: 一.在单个Activity里面,设置状态栏的背景: 效果: 1.在Activity的布局根文件中添加属性: android:fitsSystem ...
- funny_python 00 The Zen of Python
# 打算每天多动的时候尽量搜索一些和coding相关的funny stuff Day 00 - PEP 20 The Zen of Python 在shell里面输入python -m this 回车 ...
- camstar --设备保养
- I/O流——字符流
字符流 字节流提供处理任何类型输入/输出操作的足够功能,但不能直接操作Unicode字符,因而需要字符流. 字符流层次结构的顶层是Reader和Writer抽象类. 实际上,字符流的底层就是字节流. ...
- 关于readdir返回值中struct dirent.d_type的取值有关问题(转)
关于readdir返回值中struct dirent.d_type的取值问题 原网页链接 http://www.gnu.org/software/libc/manual/html_node/Direc ...
- vue路由的简单实例
vue2.0 和 vue1.0 路由的语法还是有点稍微的差别,下面介绍一下vue-router 2的简单实例: <!DOCTYPE html> <html lang="en ...
- ZT 北大青鸟APTECH(南京泰思特)
北大青鸟APTECH(南京泰思特)授权培训中心诚招 高级软件测试/开发培训讲师 Posted on 2008-01-14 11:38 追求卓越 阅读(590) 评论(7) 编辑 收藏 北京阿博泰克北大 ...
- HTML5离线篇收藏--- cache manifest
自从翻译了<解读 HTML5:建议.技巧和技术>,就一直没有时间去看 HTML5 相关的东西.上周一次偶然的工作间隙折腾了下 Cache Manifest .当时直接拿博客当测试环境,虽然 ...
- netcat使用
一.端口监听(实时消息) 首先在A计算机上,它充当的是服务器角色,$ nc -l 3333 这时就创建了一个监听端口(listening socket(server)).- -l 它让 nc 监听一个 ...