java实现压缩文件夹(层级压缩)下载,java打包压缩文件夹下载

工具类如下
打包下载方法:exportZip(支持整个文件夹或单文件一起)
注意:前端发送请求不能用ajax,form表单提交可以,location.href也可以,window.open也可以,总之就ajax请求就是不行

import com.leatop.common.utils.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;public class FileToZipUtils {// 日志private static Logger log = LoggerFactory.getLogger(FileToZipUtils.class);// 创建文件夹public static String CreateFile(String dir) {File file = new File(dir);if (!file.exists()) {//创建文件夹boolean mkdir = file.mkdir();} else {}return dir;}// 复制文件public static void copyFile(File source, File dest) throws IOException {FileChannel inputChannel = null;FileChannel outputChannel = null;try {inputChannel = new FileInputStream(source).getChannel();outputChannel = new FileOutputStream(dest).getChannel();outputChannel.transferFrom(inputChannel, 0, inputChannel.size());} finally {inputChannel.close();outputChannel.close();}}// 删除文件public void delFile(File file) {File[] listFiles = file.listFiles();if (listFiles != null) {for (File f : listFiles) {if (f.isDirectory()) {delFile(f);} else {f.delete();}}}file.delete();}/*** 通过递归逐层删除文件信息** @param filePath*/public static void deleteFileByIO(String filePath) {File file = new File(filePath);File[] list = file.listFiles();if (list != null) {for (File temp : list) {deleteFileByIO(temp.getAbsolutePath());}}file.delete();}/*** 将指定路径下的所有文件打包zip导出** @param response       HttpServletResponse* @param sourceFilePath 要打包的路径* @param fileName       下载时的文件名称*                       //     * @param postfix 下载时的文件后缀 .zip/.rar*/public static void exportZip(HttpServletResponse response, String sourceFilePath, String fileName) {// 默认文件名以时间戳作为前缀SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String downloadName = sdf.format(new Date()) + fileName;// 将文件进行打包下载try {OutputStream os = response.getOutputStream();// 接收压缩包字节byte[] data = createZip(sourceFilePath);response.reset();response.setCharacterEncoding("UTF-8");response.addHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Expose-Headers", "*");// 下载文件名乱码问题response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));//response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);response.addHeader("Content-Length", "" + data.length);response.setContentType("application/octet-stream;charset=UTF-8");IOUtils.write(data, os);os.flush();os.close();} catch (Exception e) {e.printStackTrace();}}/**** 压缩文件变成zip输出流*/public static byte[] createZip(String srcSource) throws Exception {ByteArrayOutputStream outputStream = new ByteArrayOutputStream();ZipOutputStream zip = new ZipOutputStream(outputStream);//将目标文件打包成zip导出File file = new File(srcSource);createAllFile(zip, file, "");IOUtils.closeQuietly(zip);return outputStream.toByteArray();}/**** 对文件下的文件处理*/public static void createAllFile(ZipOutputStream zip, File file, String dir) throws Exception {//如果当前的是文件夹,则进行进一步处理if (file.isDirectory()) {//得到文件列表信息File[] files = file.listFiles();//将文件夹添加到下一级打包目录zip.putNextEntry(new ZipEntry(dir + "/"));dir = dir.length() == 0 ? "" : dir + "/";//循环将文件夹中的文件打包for (int i = 0; i < files.length; i++) {createAllFile(zip, files[i], dir + files[i].getName());                 //递归处理}} else {     //当前的是文件,打包处理//文件输入流BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));ZipEntry entry = new ZipEntry(dir);zip.putNextEntry(entry);zip.write(FileUtils.readFileToByteArray(file));IOUtils.closeQuietly(bis);zip.flush();zip.closeEntry();}}/*** 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下** @param sourceFilePath :待压缩的文件路径* @param zipFilePath    :压缩后存放路径* @param fileName       :压缩后文件的名称* @return*/public static boolean fileToZip(String sourceFilePath, String zipFilePath, String fileName) {boolean flag = false;File sourceFile = new File(sourceFilePath);FileInputStream fis = null;BufferedInputStream bis = null;FileOutputStream fos = null;ZipOutputStream zos = null;if (sourceFile.exists() == false) {log.info("待压缩的文件目录:" + sourceFilePath + "不存在.");} else {try {File zipFile = new File(zipFilePath + "/" + fileName + ".zip");if (zipFile.exists()) {log.info(zipFilePath + "目录下存在名字为:" + fileName + ".zip" + "打包文件.");} else {File[] sourceFiles = sourceFile.listFiles();if (null == sourceFiles || sourceFiles.length < 1) {log.info("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");} else {fos = new FileOutputStream(zipFile);zos = new ZipOutputStream(new BufferedOutputStream(fos));byte[] bufs = new byte[1024 * 10];for (int i = 0; i < sourceFiles.length; i++) {// 创建ZIP实体,并添加进压缩包ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());zos.putNextEntry(zipEntry);// 读取待压缩的文件并写进压缩包里fis = new FileInputStream(sourceFiles[i]);bis = new BufferedInputStream(fis, 1024 * 10);int read = 0;while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {zos.write(bufs, 0, read);}}flag = true;}}} catch (FileNotFoundException e) {e.printStackTrace();throw new RuntimeException(e);} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);} finally {// 关闭流try {if (null != bis)bis.close();if (null != zos)zos.close();} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}}return flag;}/*** 解压缩zip包** @param zipFilePath        需要解压的zip文件的全路径* @param unzipFilePath      解压后的文件保存的路径* @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含*/@SuppressWarnings("unchecked")public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception {if (StringUtils.isNotBlank(zipFilePath) || StringUtils.isNotBlank(unzipFilePath)) {File zipFile = new File(zipFilePath);// 如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径if (includeZipFileName) {String fileName = zipFile.getName();if (StringUtils.isNotEmpty(fileName)) {fileName = fileName.substring(0, fileName.lastIndexOf("."));}unzipFilePath = unzipFilePath + File.separator + fileName;}// 创建解压缩文件保存的路径File unzipFileDir = new File(unzipFilePath);if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {unzipFileDir.mkdirs();}                        // 开始解压ZipEntry entry = null;String entryFilePath = null, entryDirPath = null;File entryFile = null, entryDir = null;int index = 0, count = 0, bufferSize = 1024;byte[] buffer = new byte[bufferSize];BufferedInputStream bis = null;BufferedOutputStream bos = null;ZipFile zip = new ZipFile(zipFile);Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();// 循环对压缩包里的每一个文件进行解压while (entries.hasMoreElements()) {entry = entries.nextElement();// 构建压缩包中一个文件解压后保存的文件全路径entryFilePath = unzipFilePath + File.separator + entry.getName();// 构建解压后保存的文件夹路径index = entryFilePath.lastIndexOf(File.separator);if (index != -1) {entryDirPath = entryFilePath.substring(0, index);} else {entryDirPath = "";}entryDir = new File(entryDirPath);// 如果文件夹路径不存在,则创建文件夹if (!entryDir.exists() || !entryDir.isDirectory()) {entryDir.mkdirs();}                                // 创建解压文件entryFile = new File(entryFilePath);if (entryFile.exists()) {// 检测文件是否允许删除,如果不允许删除,将会抛出SecurityExceptionSecurityManager securityManager = new SecurityManager();securityManager.checkDelete(entryFilePath);// 删除已存在的目标文件entryFile.delete();}                                // 写入文件bos = new BufferedOutputStream(new FileOutputStream(entryFile));bis = new BufferedInputStream(zip.getInputStream(entry));while ((count = bis.read(buffer, 0, bufferSize)) != -1) {bos.write(buffer, 0, count);}bos.flush();bos.close();}zip.close();// 切记一定要关闭掉,不然在同一个线程,你想解压到临时路径之后,再去删除掉这些临时数据,那么就删除不了}}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/746055.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

生成式人工智能如何赋能SOC分析师?

以下是已经在全球SOC崭露头角的六大生成式人工智能应用。 在当今网络安全威胁日益严峻的形势下&#xff0c;安全运营中心(SOC)肩负着重大责任。然而&#xff0c;SOC分析师往往人手不足&#xff0c;工作繁重。生成式人工智能(GenAI)的出现为缓解这一困境带来了希望&#xff0c;使…

python:NP28---密码游戏

文章目录 前言一、题意描述输入描述&#xff1a;输出描述&#xff1a; 二、代码1.代码的实现2.读入数据 总结 前言 在python基础知识的学习中&#xff0c;我们很多时候会遇见让我们把数字拆分成各个位数的题&#xff0c;下面这道就是经典的数字拆分的l例题 一、题意 描述 牛…

工具篇--从零开始学Git

一、git概述 1.1版本控制方式 集中式版本控制工具 集中式版本控制工具&#xff0c;版本库是集中存放在中央服务器的&#xff0c; team 里每个人 work 时从中央服务器下载代码&#xff0c;是必须联网才能工作&#xff0c; 局域网或互联网&#xff0c;个人修改之…

IAB视频广告标准《数字视频和有线电视广告格式指南》之 简介、目录及视频配套广告 - 我为什么要翻译介绍美国人工智能科技公司IAB系列(2)

写在前面 谈及到中国企业走入国际市场&#xff0c;拓展海外营销渠道的时候&#xff0c;如果单纯依靠一个小公司去国外做广告&#xff0c;拉渠道&#xff0c;找代理公司&#xff0c;从售前到售后&#xff0c;都是非常不现实的。我们可以回想一下40年前&#xff0c;30年前&#x…

【学习记录】调试千寻服务+DTU+导远RTK过程的记录

最近调试车载定位的时候&#xff0c;遇到了一些问题&#xff0c;千寻服务已经正确配置到RTK里面了&#xff0c;但是导远的定位设备一直显示RTK浮动解&#xff0c;通过千寻服务后台查看状态&#xff0c;长时间显示不合法的GGA值。 首先&#xff0c;通过四处查资料&#xff0c;千…

嵌入式面经-ARM体系架构-计算机基础

嵌入式系统分层 操作系统的作用&#xff1a;向下管理硬件&#xff0c;向上提供接口&#xff08;API&#xff09; 应用开发&#xff1a;使用操作系统提供的接口&#xff08;API&#xff09;&#xff0c;做上层的应用程序开发&#xff0c;基本不用去关内核操作硬件是怎么实现的 …

网络架构层_服务器上下行宽带

网络架构层_服务器上下行宽带 解释一 云服务器ECS网络带宽的概念、计费、安全及使用限制_云服务器 ECS(ECS)-阿里云帮助中心 网络带宽是指在单位时间&#xff08;一般指的是1秒钟&#xff09;内能传输的数据量&#xff0c;带宽数值越大表示传输能力越强&#xff0c;即在单位…

蓝桥杯第十一届电子类单片机组程序设计

目录 前言 单片机资源数据包_2023&#xff08;点击下载&#xff09; 一、第十一届比赛原题 1.比赛题目 2.赛题解读 1&#xff09;计数功能 2&#xff09;连续按下无效按键 二、部分功能实现 1.计数功能的实现 2.连续按下无效按键的处理 3.其他处理 1&#xff09;对于…

verilog中,何时用reg和wire

何时用&#xff1f; 组合逻辑用wire&#xff0c;时序逻辑用reg。 reg 可以存储数据&#xff0c;wire则就是一根线&#xff0c;只能传递数据。 比如&#xff1f; 时序逻辑 always ( posedge clk or negedge rst_n) begin// big river goes to the east endinitial begin//…

每天一个数据分析题(二百一十一)

关于优化模型的3个组成部分&#xff0c;下列描述不正确的是&#xff08;&#xff09; A. 目标函数&#xff1a;要优化的函数&#xff08;目标函数中不包含决策变量&#xff09; B. 目标函数&#xff1a;要优化的函数&#xff08;最大化或最小化&#xff09; C. 决策变量&…

CentOS7.9 安装SIPp3.6

epel里面的SIPp版本比较旧&#xff0c;先不要epel yum remove -y epel-release 编辑 /etc/yum.repos.d/okay.repo&#xff0c;内容为&#xff1a; [okay] nameExtra OKay Packages for Enterprise Linux - $basearch baseurlhttp://repo.okay.com.mx/centos/$releasever/$basea…

栈的顺序存储结构的构建(C++)+ 两栈共享空间

栈顶指针为-1的时候代表栈为空 栈的类定义 &#xff1a; const int N 1000; class SStack{public:SStack();//构造空栈~SStack();//析构函数void push(int x);//入栈int pop();//出栈int getTop();//取栈顶元素bool empty();//判空private:int data[N];//存放栈元素的数组in…

SQLiteC/C++接口详细介绍之sqlite3类(八)

返回目录&#xff1a;SQLite—免费开源数据库系列文章目录 上一篇&#xff1a;SQLiteC/C接口详细介绍之sqlite3类&#xff08;七&#xff09; 下一篇&#xff1a; SQLiteC/C接口详细介绍之sqlite3类&#xff08;八&#xff09;&#xff08;暂未发表&#xff09; 24.sqlite3_cr…

OSCP靶场--BlackGate

OSCP靶场–BlackGate 考点(1.redis rce 2. CVE-2021-4034提权) 1.nmap扫描 ┌──(root㉿kali)-[~/Desktop] └─# nmap -sV -sC -p- 192.168.163.176 --min-rate 2500 Starting Nmap 7.92 ( https://nmap.org ) at 2024-03-14 03:32 EDT Nmap scan report for 192.168.163.…

牛-迈面试题----答案/类似题/知识点

来源在这里 来源在这里 1.Redis的优势 (1) 速度快&#xff0c;因为数据存在内存中&#xff0c;类似于HashMap&#xff0c;HashMap的优势就是查找和操作的时间复杂度都很低 (2)支持丰富数据类型&#xff0c;支持string&#xff0c;list&#xff0c;set&#xff0c;sorted set&…

docker部署keepalived(搭建keepalived)

1&#xff1a;拉去镜像osixia/keepalived [rootlocalhost ~]# docker pull osixia/keepalived Using default tag: latest latest: Pulling from osixia/keepalived 89d9c30c1d48: Pull complete c9ada908efbe: Pull complete 4a9101e4ada9: Pull complete b428f77d2dc4: P…

前端基础篇-深入了解用 HTML 与 CSS 实现标题排版

&#x1f525;博客主页&#xff1a; 【小扳_-CSDN博客】 ❤感谢大家点赞&#x1f44d;收藏⭐评论✍ 文章目录 1.0 HTML 与 CSS 概述 2.0 HTML - 标题排版 2.1 图片标签 2.2 标题标签 2.3 水平标签 2.4 实现标题排版 3.0 HTML - 标题样式(style 样式) 3.1 CSS 的引入方式 3.2…

《系统架构设计师教程(第2版)》第6章-数据库设计基础知识-04-应用程序与数据库的交互

文章目录 1. 库函数级别访问接口2. 嵌入SQL访问接口2.1 概述2.2 数据库厂商 3. 通用数据接口标准3.1 开放数据库连接 (ODBC)3.2 一些数据库接口1&#xff09;数据库访问对象 (DAO)2&#xff09;远程数据库对象 (RDO)3&#xff09;ActiveX数据对象 (ADO)4&#xff09;Java数据库…

【科普文】三种处理器:CPU,GPU,NPU到底是什么?

更多内容在 从最早的CPU到GPU, 再到现在的NPU。处理器的型号和种类越来越多。每种处理器适用与不同的产品以及功能。下面来介绍三种处理器的区别和应用场景 CPU (中央处理器) CPU是计算机系统的核心&#xff0c;负责执行程序指令、处理数据和控制其他硬件设备。它通常由控制单…

Ollama管理本地开源大模型,用Open WebUI访问Ollama接口

现在开源大模型一个接一个的&#xff0c;而且各个都说自己的性能非常厉害&#xff0c;但是对于我们这些使用者&#xff0c;用起来就比较尴尬了。因为一个模型一个调用的方式&#xff0c;先得下载模型&#xff0c;下完模型&#xff0c;写加载代码&#xff0c;麻烦得很。 对于程…