2
zipfile = new ZipOutputStream(response.getOutputStream());
response.setHeader("Content-Disposition", "attachment; filename=\"" + "myReport.zip\";");
response.setContentType("application/zip");
List<JasperPrint> print2 = new ArrayList<JasperPrint>();
print2.add(print);
try {      
    for (int j = 0; j < 5; j++) {
    byte[] pdfAsBytes;   
    byte[] buffer = new byte[1024];
    pdfAsBytes =  JasperExportManager.exportReportToPdf(print2.get(j));
    ByteArrayInputStream fis = new ByteArrayInputStream(pdfAsBytes);
    zipfile.putNextEntry(new ZipEntry(print2.get(j).getName()+".pdf"));
    int length;
    while ((length = fis.read(buffer,0,1024)) > 0) {
        zipfile.write(buffer, 0, length);
    }
    zipfile.closeEntry();
    // close the InputStream
    fis.close();
}  
zipfile.close();

I am trying to zip multiple jasper reports. I can zip one report but not been able to zip multiple reports. I am using Java list to add same Jasperprint just to test, whether this can zip more then one report.

SujitKumar
  • 142
  • 1
  • 10
Khurram
  • 59
  • 1
  • 6
  • 1
    Could you print `print2.get(j).getName()+".pdf"` to verify each file name is unique? if there are more than one ZipEntry named the same, some of them will be overwritten when you unpacked the zip file . – Beck Yang Dec 31 '16 at 01:02
  • This solved the problem. Thankyou – Khurram Jan 05 '17 at 16:15

0 Answers0