Crystal Reports for Eclipse Developer Guide

Sample MyLocationInfo class

This example shows a class that copies a report from a custom location to a temporary file that can be used with the Crystal Reports editor. In this example, the custom location is a compressed archive file located on disk.
Example: 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class MyLocationInfo
{
  File tempFile = null;
  private final File zipFile;
  private String reportName = null;

  public MyLocationInfo()
  {
    this.zipFile = new File ("C:\\zippedReport.zip");
  }

  public File getTempFile() throws IOException
  {
    if (tempFile == null)
      convertReportToTempFile();
    return tempFile;
  }

  public void convertReportToTempFile() throws IOException
  {
    ZipFile z = new ZipFile(zipFile, ZipFile.OPEN_READ);
    try
    {
      reportName = calculateReportName();
      ZipEntry entry = z.getEntry(reportName);
      tempFile = File.createTempFile(reportName, ".rpt");
      tempFile.deleteOnExit();
      BufferedInputStream is = new BufferedInputStream(z.getInputStream(entry));
      BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(tempFile), 4096);
      writeStream(is, dest);
      dest.flush();
      dest.close();
      is.close();
    }
    finally
    {
      z.close();
    }
  }

  public String calculateReportName() throws ZipException, IOException
  {
    ZipFile temp = new ZipFile (zipFile, ZipFile.OPEN_READ);
    try
    {
      Enumeration <? extends ZipEntry> zipFileEntries = temp.entries();
      while (zipFileEntries.hasMoreElements())
      {
        ZipEntry entry = zipFileEntries.nextElement();
        String name = entry.getName();
        if (!entry.isDirectory() && name.endsWith (".rpt"))
          return name;
      }
      return null;
     }
     finally
     {
        temp.close();
     }
  }

  public void copyTempFileToCustomLocation() throws IOException
  {
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
    out.setLevel(Deflater.DEFAULT_COMPRESSION);
    try
    {
       FileInputStream in = new FileInputStream(getTempFile());
       try
       {
         out.putNextEntry(new ZipEntry(reportName));
         try
         {
           writeStream(in, out);
         }
         finally
         {
           out.closeEntry();
         }
       }
       finally
       {
          in.close();
       }
     }
     finally
     {
        out.close();
     }
  }

  private static final void writeStream(InputStream in, OutputStream out) throws IOException
  {
    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    while (-1 != (bytesRead = in.read(buffer, 0, buffer.length)))
    {
       out.write(buffer, 0, bytesRead);
    }
  }  
}    



SAP BusinessObjects
http://www.sap.com/sapbusinessobjects/
Support services
http://service.sap.com/bosap-support/
Product Documentation on the Web
http://help.sap.com/