java-压缩文件成zip文件(多文件/单文件/多目录/单目录/无目录),用于下载
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处。
http://www.cnblogs.com/king-xg/p/6424788.html
上代码:
package com.king.util; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; /**
* 压缩文件工具类
* 功能: 支持单文件下载,多文件下载,一目录多文件下载,一目录一文件下载
*
* @author xg君
*
*/
public class CompressFileUtil { private final static byte[] bytes = new byte[1024]; /**
* 单文件压缩
*
* @param srcFilePath
* @param descFilePath
* @param directory
* @throws Exception
*/
public static void compress(String srcFilePath, String descFilePath, String directory) throws Exception{
// 源文件
File srcFile = new File(srcFilePath);
// 目标文件
File descFile = new File(descFilePath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(descFile));
compress(srcFile,zos,directory);
zos.close();
} private static void compress(File srcFile ,ZipOutputStream zos, String directory) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
String basePath = "";
if (directory.trim().length() != 0) {
basePath = directory + File.separator;
}
zos.putNextEntry(new ZipEntry(basePath + srcFile.getName()));
int len = -1;
while((len = bis.read(bytes)) > 0){
zos.write(bytes);
}
zos.flush();
bis.close();
} /**
* 多文件下载 - one or no directory
*
* @param srcFiles
* @param descFilePath
* @throws Exception
*/
public static void compress(File[] srcFiles, String descFilePath, String directory) throws Exception{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(descFilePath)));
for(File file : srcFiles){
compress(file,zos,directory);
}
zos.close();
} /**
* 多文件下载 - multiple files and directories
*
* @param srcFiles
* @param descFilePath
* @param directories
* @throws Exception
*/
public static void compress(File[] srcFiles, String descFilePath, String[] directories) throws Exception{
if(srcFiles.length != directories.length){
throw new Exception("源文件(srcFiles)数量与目录(directories)数量不符!");
}
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(descFilePath)));
for(int i = 0; i < srcFiles.length; i++){
compress(srcFiles[i], zos, directories[i]);
}
zos.close();
} /**
* 多文件下载 - one directory for multiple files
*
* @param map
* @param descFilePath
* @throws Exception
*/
public static void compress(Map<String, String[]> map, String descFilePath) throws Exception{
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(descFilePath)));
Set<Entry<String, String[]>> set = map.entrySet();
for(Entry<String, String[]> en : set){
String directory = en.getKey();
String[] srcFilesPath = en.getValue();
for(String srcFilePath : srcFilesPath){
compress(new File(srcFilePath),zos,directory);
}
}
zos.close();
} }
这是我在工作之余,小结的代码。
java-压缩文件成zip文件(多文件/单文件/多目录/单目录/无目录),用于下载的更多相关文章
- Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)
本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...
- java 压缩和解压zip包
网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包
package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...
- Java打包多文件成zip
package com.zh.java.util; import lombok.extern.slf4j.Slf4j; import java.io.File;import java.io.FileI ...
- python实现压缩文件成zip格式
实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- Java实现将文件或者文件夹压缩成zip
最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类. 这个工具类的功能为: ( ...
- java将文件打包成ZIP压缩文件的工具类实例
package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...
随机推荐
- Focalprice李培亮:梦想让人在我店里排队
[亿邦动力网讯]4月3日消息,外贸B2C平台Focalprice总裁李培亮日前亮相亿邦动力网联合河南省商务厅举办的“第九届中国中小企业电子商务大会暨2014中国(河南)跨境贸易电子商务峰会”,表达自己 ...
- 无法设置主体sa的凭据
设置允许SQL Server身份登录 1.先用Window方式登陆进去,选择数据库实例,右键选择属性——安全性:把服务器身份验证选项从“Window身份验证模式”改为“SQLServer和Window ...
- 福大软工1816:Beta(4/7)
Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 准备四六级 展示GitHub当日代码/ ...
- web登录密码加密
文章:如何实现登录页面密码加密 文章:用RSA加密实现Web登录密码加密传输 文章:web登录用户名密码加密 知乎文章:Web前端密码加密是否有意义? 文章:记录一次黑客模拟攻击 成功拿到淘宝账号和密 ...
- Servlet中常用对象及API类之间的关系
Servlet最常用的对象: 请求对象:ServletRequest和HttpServletRequest,通过该对象获取来自客户端的请求信息 响应对象:ServletResponse和HttpSer ...
- CCF——折点计数201604-1
问题描述 给定n个整数表示一个商店连续n天的销售量.如果某天之前销售量在增长,而后一天销售量减少,则称这一天为折点,反过来如果之前销售量减少而后一天销售量增长,也称这一天为折点.其他的天都不是折点.如 ...
- Objective - C 之延展
延展:为已有的类新增私有方法,只能在本类中使用 一.创建过程: 二.总结: 1.延展只有.h文件,在其中写新方法的声明,在原本的类(Person)中写方法的实现: 2.上述的方法其实很不安全,因为如果 ...
- Beats Solo3 Wireless 无法链接 MacBook pro
Beats Solo3 Wireless 无法链接 MacBook pro 问题解决了,原来只要长按耳机的开关按钮就能被识别到了,貌似需要5秒钟不松手. https://bbs.feng.com/re ...
- chrome调试selenium。其实我是无聊了
from selenium import webdriverdriver = webdriver.Chrome()driver.get("http://www.baidu.com" ...
- 秒杀多线程第十四篇 读者写者问题继 读写锁SRWLock (续)
java 包实现了读写锁的操作: package com.multithread.readwritelock; import java.util.concurrent.CountDownLatch; ...