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

Java 下载压缩zip

Java压缩zip

 /*** 下载压缩包** @param instId   实例id* @param response 响应* @author 梁伟浩* @date 2023-08-21*/@GetMapping("/downloadZip")@ApiOperation(value = "下载压缩包")@ApiImplicitParam(name = "instId", value = "实例id", required = true, paramType = "query", dataTypeClass = String.class)public void downloadZip(@RequestParam("instId") String instId, HttpServletResponse response) {Asserts.isEmpty(instId, "实例id[instId]不能为空");InputStream inputStream = null;OutputStream outputStream = null;try {businessContentService.zipAttachTree(instId,response);outputStream.flush();} catch (Exception e) {response.setStatus(SC_BAD_REQUEST);try {if (outputStream != null) {response.setCharacterEncoding("utf-8");response.setHeader("Content-Type", "application/json;charset=utf-8");outputStream.write(JSON.toJSONString(R.fail(e.getMessage())).getBytes());}} catch (IOException ex) {throw new RuntimeException(ex);}} finally {try {if (outputStream != null) {outputStream.close();}} catch (IOException ex) {throw new RuntimeException(ex);}try {if (inputStream != null) {inputStream.close();}} catch (IOException ex) {throw new RuntimeException(ex);}}}
 @Overridepublic void zipAttachTree(String instId, HttpServletResponse response) {InstBusinessInfo instBusinessInfo = instBusinessInfoMapper.selectById(instId);String instTitle = instBusinessInfo.getInstTitle();if (instBusinessInfo == null) {throw new BusinessException(instId);}//获取附件树目录BusinessContentRequest instanceContentRequest = new BusinessContentRequest();BusinessContentResponse contentResponse = null;instanceContentRequest.setInstId(instId);instanceContentRequest.setGroupEnum(BusinessContentGroupEnum.DIR_TREE);try {contentResponse = this.getBusinessContent(instanceContentRequest);} catch (Exception e) {e.printStackTrace();}List<BusinessContentVO> contentVOList = contentResponse.getContentVOList();File directory = new File("./" + instTitle);directory.mkdir();//递归创建文件夹并把文件流放进对应文件夹this.getFileName(contentVOList, new StringBuilder(), instTitle);File file = new File("./" + instTitle);File zipFile = null;List<File> souceFileList = new ArrayList();try {souceFileList.add(file);zipFile = new File(instTitle + ".zip");ZipUtil.toZip(zipFile.getName(), souceFileList);BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFile));byte[] buffer = new byte[fis.available()];fis.read(buffer);fis.close();// 清空responseresponse.reset();response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8"));OutputStream toClient = new BufferedOutputStream(response.getOutputStream());toClient.write(buffer);toClient.flush();toClient.close();} catch (Exception e) {e.printStackTrace();} finally {//删除本地生成的文件夹与zip压缩包if (file!=null && file.exists()){FileUtil.del(file);}if (zipFile != null && zipFile.exists()) {zipFile.delete();}}}//递归生成文件夹并把流写到对应文件夹public void getFileName(List<BusinessContentVO> contentVOList, StringBuilder sb, String instTitle) {List<StringBuilder> fileNames = new ArrayList<>();for (BusinessContentVO contentVO : contentVOList) {OutputStream outputStream = null;StringBuilder newSb = new StringBuilder(sb); // 创建新的 StringBuilder 对象if (Func.isEmpty(contentVO.getDataId())) {File directory = new File("./" + instTitle + "/" + contentVO.getNodeName());directory.mkdir();newSb.append(contentVO.getNodeName() + "/");} else {String dataId = contentVO.getDataId();InstFileInfo instFileInfo = instFileInfoMapper.selectById(dataId);InputStream inputStream = minioTemplate.getObject(instFileInfo.getFilePath());String fileName = "./" + instTitle + "/" + sb + "/" + contentVO.getNodeName() + "";File outputFile = new File(fileName);try {//把文件夹创建成输出文件outputStream = new FileOutputStream(outputFile);byte[] buffer = new byte[1024];int length;//把文件写到对应的文件夹中while ((length = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, length);}inputStream.close();outputStream.close();} catch (Exception e) {e.printStackTrace();}}List<BusinessContentVO> childrenList = contentVO.getChildren();if (childrenList != null) {getFileName(childrenList, newSb, instTitle); // 递归调用时使用新的 StringBuilder 对象}}}

将流写到文件中

 MultipartFile fileInputStream is = file.getInputStream();ZipInputStream zipInputStream = new ZipInputStream(is, Charset.forName("UTF-8"));File jsonFile = new File("./pre/" + zipEntryNameStr);this.writeFile(jsonFile.getAbsolutePath(), zipInputStream);/*** @描述 将流写到文件中* @作者 吕嘉伟* @日期 2023/3/30 17:49*/public void writeFile(String filePath, ZipInputStream zipInputStream) {try (OutputStream outputStream = new FileOutputStream(filePath)) {byte[] bytes = new byte[4096];int len;while ((len = zipInputStream.read(bytes)) != -1) {outputStream.write(bytes, 0, len);}} catch (IOException ex) {System.out.println("解压文件时,写出到文件出错");}}
http://www.lryc.cn/news/133970.html

相关文章:

  • GTK3实现自定义列表
  • Go语言基础之数组
  • 信息安全从业者考试认证大全
  • 详解react 15~18新增特性
  • SpringBoot整合FFmpeg进行视频分片上传(Linux)
  • eNSP综合小实验:VRRP、MSTP、Eth-Trunk、NAT、DHCP等技术应用
  • 正中优配:尾盘拉升的股票第二天的走势?
  • ios小组件报错:Please adopt containerBackground API
  • 基于AWS的3D模型搜索服务实现
  • pycharm远程连接docker容器
  • 开源全球地理空间数据可视化框架——Cesium学习(2023.8.21)
  • RT-Thread学习日记——点亮LED
  • 粘包问题(TCP面向字节流批量发送数据导致)
  • selenium Chrome驱动下载地址
  • Linux命令200例:tar命令主要用于创建、查看和提取归档文件(常用)
  • 【Django】Task4 序列化及其高级使用、ModelViewSet
  • FFMPEG RTMP流打开速度慢优化方法一
  • NextJs - Middleware(中间件)
  • 记录几个Hudi Flink使用问题及解决方法
  • Go:测试框架GoConvey 简介
  • JavaWeb-特殊文件(propertis与XML)
  • ffmpeg合并mp4视频文件
  • ATF BL1/BL2 ufs_read_blocks/ufs_write_blocks使用分析
  • Elasticsearch(十二)搜索---搜索匹配功能③--布尔查询及filter查询原理
  • 解决Windows下的docker desktop无法启动问题
  • LLM生成式 AI 项目生命周期Generative AI project lifecycle
  • java高并发系列 - 第13天:JUC中的Condition对象
  • 【TTY子系统】printf与printk深入驱动解析
  • 无涯教程-PHP - 全局变量函数
  • shell脚本之循环语句