本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处。

  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文件(多文件/单文件/多目录/单目录/无目录),用于下载的更多相关文章

  1. Java压缩/解压.zip、.tar.gz、.tar.bz2(支持中文)

    本文介绍Java压缩/解压.zip..tar.gz..tar.bz2的方式. 对于zip文件:使用java.util.zip.ZipEntry 和 java.util.zip.ZipFile,通过设置 ...

  2. java 压缩和解压zip包

    网上有关压缩和解压zip包的博文一大堆,我随便找了一个.看了看,依照自己的须要改动了一下,与各位分享一下,希望各位大神指正: package com.wangpeng.utill; import ja ...

  3. 原生java 压缩解压zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  4. JAVA压缩解压ZIP文件,中文乱码还需要ANT.JAR包

    package zip; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStrea ...

  5. Java打包多文件成zip

    package com.zh.java.util; import lombok.extern.slf4j.Slf4j; import java.io.File;import java.io.FileI ...

  6. python实现压缩文件成zip格式

    实现代码如下: #压缩文件 import time,zipfile class zip: def get_zip(self,files,zip_name): zp=zipfile.ZipFile(zi ...

  7. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  8. Java实现将文件或者文件夹压缩成zip

            最近碰到个需要下载zip压缩包的需求,于是我在网上找了下别人写好的zip工具类.但找了好多篇博客,总是发现有bug.因此就自己来写了个工具类.         这个工具类的功能为: ( ...

  9. java将文件打包成ZIP压缩文件的工具类实例

    package com.lanp; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

随机推荐

  1. New York Comic Con 2013 - 2013年纽约动漫展

    New York Comic Con - 2013年纽约动漫展 New York Comic Con is the largest pop culture event on the East Coas ...

  2. Centos 7 zabbix 实战应用

    实际需求:公司已经有了100台服务器,现在需要使用zabbix全部监控起来. 先出个方案(规划) 常规监控:cpu,内存,磁盘,网卡 问题:怎样快速添加100台机器         方法1:使用克隆的 ...

  3. World Cup(思维+模拟)

    Description Allen wants to enter a fan zone(球迷区) that occupies a round square and has nn entrances. ...

  4. pandas中DataFrame的ix,loc,iloc索引方式的异同

    pandas中DataFrame的ix,loc,iloc索引方式的异同 1.loc: 按照标签索引,范围包括start和end 2.iloc: 在位置上进行索引,不包括end 3.ix: 先在inde ...

  5. 第二章:Internet地址结构

    引言 本章主要介绍了: 如何为Internet中的设备分配IP地址. 有助于理由可扩展性的地址结构分配方式. 特殊用途的地址. 表示IP地址 IPv4地址 长32位,采用点分四组或点分十进制来表示. ...

  6. 2016-2017 ACM-ICPC Northeastern European Regional Contest Problem E. Expect to Wait

    题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229509 时间限制:2s 空间限制:512MB 题目大意: 在一个车站中有若干人在队列中等待 ...

  7. 七周七语言之使用prolog解决爱因斯坦斑马难题

    如果你想获得更好的阅读体验,可以前往我在 github 上的博客进行阅读,http://lcomplete.github.io/blog/2013/06/28/sevenlang-prolog/. 目 ...

  8. mosquitto启动时Address already in use 和 一般的 Address already in use

    对于mosquitto启动时的地址占用错误,可能是mosquitto启动之后没关掉,进程一直占用了端口.查看mosquitto相关的进程,然后关掉就可启动了. 关掉mosquitto进程即可 参考: ...

  9. CodeForces Round #527 (Div3) D1. Great Vova Wall (Version 1)

    http://codeforces.com/contest/1092/problem/D1 Vova's family is building the Great Vova Wall (named b ...

  10. laya3d 文件格式

    先认识下laya3d的一些文件 导出文件说明:               ls        ---    场景文件: Json文件,包含场景中所有节点的数据信息,包含光照贴图信息          ...