Extract TAR File PeopleCode Example

In this tutorial, we will explain how to extract a TAR file in PeopleCode using JTAR Java library. If you are looking to generate a TAR file in PeopleSoft, then you should refer to create TAR file tutorial. You can always run native UNIX commands to extract TAR file before processing them in PeopleCode, however a Java based solution is much cleaner and is independent of the operating system where your code runs. You can easily stick this PeopleCode to an Application engine program or even a record field event, depending on your requirements. The high level steps involved in the extraction of a TAR file in PeopleSoft are provided below:

Steps to Extract TAR Files in PeopleCode
Steps to Extract TAR Files in PeopleCode

1. Prepare JTAR library


Depending on the Java version you are using, you may have to recompile the JTAR library source code so that you can use it in PeopleCode. If you refer to my create TAR file tutorial, I would have provided a version for JDK 1.4 users to use JTAR. Otherwise, you can directly use the distribution, and make sure you download a copy of all class files and loaded them into your application server / process scheduler class path. When the PeopleCode runs, it will look for libraries to extract TAR files in the classpath location. If the file could not be found, then you will get an exception in your program. So, this step is very important.


2. Read TAR File in PeopleCode


The first step in the extraction of TAR file is to read it as an object of type TarInputStream in a JavaObject in PeopleCode. The constructor for TarInputStream accepts an object of type java.io.FileInputStream, which in turns points to the physical location of TAR file on the disk. The PeopleCode construct to access a TAR file into TarInputStream java object is shown below

Local JavaObject &TarFileReader = CreateJavaObject("org.xeustechnologies.jtar.TarInputStream", CreateJavaObject("java.io.FileInputStream", "c:\temp\myTarFile.tar"));


3.Extract Directories from TAR File


It is a good idea to extract all folders in the TAR file archive first, as it would create the base structure for extracting the files later. To extract folders, we create an infinite while loop that breaks when all the entries in the TAR file are read. Each entry in the TAR file is read by using getNextEntry method available in TarInputStream class. The getNextEntry method returns NULL when no more entries are available in the TAR file for reading. This can be used as a condition for us to break the infinite loop. We then use isDirectory method to check if the entry is a directory. If so, we create the directory physically on the disk by using mkdir method available in java.io.File. The PeopleCode snippet is provided below:

/* Create all directories first */
While True
   &TarEntry = &TarFileReader.getNextEntry();
   If &TarEntry = Null Then
      Break;
   Else
      If (&TarEntry.isDirectory()) Then
         Local JavaObject &dir = CreateJavaObject("java.io.File", "c:\temp\" | &TarEntry.getName());
         Local boolean &b = &dir.mkdir();
      End-If;
   End-If;
End-While;
&TarFileReader.close();


4.Extract Files into Folders


Using the same approach we used to extract and create directories, we can read non-directory (i.e. file) entries from TAR file now in PeopleCode. For every file entry that is read, a FileOutputStream object is constructed, for which we obtain the name of the file using getName method available in TarEntry class. We then read bytes from the tar archive for the file inside a while loop, and write this to the OutputStream. We have discussed this approach during our extract ZIP file PeopleCode tutorial, and we reuse the code here.

/* Now we handle files */
&TarFileReader = CreateJavaObject("org.xeustechnologies.jtar.TarInputStream", CreateJavaObject("java.io.FileInputStream", "c:\temp\myTarFile.tar"));
While True
   &TarEntry = &TarFileReader.getNextEntry();
   If &TarEntry = Null Then
      Break;
   Else
      If (&TarEntry.isDirectory()) Then
         /* do nothing */
      Else
         Local JavaObject &out = CreateJavaObject("java.io.FileOutputStream", "c:\temp\" | &TarEntry.getName(), True);
         &NumberOfBytes = &TarFileReader.read(&ByteBufferArray);
         While &NumberOfBytes > 0
            &out.write(&ByteBufferArray, 0, &NumberOfBytes);
            &NumberOfBytes = &TarFileReader.read(&ByteBufferArray);
         End-While;
      End-If;
   End-If;
End-While;


5.Close TAR File


As a last step, we close all open input streams in PeopleCode. This is shown below:

&TarFileReader.close();


Extract TAR File in PeopleCode – Complete Code Snippet


The complete PeopleCode example to extract a TAR file in PeopleSoft using PeopleCode is shown below.

/* Extract TAR Files in PeopleCode - Example */
Local JavaObject &TarFileReader = CreateJavaObject("org.xeustechnologies.jtar.TarInputStream", CreateJavaObject("java.io.FileInputStream", "c:\temp\myTarFile.tar"));
Local JavaObject &TarEntry;
Local JavaObject &ByteBufferArray = CreateJavaArray("byte[]", 1024);
Local number &NumberOfBytes;

/* Create all directories first */
While True
   &TarEntry = &TarFileReader.getNextEntry();
   If &TarEntry = Null Then
      Break;
   Else
      If (&TarEntry.isDirectory()) Then
         Local JavaObject &dir = CreateJavaObject("java.io.File", "c:\temp\" | &TarEntry.getName());
         Local boolean &b = &dir.mkdir();
      End-If;
   End-If;
End-While;
&TarFileReader.close();


/* Now we handle files */
&TarFileReader = CreateJavaObject("org.xeustechnologies.jtar.TarInputStream", CreateJavaObject("java.io.FileInputStream", "c:\temp\myTarFile.tar"));
While True
   &TarEntry = &TarFileReader.getNextEntry();
   If &TarEntry = Null Then
      Break;
   Else
      If (&TarEntry.isDirectory()) Then
         /* do nothing */
      Else
         Local JavaObject &out = CreateJavaObject("java.io.FileOutputStream", "c:\temp\" | &TarEntry.getName(), True);
         &NumberOfBytes = &TarFileReader.read(&ByteBufferArray);
         While &NumberOfBytes > 0
            &out.write(&ByteBufferArray, 0, &NumberOfBytes);
            &NumberOfBytes = &TarFileReader.read(&ByteBufferArray);
         End-While;
      End-If;
   End-If;
End-While;
&TarFileReader.close();


You can easily insert this code into an AE program in PeopleSoft. You can even wrap this into a function / method and reuse it as many times as you want.

That completes a breezy tutorial on extracting TAR files in Peoplecode. You can post a comment in the comments section of this blog, should you have anything.  See you in a different tutorial, next time.

No comments:

Post a Comment