简体   繁体   中英

Zip multiple pdf into a single file zip file using Java

I'm able to generate a pdf and flush it to the browser. But, now my requirement is changed. I need to generate multiple pdf and keep them in a single zip file and flush it to the browser. I followed this http://www.avajava.com/tutorials/lessons/how-can-i-create-a-zip-file-from-a-set-of-files.html

But could not find how to integrate in my code. Here is my code. Any ideas would be greatly appreciated.

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();
        }

Initially I had only code which is in loop. Now, I want to generate 5 reports based on i value.

Updated code for 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();

Below code should be worked and can be directly downloaded without creating any temp files. All are created on the fly and are on memory.

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();

UPDATED

I have just tried the below code and without problem. A new zip file can be created with 5 text files inside. So I have no idea why you get exceptions.

    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();   

I had done same task with xml file.below is my code

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;
}

where filePath is list that contains path of xml files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM