使用压缩下载需要用到两个工具类 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) {}}
}