In this tutorial, we will explain how to create a TAR file in a PeopleSoft application by using PeopleCode and Java. There is no native support available in Java to create a TAR file, and to overcome this limitation, we will use JTAR library inside PeopleSoft and generate our TAR file. You should download a copy of JTAR (look into references below )for trying out the example provided in this section. The high level steps involved in creation of TAR file in PeopleSoft are captured below:
Steps to Create a TAR File in PeopleCode |
Note that this example covers the TAR operation for single file only. We will provide an example later that will explain how to TAR multiple files through PeopleCode. It is just a concept of extending the operation to recursively handle multiple files. With all this introduction, we are now ready to go through the step by step process of creating TAR files in PeopleSoft.
1. Prepare JTAR Library
JTAR library has only a handful of class files. Depending on your Java version, you may have to recompile the source files to support running this library in your environment. I tested this on a JDK 1.4 version, and have prepared the class files, in case you want to make use of them. You can download it from here. [JTAR is open source, but make sure you check the license terms in their website, before taking live your code. This tutorial is written for educational purpose only]
This is to avoid issues at runtime around version mismatches. Make sure you copy the class files to your application server / process scheduler class file directories. We will be creating Java objects to these class files via PeopleCode, and these files should be in your CLASSPATH for this to work properly.
2. Read Input Files to TAR
java.io.FileInputStream
through which, we can read the input file that we want to TAR. We will be reading the file byte by byte to create TAR file later in a loop. The PeopleCode snippet to do this is provided below: [You can use relative file path as opposed to absolute paths, depending on your requirement]/* Read source file into input stream */
Local string &fileNameToTar = "c:\test_tar_input.svg";
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToTar);
3. Create Target TAR File
In this step, we will use
org.xeustechnologies.jtar.TarOutputStream
and create a FileOutputStream
object, that will map to the physical TAR file on the disk. The TarOutputStream
class accepts an OutputStream
object in the constructor. The PeopleCode segment is shown below:/* Create Target TAR File */
Local JavaObject &Tar = CreateJavaObject("org.xeustechnologies.jtar.TarOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar", True));
4. Add TAR Entries
Every file that has to be a part of your TAR file, should have a TarEntry created for it. TAR entry is defined in
org.xeustechnologies.jtar.TarEntry
. You create a TarEntry
by passing the File to be TARed as an object of type java.io.File
and the file name as a string. Once a TarEntry is created, you can add the entry to your TAR file by using putNextEntry
method in TarOutputStream
class. The PeopleCode snippet to do this is shown below:/* For every File you want to add to TAR File, you have to create a TAR Entry */
Local JavaObject &TarEntry = CreateJavaObject("org.xeustechnologies.jtar.TarEntry", CreateJavaObject("java.io.File", &fileNameToTar), &fileNameToTar);
/* Add TAR Entry to TAR OutputStream file */
&Tar.putNextEntry(&TarEntry);
5. Copy Bytes to TAR File
Once you have added the TarEntry to your TAR file, you can declare a byte Java array in PeopleCode and read the input file byte by byte. For every set of bytes that is read, you can use the
write
method available in TarOutputStream
, to write the data that is read to the file. We have seen this in action in our gzip file creation tutorial earlier. We will reuse the same code segment here./* 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 of Data to TAR Output Stream */
&Tar.write(&filebuffer, 0, &byteCount);
&byteCount = &in.read(&filebuffer);
End-While;
You need to do this for every file, if you are planning to TAR multiple files through PeopleCode. This step adds the file to the output TAR file.
6. Close TAR File
Finally, you close the output stream / input stream objects using the close method. Your TAR file is now ready.
Create TAR File in PeopleCode – Complete Example
Putting it all together, the complete code segment to create a TAR file through PeopleCode is shown below.
MessageBox(0, "", 0, 0, "PeopleCode - Manipulating TAR Files - Example");
/* Read source file into input stream */
Local string &fileNameToTar = "c:\test_tar_input.svg";
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToTar);
/* Create Target TAR File */
Local JavaObject &Tar = CreateJavaObject("org.xeustechnologies.jtar.TarOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar", True));
/* For every File you want to add to TAR File, you have to create a TAR Entry */
Local JavaObject &TarEntry = CreateJavaObject("org.xeustechnologies.jtar.TarEntry", CreateJavaObject("java.io.File", &fileNameToTar), &fileNameToTar);
/* Add TAR Entry to TAR OutputStream file */
&Tar.putNextEntry(&TarEntry);
/* 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 of Data to TAR Output Stream */
&Tar.write(&filebuffer, 0, &byteCount);
&byteCount = &in.read(&filebuffer);
End-While;
/* Close input stream */
&in.close();
/* Close TarOutputStream*/
&Tar.close();
You can inject this segment into the any PeopleCode location of your choice. A sample TAR file created by this code is shown in the screenshot below:
Output TAR File Created in PeopleCode - Example |
Note that there is no compression for TAR files. You can GZIP the resulting TAR file using our tutorial earlier, if you want to GZIP the file. In the next tutorial, we will discuss how to add multiple files to a single TAR file with an example. We will also discuss extracting TAR files using PeopleCode shortly.
References:
JTar Documentation
ZIP File Creation in PeopleCode
References:
JTar Documentation
ZIP File Creation in PeopleCode
No comments:
Post a Comment