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

java实现多文件打包压缩,导出zip文件

一.实现多文件打包压缩

    @Testpublic void testZipFile() throws IOException {String filePath = "D:\\导出压缩文件.zip";OutputStream outputStream = new FileOutputStream(filePath);try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {// excelzipOutputStream.putNextEntry(new ZipEntry("excel文件.xlsx"));byte[] excelFile = FileUtil.readBytes("D:\\excel文件.xlsx");zipOutputStream.write(excelFile);zipOutputStream.flush();zipOutputStream.closeEntry();// doczipOutputStream.putNextEntry(new ZipEntry("doc文件.docx"));byte[] docFile = FileUtil.readBytes("D:\\doc文件.docx");zipOutputStream.write(docFile);zipOutputStream.flush();zipOutputStream.closeEntry();// pdfzipOutputStream.putNextEntry(new ZipEntry("pdf文件.pdf"));byte[] pdfFile = FileUtil.readBytes("D:\\pdf文件.pdf");zipOutputStream.write(pdfFile);zipOutputStream.flush();zipOutputStream.closeEntry();// ...} catch (Exception e) {e.printStackTrace();}}

二.导出zip文件接口

    @RequestMapping("exportZipFile")public void exportZipFile(HttpServletResponse response) throws IOException {String zipName = "D:\\导出压缩文件.zip";response.setContentType("application/OCTET-STREAM;charset=UTF-8");response.setHeader("Content-Disposition", "attachment; filename="+ new String(zipName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));try (ServletOutputStream outputStream = response.getOutputStream();ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {// excelzipOutputStream.putNextEntry(new ZipEntry("excel文件.xlsx"));byte[] excelFile = FileUtil.readBytes("D:\\excel文件.xlsx");zipOutputStream.write(excelFile);zipOutputStream.flush();zipOutputStream.closeEntry();// doczipOutputStream.putNextEntry(new ZipEntry("doc文件.docx"));byte[] docFile = FileUtil.readBytes("D:\\doc文件.docx");zipOutputStream.write(docFile);zipOutputStream.flush();zipOutputStream.closeEntry();// pdfzipOutputStream.putNextEntry(new ZipEntry("pdf文件.pdf"));byte[] pdfFile = FileUtil.readBytes("D:\\pdf文件.pdf");zipOutputStream.write(pdfFile);zipOutputStream.flush();zipOutputStream.closeEntry();// ...} catch (Exception e) {e.printStackTrace();}}

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

相关文章:

  • java-枚举类的使用
  • Vue插槽
  • 学习c++的第二天
  • Android NDK开发详解之调试和性能分析的系统跟踪概览
  • AD9371 官方例程HDL JESD204B相关IP端口信号
  • 蓝牙服务:优化体验,提高连接效率
  • SSM校园设备管信息管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目
  • iOS的应用生命周期以及应用界面
  • Macos下安装使用Redis
  • Redis的四种部署方案
  • Microsoft Edge不能工作了,可能原因不少,那么如何修复呢
  • 算法---缺失的第一个正数
  • 【算法与数据结构】--算法应用--算法和数据结构的案例研究
  • java如何获取调用接口的ip?
  • ubuntu 18 更新git版本到 2.80.1
  • 测试C#调用Aplayer播放视频(2:VideoPlayer源码学习)
  • YOLOv5 分类模型的预处理
  • 25 行为型模式-备忘录模式
  • 物联网AI MicroPython传感器学习 之 SHT3X温湿度传感器
  • int* p = new int[5]; int *p = new int[5]();delete[] p; delete p;区别是什么?
  • 数据结构|基础知识定义
  • 物联网AI MicroPython传感器学习 之 MFRC522 RFID射频IC卡感应模块
  • 搭建ES集群
  • Tomcat的日志接收文件catalina.out nohup.out说明
  • 手机ip地址切换后有什么影响
  • C++ 赋值运算重载,const成员,取地址及const取地址操作符重载
  • 嵌入式Linux系统的闪存设备和文件系统学习纪要
  • android 8.1 disable unsupported sensor
  • 二、类与对象(一)
  • 写给所有的程序员,或者努力生活的你。