Create GZIP File in PeopleCode - Example

In this tutorial, we will discuss how to create a GZIP file through PeopleCode with an example code. You can compress only single files through GZIP normally and we will utilize the features provided by java.util.zip.GZIPOutputStream class, to achieve the .gz compression. I assume that you have a basic understanding of CreateJavaObject usage in PeopleCode. The different steps we have to write to create a .gz file in PeopleCode is provided below:

Steps to Create a GZip File in PeopleCode
Steps to Create a GZip File in PeopleCode
We will describe each of these steps in detail as we move on. You will be surprised to know that we don’t have to use any Java reflection techniques for this tutorial.


1. Read Input File in FileInputStream


In this step, we will read the input file (a text file for our example) into an InputStream object, java.io.FileInputStream. The PeopleCode construct is provided below:

/* Read source file into input stream */
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", "c:\test_file_to_gzip.txt");

This is a very simple construct and all you are doing here is to pass the complete path to FileInputStream class, and the name of the file that needs to be gzipped.


2. Create .gz GZIPOutputStream


Ok, we have an InputStream for reading. We now need an OutputStream, to which we can write the output .gz file. For this, we utilize the Java class java.util.zip.GZIPOutputStream.

You have to write the file contents into this stream, for getting the .gz compression. To create an object of type GZIPOutputStream, you need a OutputStream object. We can do this in PeopleCode as shown below:

/* Create Target Gzip File */
Local JavaObject &gzip = CreateJavaObject("java.util.zip.GZIPOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\test_file_to_gzip.gz", True));


3. Read Input File Contents into Byte Array


GZIPOutputStream class has a method write that can write an array of bytes to the compressed output stream. This is evident from the signature of the method, as shown below:

Signature of write method in GZIPOutputStream Class
Signature of write method in GZIPOutputStream Class
To pass an array of bytes for writing to the output, we create a byte array through PeopleCode, and set a pre-defined size of 1024 (length).

/* Java Array that will read bytes from input file */
Local JavaObject &filebuffer = CreateJavaArray("byte[]", 1024);



4. Write Input File to GZIPOutputStream


We now use the read method in InputStream class to read the input file contents to byte array. This method returns the number of bytes read, or a -1 if there is nothing more to read from input file. So, we iterate over this method again and again, until we have read all the bytes from the input file. Further, for every cluster of bytes read, we write it to the output stream, using the write method described earlier, in PeopleCode.

Local number &byteCount = &in.read(&filebuffer);
/* Read bytes from input file and load it byte array  - Do until all bytes are read*/
While &byteCount > 0
   /* Write bytes into Gzip stream */
   &gzip.write(&filebuffer, 0, &byteCount);
   &byteCount = &in.read(&filebuffer);
End-While;
/* Close input stream */


5. Close All Open Streams


We can close all open Input and Output streams at this step. The .gz file is now created in PeopleCode.

/* Close input stream */
&in.close();
/* Close GZIPOutputStream */
&gzip.close();


Create GZIP File in PeopleCode – Complete Program


The complete PeopleCode program segment to create a .gz (Gzip) file in PeopleCode is shown below:

MessageBox(0, "", 0, 0, "PeopleCode - Create GZip File Example");
/* Read source file into input stream */
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", "c:\test_file_to_gzip.txt");
/* Create Target Gzip File */
Local JavaObject &gzip = CreateJavaObject("java.util.zip.GZIPOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\test_file_to_gzip.gz", True));
/* Java Array that will read bytes from input file */
Local JavaObject &filebuffer = CreateJavaArray("byte[]", 1024);
Local number &byteCount = &in.read(&filebuffer);
/* Read bytes from input file and load it byte array  - Do until all bytes are read*/
While &byteCount > 0
   /* Write bytes into Gzip stream */
   &gzip.write(&filebuffer, 0, &byteCount);
   &byteCount = &in.read(&filebuffer);
End-While;
/* Close input stream */
&in.close();
/* Close GZIPOutputStream */
&gzip.close();


You can inject this code into the PeopleCode location of your choice.i.e. Application engine, Record field events etc.


An example txt file and the output gzip file created is shown in the screenshot below:

Example .GZ file created in PeopleSoft using PeopleCode
Example .GZ file created in PeopleSoft using PeopleCode
That completes a breezy tutorial to create gzip files in PeopleSoft, through PeopleCode and Java. You can even create ZIP files on the same approach by using different streams available in Java. Try your gzip code, and post a comment if something is broken.

No comments:

Post a Comment