是否可以创建 StreamedContent 列表?

Is it possible to create a StreamedContent List?

我需要创建一个 .pdf 文件列表并使用 org.primefaces.model.StreamedContent 将其全部打包到一个 zip 文件中。可能吗???这是我的总体思路:

    private StreamedContent fileDownload;
    private ArrayList<StreamedContent> filesDownload;
     for (Cobranca boleto : cob) {
        this.boletoPDF = null;
        TndFabricaBoletoWrapper wrapper = new TndFabricaBoletoWrapper();
    
        wrapper = this.cobrancaBusiness.processarBoleto(boleto, wrapper);
        wrapper.setDownload(true);
    
        JBoleto jBoleto = TndFabricaJBoleto.getJBoleto(wrapper);
        jBoleto.addBoleto();
    
        this.boletoPDF = jBoleto.closeBoleto();
    
        File anexo = File.createTempFile("Boleto", ".pdf");
        Arquivo.salvarArquivo(anexo, this.boletoPDF);
    
        byte[] arq = Arquivo.zipByte(anexo, 9);
    
        this.fileDownload = new DefaultStreamedContent(new ByteArrayInputStream(arq), "application/zip", "Boleto");
        this.filesDownload.add(this.fileDownload);
}
((DefaultStreamedContent) this.filesDownload).setName("Boleto" + ".zip"); //this is where the download action usually happens... But the (DefaultStreamdContent) cannot be cast to an ArrayList...

您需要做的是获取每个 PDF 内容的字节数组,将其添加到 zip 文件,然后生成该 Zip 文件的 StreamedContent。
假设您有一个包含所有 PDF 内容的列表,它可能是这样的:

public StreamedContent getZipDownload(List<byte[]> contentList){
    StreamedContent result = null;
    
    try {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zipOut = new ZipOutputStream(baos);
        for (int i = 0; i < contentList.size(); i++) {
            String name = "" + i + ".pdf";

            zipOut.putNextEntry(new ZipEntry(name));
            zipOut.write(contentList.get(i));
            zipOut.closeEntry();

        }

        zipOut.finish();
        zipOut.close();
        result = DefaultStreamedContent.builder()
                    .name("pdfFiles.zip")
                    .contentType("application/octet-stream")
                    .stream(() -> new ByteArrayInputStream(baos.toByteArray())).build();
    } catch (IOException e) {
        LOGGER.log(Level.WARNING, "Download failed, error in Zip Creation", e);
    }

    return result;
}