有关 java 压缩文件的两种解决方式

第一种 :是用项目的工作空间做为临时存储,优点可以节省内存空间,缺点就是可能没有权限操作项目路径。所以需要注意是否能操作项目路径。

    @PostMapping("downloadForZipInputStream")
    @ApiOperation("文件预览 返回 inputStream 流")
    public R<byte[]> downloadForZipInputStream(@RequestBody List<String> attachmentIds, HttpServletResponse response) {
        String uuId = UUID.randomUUID()+".zip";
        // 这一步是获取工作空间 为了避免压缩文件名称重复,所以用uuid来命名反正最后要删除的
        String location = System.getProperty("user.dir") +"/"+ uuId;
        try (FileOutputStream fileOutputStream = new FileOutputStream(location);
             ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);) {
            byte[] bufs = new byte[1024 * 10];
            setZipOutputStream(attachmentIds, zipOutputStream, bufs);
            return R.ok(bufs);
        } catch (IOException e) {
            log.error("downloadForZipInputStream Convert file to zipInputStream failed exception : {}", e);
            throw new ServiceException("返回inputStream失败");
        }finally {
            // 删除本地文件
            //下载完成,删掉zip包
            File fileTempZip = new File(location);
            fileTempZip.delete();
        }
    }


 /**
     * 获取文件信息,压缩文件,并写入压缩流
     * @param attachmentIds
     * @param zipOutputStream
     * @param bufs
     */
    private void setZipOutputStream(List<String> attachmentIds, ZipOutputStream zipOutputStream, byte[] bufs) {
        // 这是存贮 文件关系的表
        List<FileUpload> fileUploads = fileService.listByIds(attachmentIds);
        for (FileUpload fileInfo : fileUploads) {
            Assert.notNull(fileInfo, "该文件不存在");
            try (InputStream inputStream = uploadContext.download(fileInfo);// 获取minio中的流然后在操作
                 BufferedInputStream bis = new BufferedInputStream(inputStream, 1024 * 10)) {
                String streamfilename = fileInfo.getFilename();
                // 创建ZIP实体,并添加进压缩包 这个就是压缩过程了
                ZipEntry zipEntry = new ZipEntry(streamfilename);
                zipOutputStream.putNextEntry(zipEntry);
                int read = 0;
                while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                    zipOutputStream.write(bufs, 0, read);
                }
            } catch (Exception e) {
                log.error("downloadForZipInputStream Convert file to zipInputStream failed exception : {}", e);
                continue;
            }
        }
    }

第二种 :通过内存来压缩文件,缺点:如果有较大的文件需要压缩,对硬件有一定的需求。

    @PostMapping("minio/getZipInputStream")
    @ApiOperation("文件预览 返回 inputStream 流")
    public byte[] getZipInputStream(@RequestBody List<String> attachmentIds, HttpServletResponse response) {
        // 如果不需要返回前端可以这样写
        // ByteArrayOutputStream out = new ByteArrayOutputStream();
        // ZipOutputStream zipOutputStream= new ZipOutputStream(out);
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());) {
            byte[] bufs = new byte[1024 * 10];
            for (String attachmentId : attachmentIds) {
                FileUpload fileInfo = fileService.getById(attachmentId);
                Assert.notNull(fileInfo, "该文件不存在");
                try (InputStream inputStream = uploadContext.download(fileInfo);
                     BufferedInputStream bis = new BufferedInputStream(inputStream, 1024 * 10)) {
                    String streamfilename = fileInfo.getFilename();
                    // 创建ZIP实体,并添加进压缩包
                    ZipEntry zipEntry = new ZipEntry(streamfilename);
                    zipOutputStream.putNextEntry(zipEntry);
                    int read = 0;
                    while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) {
                        zipOutputStream.write(bufs, 0, read);
                    }
                } catch (Exception e) {
                    continue;
                }
            }
            return bufs;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

工作随手记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不求人€

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值