文件及文件夹 压缩 下载

news/2024/10/18 18:26:58/

使用压缩下载需要用到两个工具类 ZipUtil ,IOUtil 直接导入即可

1.方法

//附件下载@Override@Transactionalpublic Result<Object> download(HttpServletRequest request, HttpServletResponse response, String id) {Result<Object> result = new Result<>();//文件保存路径String savaPath = "URL";//通过ZipUtil将文件夹压缩File zip = ZipUtil.zip(savaPath);//在当前目录下保存ZipUtil.unzip("URL");//压缩包存放路径String zipPath = "URL";//压缩包名称String zipName= id+".zip";File file = new File(zipPath+"/"+zipName);InputStream fis = null;try {fis = new FileInputStream(file);response.reset();response.setCharacterEncoding("UTF-8");response.setContentType("application/force-download");// 设置强制下载不打开response.addHeader("Content-Disposition","attachment;filename=" + new String(zipName.getBytes("utf-8"), "iso8859-1"));response.setHeader("Content-Length", String.valueOf(file.length()));byte[] b = new byte[1024];int len;while ((len = fis.read(b)) != -1) {response.getOutputStream().write(b, 0, len);}response.flushBuffer();fis.close();result.setStatus(1);result.setMessage("下载完成");} catch (IOException e) {result.setStatus(2);result.setMessage("下载失败");return  result;}return result;}

2.ZipUtil

//ZipUtil
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {private ZipUtil() {}public static File zip(String filePath) {File target = null;File source = new File(filePath);if (source.exists()) {// 压缩文件名=源文件名.zipString zipName = source.getName() + ".zip";target = new File(source.getParent(), zipName);if (target.exists()) {target.delete(); // 删除旧的文件}FileOutputStream fos = null;ZipOutputStream zos = null;try {fos = new FileOutputStream(target);zos = new ZipOutputStream(new BufferedOutputStream(fos));// 添加对应的文件EntryaddEntry("/", source, zos);} catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zos, fos);}}return target;}private static void addEntry(String base, File source, ZipOutputStream zos)throws IOException {// 按目录分级,形如:/aaa/bbb.txtString entry = base + source.getName();if (source.isDirectory()) {for (File file : source.listFiles()) {// 递归列出目录下的所有文件,添加文件EntryaddEntry(entry + "/", file, zos);}} else {FileInputStream fis = null;BufferedInputStream bis = null;try {byte[] buffer = new byte[1024 * 10];fis = new FileInputStream(source);bis = new BufferedInputStream(fis, buffer.length);int read = 0;zos.putNextEntry(new ZipEntry(entry));while ((read = bis.read(buffer, 0, buffer.length)) != -1) {zos.write(buffer, 0, read);}zos.closeEntry();} finally {IOUtil.closeQuietly(bis, fis);}}}public static void unzip(String filePath) {File source = new File(filePath);if (source.exists()) {ZipInputStream zis = null;BufferedOutputStream bos = null;try {zis = new ZipInputStream(new FileInputStream(source));ZipEntry entry = null;while ((entry = zis.getNextEntry()) != null&& !entry.isDirectory()) {File target = new File(source.getParent(), entry.getName());if (!target.getParentFile().exists()) {// 创建文件父目录target.getParentFile().mkdirs();}// 写入文件bos = new BufferedOutputStream(new FileOutputStream(target));int read = 0;byte[] buffer = new byte[1024 * 10];while ((read = zis.read(buffer, 0, buffer.length)) != -1) {bos.write(buffer, 0, read);}bos.flush();}zis.closeEntry();} catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zis, bos);}}}//测试public static void main(String[] args) {String targetPath = "E:\\cesi";File file = ZipUtil.zip(targetPath);System.out.println(file);ZipUtil.unzip("E:\\cesi.zip");}
}

3.IOUtil

//IOUtil 
import java.io.Closeable;
import java.io.IOException;
public class IOUtil {public static void close(Closeable... closeables) throws IOException {if (closeables != null) {for (Closeable closeable : closeables) {if (closeable != null) {closeable.close();}}}}public static void closeQuietly(Closeable... closeables) {try {close(closeables);} catch (IOException e) {}}
}

http://www.ppmy.cn/news/901120.html

相关文章

多层、六边形、洋葱和clean架构简介

想想回到过去的美好时光&#xff0c;根本没有所谓架构&#xff0c;那些日子是多么幸福啊&#xff0c;只有了解 GoF 模式&#xff0c;你就能称自己为架构师。 然而&#xff0c;计算机变得更加强大&#xff0c;用户的需求增加&#xff0c;导致应用程序的复杂性增加。 开发人员解决…

Bandizip Archiver——新秀解压压缩工具!

电脑装机必备软件当中&#xff0c;有一款你一定必选的&#xff0c;那就是解压压缩软件。WinRAR 是很多人默认的选择&#xff0c;但是正版 WinRAR 含广告&#xff0c;且解压速度并不是特别快。Bandizip 作为一款新秀在一些细节上赢得了用户的心。 细节一、未解压快速预览 在一…

【文件压缩解压工具类-含密码】

文件压缩解压工具类-含密码 一、zip4j简介二、zip4j工具类使用步骤1.添加maven依赖2.工具类代码3.调用测试 三、结语 一、zip4j简介 zip4j功能比较强大&#xff0c;支持加密、解密压缩&#xff0c;支持文件的添加、删除等。 zip4j官网&#xff1a;zip4j官网 - 可以在"dow…

文件压缩工具类

包含文件压缩, 解压, 字节数据输出流转base64 package com.vxdata.activity.utils;import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils;import java.io.*; import java.util.Base64; import java.util.…

ZipArchive压缩文件夹[转载]

原本地址&#xff1a;https://blog.yayuanzi.com/9600.html 找了好久&#xff0c;终于找到个博客有说这个的&#xff0c;具体的请看下面的代码 PHP中有个解压缩的扩展库ZipArchive()&#xff0c;可以用来实现解压缩的功能。当我使用ZipArchive做一个压缩文件夹及子文件夹的功…

Bandizip 7.16和winRAR在文件右键压缩功能上的差异

Bandizip 7.16和winRAR在文件右键压缩功能上的差异 (1)winRAR右键对一个包含若干文件的文件夹&#xff08;假设文件夹名字为test&#xff09;进行压缩&#xff0c;无论是采用“添加到压缩文件夹”的方式&#xff0c;还是采用“test.rar”的方式&#xff0c;最终得到的压缩文件…

使用john爆破压缩文件

使用hashcat爆破rar文件跳转 第一步&#xff1a;生成哈希文件 第二步&#xff1a;用字典破解

好看又实用的压缩文件管理器——Bandizip

好看又实用的压缩文件管理器——Bandizip ​“ 精而简&#xff0c;小而美。” 首批次分享的系列为——实用软件工具系列&#xff0c;本期分享软件Bandizip。 简单的了解 Bandizip 是一款压缩软件&#xff0c;它支持WinZip、7-Zip 和 WinRAR 以及其它压缩格式。它拥有非常快…