Example #1
0
  /** Create a zip file containing all the files (masterreport, subreports, images) */
  private void writeZip(final OutputStream outStream) throws Exception {
    File zipfile = null;
    final List<String> newImagesFilenames = new LinkedList<String>();
    final JasperReport[] jra = reportCard.getRichReportJRA();

    try {
      // create zip file
      zipfile = File.createTempFile("cmdbuild_report_export", ".zip");
      final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile));

      // get jasperdesign for master report
      final JasperReport masterReport = jra[0];
      final JasperDesign jd = jasperReportToJasperDesign(masterReport);

      // add images to zip file
      final List<JRDesignImage> designImages = getImages(jd);
      final InputStream[] isa = reportCard.getImagesISA();
      final String[] origImagesName =
          reportCard.getImagesName() != null
              ? reportCard.getImagesName()
              : new String[isa.length]; // original names with check for
      // backward compatibility
      for (int i = 0; i < isa.length; i++) {

        String imageFilename;
        if (origImagesName[i] != null) {
          imageFilename = origImagesName[i];
        } else { // backward compatibility
          imageFilename = "image" + (i + 1) + "." + getImageFormatName(isa[i]);
          origImagesName[i] = imageFilename;
        }

        // update image filename
        setImageFilename(designImages.get(i), imageFilename);

        // write image
        final InputStream is = isa[i];
        newImagesFilenames.add(imageFilename);
        zos.putNextEntry(new ZipEntry(imageFilename));
        for (int b = is.read(); b != -1; b = is.read()) {
          zos.write(b);
        }
        zos.closeEntry();
      }

      // add reports (jrxml) to zip file
      for (int i = 0; i < jra.length; i++) {

        // add ZIP entry to output stream.
        final String filename = jra[i].getName() + ".jrxml";
        try {
          zos.putNextEntry(new ZipEntry(filename));
        } catch (final ZipException e) {
          Log.REPORT.warn("error while zipping elements", e);
          continue;
        }

        // if master report, update images and subreports paths
        if (i == 0) {
          // update images
          prepareDesignImagesForZipExport(designImages, origImagesName);

          // update subreports
          prepareDesignSubreportsForZipExport(getSubreports(jd), jra);

          // compile updated jasperdesign
          final JasperReport newjr = JasperCompileManager.compileReport(jd);

          // replace jasperreport obj with the new one
          jra[0] = newjr;
        }

        // write to zip
        JRXmlWriter.writeReport(jra[i], zos, "UTF-8");

        // complete the entry
        zos.closeEntry();
      }
      // close zip stream
      zos.close();

      // send final zip to output stream
      final FileInputStream fis = new FileInputStream(zipfile);
      for (int b = fis.read(); b != -1; b = fis.read()) {
        outStream.write(b);
      }
      fis.close();

    } finally {
      // delete zip file
      if (zipfile != null && zipfile.exists()) {
        zipfile.delete();
      }
    }
  }