IO流:java实现-压缩包zip

public void batchPropertyDownloadPptTemp(String accountingYear, String accountingPeriod, HttpServletResponse response) {

        String targetPath = getPPTOutputName();
        String excelTargetPath = getExcelOutputName();
        File file1 = null;
        File file2 = null;
        try{
            OutputStream outputStream = response.getOutputStream();
            ZipArchiveOutputStream zous = new ZipArchiveOutputStream(outputStream);

            zous.setUseZip64(Zip64Mode.AsNeeded);
            log.info("输出路径:"+targetPath);
            log.info("生产PPT的版本为:"+accountingYear+"-"+accountingPeriod);
            pptService.processNewPPT(targetPath, accountingYear,accountingPeriod);
            file1 = new File(targetPath);
            InputStream inputStream1 = new FileInputStream(file1);
            byte[] bytes = null;
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int size;
            while ((size = inputStream1.read(buffer)) != -1) {
                baos1.write(buffer, 0, size);
            }
            bytes = baos1.toByteArray();
            ArchiveEntry entry1 = new ZipArchiveEntry(accountingYear + "年" + accountingPeriod+ "月_" + getToday() + ".pptx");
            zous.putArchiveEntry(entry1);
            zous.write(bytes);
            zous.closeArchiveEntry();

            pptExcelDataService.getPptExcel(excelTargetPath, accountingYear,accountingPeriod);
            file2 = new File(excelTargetPath);
            InputStream inputStream2 = new FileInputStream(file2);
            while ((size = inputStream2.read(buffer)) != -1) {
                baos2.write(buffer, 0, size);
            }
            bytes = baos2.toByteArray();
            ArchiveEntry entry2 = new ZipArchiveEntry(accountingYear + "年" + accountingPeriod+ "月_" + getToday() + ".xlsx");
            zous.putArchiveEntry(entry2);
            zous.write(bytes);
            zous.closeArchiveEntry();

            inputStream1.close();
            inputStream2.close();
            outputStream.flush();
            outputStream.close();
            baos1.close();
            baos2.close();
            zous.close();
        } catch (Exception e) {
            log.error("导出报表PPT异常:{[]}", e);
            throw new BusinessException("导出报表PPT异常");
        }finally {
            if (file1 != null) {
                if (file1.exists()) {
                    file1.delete();
                }
            }
            if (file2 != null) {
                if (file2.exists()) {
                    file2.delete();
                }
            }
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值