此功能背景是批量生成一批二维码,打成压缩包并下载

java批量对文件打压缩包,此处用的是文件流,不用创建文件,读取文件
public void exportZip(CodePlateDeviceQueryDto queryDto, OutputStream out) throws IOException {
    List<CodePlateDeviceListDto> list = codePlateDeviceMapper.selectDevNo(queryDto);
    ZipOutputStream zout = new ZipOutputStream(out);
    for (CodePlateDeviceListDto dto : list) {
        BufferedImage bufferedImage = CreateCodeUtil.encode(dto.getQrUrl(), "", dto.getDevNo(), "");
        String name = String.format("images/%s.png", dto.getDevNo());
        ZipEntry ze = new ZipEntry(name);
        zout.putNextEntry(ze);
        ImageIO.write(bufferedImage, ImgUtil.IMAGE_TYPE_PNG, zout);
        zout.closeEntry();
    }
    zout.flush();
    zout.close();
}
原始方式把文件打入压缩包的方式
//临时文件tmpFile1 tmpFile2

//OutputStream out = null; 这个是响应流

List<File> fileList = CollUtil.newArrayList();
fileList.add(tmpFile1);
fileList.add(tmpFile2);
File tmpZipFile = File.createTempFile(queryDto.getBatchNo(), ".zip");
ZipUtil.zip(tmpZipFile, false, fileList.toArray(new File[fileList.size()]));

//删除生成的临时的jpg文件
for (File file : fileList) {
    file.delete();
}

try (FileInputStream fileInputStream = new FileInputStream(tmpZipFile)) {
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = fileInputStream.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
} catch (IOException e) {
    throw new RuntimeException(e);
}
//删除临时的zip文件
tmpZipFile.delete();
实战案列
private String handlerExcelToZipFile() throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ZipOutputStream zout = new ZipOutputStream(out);
        for (String businessType : BUSINESS_TYPE_LIST) {
            OrgAccountFreezeInfoHkRept orgAccountFreezeInfoHkRept = new OrgAccountFreezeInfoHkRept();
            List<OrgAccountFreezeDto> freezeDtoList = orgAccountFreezeMapper.exportOrgAccountFreezeToHkExcel(businessType);
            orgAccountFreezeInfoHkRept.setOrgAccountFreezeDto(freezeDtoList);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            try {
                //处理Excel,到输出流bout,正常情况下我们会把流写到response里面去
                viewExportProcessor.exportExcel(orgAccountFreezeInfoHkRept, bout);

                //把多个文件写入到一个压缩包
                String name = String.format("file/%s.xlsx", BusinessTypeEnum.get(businessType).getDesc());
                ZipEntry ze = new ZipEntry(name);
                zout.putNextEntry(ze);
                IOUtils.write(bout.toByteArray(), zout);
                zout.closeEntry();
            } catch (Exception e) {
                Log.error(Thread.currentThread().getClass().getSimpleName() + "写入Excel文件错误", e);
            } finally {
                IOKits.closeQuietly(bout);
            }
        }
        zout.flush();
        zout.close();
        
        //发生流数据到fastdfs,这个根据各自实际情况来
        FileDataRequest request = new FileDataRequest();
        request.setFileName("账户冻结数据统计.zip");
        request.setFileData(out.toByteArray());
        FeignResponse<String> feignResponse = fastDFSFeign.uploadStream(request);
        Log.info("账户冻结数据统计上传fastdfs,返回结果:%s", JSON.toJSONString(feignResponse));
        out.close();
        return feignResponse.getData();
    }