当前位置: 首页 > article >正文

java打包zip并下载_Java批量下载文件并zip打包

客户需求:列表勾选需要的信息,点击批量下载文件的功能。这里分享下我们系统的解决方案:先生成要下载的文件,然后将其进行压缩,生成zip压缩文件,然后使用浏览器的下载功能即可完成批量下载的需求。以下是zip工具类:

package test;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.collections.CollectionUtils;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

*

* 描述: ZipUtils.java

* @author 小当家

* @created 2017年10月27日

*/

public class ZipUtils {

private static Logger logger = LoggerFactory.getLogger(ZipUtils.class);

// 目录标识判断符

private static final String PATCH = "/";

// 基目录

private static final String BASE_DIR = "";

// 缓冲区大小

private static final int BUFFER = 2048;

// 字符集

private static final String CHAR_SET = "GBK";

/**

*

* 描述: 压缩文件

* @author 小当家

* @created 2017年10月27日

* @param fileOutName

* @param files

* @throws Exception

*/

public static void compress(String fileOutName, List files) throws Exception {

try {

FileOutputStream fileOutputStream = new FileOutputStream(fileOutName);

ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

zipOutputStream.setEncoding(CHAR_SET);

if (files != null && files.size() > 0) {

for (int i = 0,size = files.size(); i < size; i++) {

compress(files.get(i), zipOutputStream, BASE_DIR);

}

}

// 冲刷输出流

zipOutputStream.flush();

// 关闭输出流

zipOutputStream.close();

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述:压缩文件并进行Base64加密

* @author 小当家

* @created 2017年10月27日

* @param files

* @return

* @throws Exception

*/

public static String compressToBase64(List files) throws Exception {

try {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ZipOutputStream zipOutputStream = new ZipOutputStream(bos);

zipOutputStream.setEncoding(CHAR_SET);

if (files != null && files.size() > 0) {

for (int i = 0,size = files.size(); i < size; i++) {

compress(files.get(i), zipOutputStream, BASE_DIR);

}

}

// 冲刷输出流

zipOutputStream.flush();

// 关闭输出流

zipOutputStream.close();

byte[] data = bos.toByteArray();

return new String(Base64.encodeBase64(data));

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述: 压缩

* @author 小当家

* @created 2017年10月27日

* @param srcFile

* @param zipOutputStream

* @param basePath

* @throws Exception

*/

public static void compress(File srcFile, ZipOutputStream zipOutputStream, String basePath) throws Exception {

if (srcFile.isDirectory()) {

compressDir(srcFile, zipOutputStream, basePath);

} else {

compressFile(srcFile, zipOutputStream, basePath);

}

}

/**

*

* 描述:压缩目录下的所有文件

* @author 小当家

* @created 2017年10月27日

* @param dir

* @param zipOutputStream

* @param basePath

* @throws Exception

*/

private static void compressDir(File dir, ZipOutputStream zipOutputStream, String basePath) throws Exception {

try {

// 获取文件列表

File[] files = dir.listFiles();

if (files.length < 1) {

ZipEntry zipEntry = new ZipEntry(basePath + dir.getName() + PATCH);

zipOutputStream.putNextEntry(zipEntry);

zipOutputStream.closeEntry();

}

for (int i = 0,size = files.length; i < size; i++) {

compress(files[i], zipOutputStream, basePath + dir.getName() + PATCH);

}

} catch (Exception e) {

throw new Exception(e.getMessage(), e);

}

}

/**

*

* 描述:压缩文件

* @author 小当家

* @created 2017年10月27日

* @param file

* @param zipOutputStream

* @param dir

* @throws Exception

*/

private static void compressFile(File file, ZipOutputStream zipOutputStream, String dir) throws Exception {

try {

// 压缩文件

ZipEntry zipEntry = new ZipEntry(dir + file.getName());

zipOutputStream.putNextEntry(zipEntry);

// 读取文件

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

int count = 0;

byte data[] = new byte[BUFFER];

while ((count = bis.read(data, 0, BUFFER)) != -1) {

zipOutputStream.write(data, 0, count);

}

bis.close();

zipOutputStream.closeEntry();

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述: 文件Base64加密

* @author 小当家

* @created 2017年10月27日 上午9:27:38

* @param srcFile

* @return

* @throws Exception

*/

public static String encodeToBASE64(File srcFile) throws Exception {

try {

ByteArrayOutputStream bos = new ByteArrayOutputStream();

// 读取文件

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));

int count = 0;

byte data[] = new byte[BUFFER];

while ((count = bis.read(data, 0, BUFFER)) != -1) {

bos.write(data, 0, count);

}

bis.close();

byte[] base64Data = Base64.encodeBase64(bos.toByteArray());

if (null == base64Data) {

bos.close();

return null;

}

bos.close();

return new String(base64Data, CHAR_SET);

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述: 文件Base64解密

* @author 小当家

* @created 2017年10月27日

* @param destFile

* @param encodeStr

* @throws Exception

*/

public static void decodeToBase64(File destFile, String encodeStr) throws Exception {

try {

byte[] decodeBytes = Base64.decodeBase64(encodeStr.getBytes());

ByteArrayInputStream bis = new ByteArrayInputStream(decodeBytes);

// 读取文件

FileOutputStream fileOutputStream = new FileOutputStream(destFile);

int count = 0;

byte data[] = new byte[BUFFER];

while ((count = bis.read(data, 0, BUFFER)) != -1) {

fileOutputStream.write(data, 0, count);

}

fileOutputStream.close();

bis.close();

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述: 解压缩

* @author 小当家

* @created 2017年10月27日

* @param srcFileName

* @param destFileName

* @throws Exception

*/

@SuppressWarnings("unchecked")

public static void decompress(String srcFileName, String destFileName)  throws Exception {

try {

ZipFile zipFile = new ZipFile(srcFileName);

Enumeration entries = zipFile.getEntries();

File destFile = new File(destFileName);

InputStream inputStream = null;

while(entries.hasMoreElements()) {

ZipEntry zipEntry = (ZipEntry)entries.nextElement();

String dir = destFile.getPath() + File.separator + zipEntry.getName();

File dirFile = new File(dir);

if (zipEntry.isDirectory()) {

dirFile.mkdirs();

} else {

fileProber(dirFile);

inputStream = zipFile.getInputStream(zipEntry);

decompressFile(dirFile, inputStream);

}

}

zipFile.close();

} catch (Exception e) {

throw new Exception(e.getMessage(),e);

}

}

/**

*

* 描述: 解压文件

* @author 小当家

* @created 2017年10月27日

* @param destFile

* @param inputStream

* @throws Exception

*/

private static void decompressFile(File destFile, InputStream inputStream) throws Exception {

try {

// 文件输入流

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

int count = 0;

byte data[] = new byte[BUFFER];

while ((count = inputStream.read(data, 0, BUFFER)) != -1) {

bos.write(data, 0, count);

}

bos.close();

inputStream.close();

} catch (Exception e) {

throw new Exception(e.getMessage(), e);

}

}

/**

*

* 描述:文件探测

* @author 小当家

* @created 2017年10月27日

* @param dirFile

*/

private static void fileProber(File dirFile) {

File parentFile = dirFile.getParentFile();

if (!parentFile.exists()) {

// 递归寻找上级目录

fileProber(parentFile);

parentFile.mkdir();

}

}

public static void main(String[] args) {

try {

ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(new File("D:/a/a.zip")));

zipOutputStream.setEncoding(CHAR_SET);

List files = new ArrayList();

files.add(new File("D:/a/1.xls"));

files.add(new File("D:/a/2.xls"));

files.add(new File("D:/a/1.java"));

if (CollectionUtils.isEmpty(files) == false) {

for (int i = 0,size = files.size(); i < size; i++) {

compress(files.get(i), zipOutputStream, BASE_DIR);

}

}

// 冲刷输出流

zipOutputStream.flush();

// 关闭输出流

zipOutputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

所需要的jar包:

ant-1.9.4.jar

commons-codec-1.10.jar

commons-collections-3.2.2.jar

log4j-1.2.17.jar

slf4j-api-1.7.21.jar

slf4j-log4j12-1.7.21.jar

例子中最后会压缩成一个a.zip如图:

a419393e7e80a0d5cb4b84fda52f9f4d.png

DEMO下载地址:https://dwz.cn/Jw3z6fVq

http://www.lryc.cn/news/2419108.html

相关文章:

  • 常量和常量表达式
  • 替代联阳IT6564方案|CapstoneCS5262替代IT6564设计DP转HDMI+VGA扩展坞方案|Capstone CS5262设计参考
  • 网站打开速度慢的原因,排查方法及优化方法(大全)
  • 浪潮集团数据中台建设方案(WORD)
  • 简单易懂DFS(一) dfs + 回溯
  • 使用ensp模拟器中的路由器配置vrrp详解
  • 海思3518E开发笔记1.2——海思SDK脚本学习
  • Hibernate笔记
  • 启动应用程序出现wsock32.dll找不到问题解决
  • 用Sygate实现单网卡共享上网
  • AlertDialog详解
  • Android终端系统APP应用性能测试之响应速度流畅度
  • EasyCamera--更简单更灵活的相机应用编写
  • 轻量级网络IP扫描器WatchYourLAN
  • 如何组建局域网?
  • 新手iso系统怎么安装 新手安装iso镜像文件详细步骤
  • IDEA使用教程汇总
  • 自学前端第二十四天:Animation动画栈帧效果
  • win2008 r2 安装sqlserver 2000问题的解决方法
  • 标题栏位于图纸的什么位置_【教程】教你如何看懂机械图纸!
  • 51单片机内核及其工作原理
  • WebRequest 模拟请求登录 终于搞定了!
  • cygwin下载地址
  • iOS 性能调优,成为一名合格iOS程序员必须掌握的技能
  • 进程间通信 —— 管道(Interprocess Communications —— Pipes)
  • SLM7.1SR1SP05 配置(configuration guide+ link help) - 03 initial configuration part4
  • c语言time_t转oletime,CTime、COleDateTime和CString之间的相互转化 | 求索阁
  • 《炬丰科技-半导体工艺》多晶硅表面微加工技术
  • 《Adobe After Effects CS6中文版经典教程》——1.2 创建项目并导入素材
  • 递归下降分析法js版