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

批量导出pdf为zip文件(可以修改zip中pdf名称)

核心代码

  public static void compressZip1(HashMap<String,File> map, String rootPath, String zipFileName) throws FileNotFoundException {FileOutputStream fileOutputStream = new FileOutputStream(zipFileName);ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(rootPath));try {for (Map.Entry<String, File> entry : map.entrySet()) {String fileName = entry.getKey(); // 获取文件名File file = entry.getValue(); // 获取文件FileInputStream fileInputStream = new FileInputStream(file);zip1(fileInputStream, zipOutputStream, fileName, file);fileInputStream.close();}zipOutputStream.close();fileOutputStream.close();} catch (IOException e) {log.error("context", e);}finally {try {zipOutputStream.close();fileOutputStream.close();} catch (IOException e) {log.error("context", e);}}}private static void zip1(FileInputStream fileInputStream,ZipOutputStream zipOutputStream,  String fileName, File file) throws IOException {// 设置自定义pdf文件名String newFileName = fileName + file.getName();ZipEntry zipEntry = new ZipEntry(newFileName);zipOutputStream.putNextEntry(zipEntry);byte[] buffer = new byte[1024*5];BufferedInputStream bufferStream  = new BufferedInputStream(fileInputStream);int length;// 输入缓冲流int read = 0;
//        while ((length = fileInputStream.read(buffer)) > 0) {while ((read = bufferStream.read(buffer)) != -1) {zipOutputStream.write(buffer, 0, read);}zipOutputStream.closeEntry();bufferStream.close();}

注意!!!

这里需要使用BufferedInputStream,如果用的是FileInputStream 就算flush有时候还是会文件损坏,我没再深点测试这个为啥得BufferedInputStream

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

相关文章:

  • [国家集训队] Tree II 题解报告
  • 【redis】docker搭建redis集群
  • 前端个人年度工作述职报告(二十篇)
  • TypeScript 编译配置
  • 使用DMA传输实现单片机高效串口转发——以STM32系列为例
  • 一文了解 Android Auto 车载开发~
  • Pixel4 安卓源码及内核修改编译教程 | 基于Android12 AOSP
  • 如何做好Code Review
  • Unity技术框架集合、Unity技术栈汇总
  • 安卓SDK开发的一些疑问
  • 【基础类】—三栏页面布局的方案和优缺点
  • OPENCV C++(四)形态学操作+连通域统计
  • tomcat上部署jpress
  • 篇十:外观模式:简化复杂系统
  • linux gcc __attribute__
  • 【SpringCloud】RabbitMQ基础
  • css, resize 拖拉宽度
  • Python识别抖音Tiktok、巨量引擎滑块验证码识别
  • EvilBox One靶场笔记
  • shell脚本中的export无效
  • 前沿分享-鱼形机器人
  • 摄像机终端IP地址白名单配置流程
  • Glibc—查看版本
  • C++物理引擎Box2D的下载,编译,VS2013配置环境
  • STL容器详解——map容器
  • VR全景在建筑工程行业能起到哪些作用?
  • P1257 平面上的最接近点对
  • 8月1日上课内容 第一章web基础与http协议
  • Gson 添加数据默认值问题记录
  • 利用Arthas+APM监控进行Java性能深度定位