使用Java将多个pdf压缩为单个文件zip文件

时间:2017-10-04 08:50:39

标签: java pdf zip

我能够生成pdf并将其刷新到浏览器。但是,现在我的要求发生了变化。我需要生成多个pdf并将它们保存在一个zip文件中并将其刷新到浏览器。我遵循了http://www.avajava.com/tutorials/lessons/how-can-i-create-a-zip-file-from-a-set-of-files.html

但是找不到如何集成我的代码。这是我的代码。任何想法都将不胜感激。

for(int i = 0; i < 5 ; i++) {
            byte[] documentBytes =  TSService.generateDocument(dealKey, i);
            String documentType = TSUtil.getDocumentType(i);
            response.setHeader("Content-Disposition", "attachment;filename="+documentType);
            response.setContentType("application/pdf");
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
            response.setHeader("Pragma", "public");
            response.setContentLength(documentBytes.length); 
            ServletOutputStream out = response.getOutputStream();
            out.write(documentBytes);       
            out.flush();
            out.close();
        }

最初我只有循环代码。现在,我想基于i值生成5个报告。

更新了Alex

的代码
String documentType = TSUtil.getDocumentType(Integer.valueOf(documentKey));
        response.setHeader("Content-Disposition", "attachment;filename=dd.zip");
        response.setContentType("application/zip");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
        response.setHeader("Pragma", "public");

        ServletOutputStream out = response.getOutputStream();
        ZipOutputStream zout = new ZipOutputStream(out);

        for(int i = 1; i <= 5 ; i++) {
            byte[] documentBytes =  TSService.generateDocument(dealKey, i);
            ZipEntry zip = new ZipEntry(i+".pdf");
            zout.putNextEntry(zip);
            zout.write(documentBytes);
            zout.closeEntry();
        }

        zout.close();

2 个答案:

答案 0 :(得分:2)

下面的代码应该可以工作,可以直接下载而无需创建任何临时文件。所有这些都是在运行中创建的,并且都在内存中。

response.setHeader("Content-Disposition", "attachment;filename="+***Your zip filename***);
response.setContentType("application/zip");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
response.setHeader("Pragma", "public");

ServletOutputStream out = response.getOutputStream();
ZipOutputStream zout = new ZipOutputStream(out);

for(int i = 0; i < 5 ; i++) {
    byte[] documentBytes =  TSService.generateDocument(dealKey, i);
    ZipEntry zip = new ZipEntry(***your pdf name***);
    zout.putNextEntry(zip);
    zout.write(documentBytes);
    zout.closeEntry();
}

zout.close();

<强>已更新

我刚试过以下代码而没有问题。可以创建一个包含5个文本文件的新zip文件。所以我不知道你为什么会得到例外。

    FileOutputStream out = new FileOutputStream("abc.zip");
    ZipOutputStream zout = new ZipOutputStream(out);

    for(int i = 0; i < 5 ; i++) {
        byte[] documentBytes =  "12345".getBytes();
        ZipEntry zip = new ZipEntry(i+".txt");
        zout.putNextEntry(zip);
        zout.write(documentBytes);
        zout.closeEntry();
    }

    zout.close();   

答案 1 :(得分:1)

我用xml文件完成了相同的任务。我是我的代码

public String createZipFromXmlFile(List<String> filePath) {
    String date = new SimpleDateFormat("yyyMMddHHmmssSS").format(new Date());
    String fName = "zipDownload" + date + ".zip";
    System.out.println(" File Path.................."+filePath);
    String fileName = filePath.get(0).substring(0, filePath.get(0).indexOf("xml")) + "//" + fName;
    try {
        FileOutputStream fout = new FileOutputStream(fileName);
        ZipOutputStream zout = new ZipOutputStream(fout);
        for (String fnm : filePath) {
            FileInputStream fin = new FileInputStream(new File(fnm));
            ZipEntry zip = new ZipEntry(fnm.substring(fnm.indexOf("xml")));
            zout.putNextEntry(zip);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = fin.read(bytes)) >= 0) {
                zout.write(bytes, 0, length);
            }
            zout.closeEntry();
            fin.close();
        }
        zout.close();
        fout.close();
    } catch (Exception e) {
    }
    return fileName;
}

其中filePath是包含xml文件路径的列表。