In this tutorial we will describe how to create a .tar.gz file in PeopleCode, using native Java libraries and JTar. We will be using examples from our tutorials earlier for this, and so make sure you read (strongly recommended) the following tutorials before starting with this one:
Create GZIP File in PeopleCode - Explains how to create .gz file in PeopleCode
Create TAR File in PeopleCode - Explains how to create TAR file in PeopleCode
We will be reusing bulk of the code in these tutorials, and just add support for TARing multiple input files and doing a gzip on them. I have structured the tutorial as below:
Steps to create tar.gz file for multiple input files in PeopleCode |
1. Add Bytes to File - Generic Function
Both
TarOutputStream
and ZIPOutputStream
supports write
method to write the bytes from a File to the archive. The code to read an input file from FileInputStream
and writing it to a archive, is consistent across these, as you may have inferred from the tutorials earlier. So, we write a function AddBytesToFile
that will do the following:- Accept an archive and Input File
- Get Input File in FileInputStream
- Declare a Byte Array and
read
bytes from the File - Write bytes to the archive until all bytes from the input are read
- Close the InputStream
The generic function code is provided below:
Function AddBytesToFile(&archive, &fileName)
/* Read source file into input stream */
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileName);
/* 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 corresponding Archive Output Stream */
&archive.write(&filebuffer, 0, &byteCount);
&byteCount = &in.read(&filebuffer);
End-While;
/* Close input stream */
&in.close();
End-Function;
2. Create TAR Entry Function
Only to create TAR files, we need a function to accept an archive and file name to create a
TarEntry
and add the TAR entry to the archive. This is not required for .gz files. We have already discussed the functionality behind this code earlier, so we will provide only the PeopleCode segment below:Function CreateTarEntry(&Tar, &fileNameToTar)
/* 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);
/* Invoke Add Bytes to File function to add the file to TAR archive */
AddBytesToFile(&Tar, &fileNameToTar);
End-Function;
Note that we are invoking
AddBytesToFile
function at the end, as we want to add the bytes to the TAR file after creating a TAR entry.3. Code to TAR Multiple Files
The code to TAR multiple files is very simplified now. You just have to invoke CreateTarEntry function as many times you want, depending on the number of input files you have. Of course, you have to create TarOutputStream that will point to physical TAR file, before doing this.
/* Create Target TAR File */
Local JavaObject &Tar = CreateJavaObject("org.xeustechnologies.jtar.TarOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar", True));
CreateTarEntry(&Tar, "c:\dest.log");
CreateTarEntry(&Tar, "c:\dest.sql");
CreateTarEntry(&Tar, "c:\final.sql");
/* Close TarOutputStream*/
&Tar.close();
The PeopleCode segment above successfully creates a TAR file, which will comprise three independent files.
4. Code to .GZ Final TAR File
You will be getting a TAR file at the end of Step 3. All you need to do now is to pass this TAR file to GZIPOutputStream and invoke the AddBytesToFile function, which will add the bytes from the TAR file and create a .gz file out of it. The PeopleCode segment is shown below:
/* We will now create a GZIP file */
Local JavaObject &gzip = CreateJavaObject("java.util.zip.GZIPOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar.gz", True));
AddBytesToFile(&gzip, "c:\myTarFile.tar");
&gzip.close();
The tar.gz file is ready. !
Complete PeopleCode to Create tar.gz file
Putting it all together, the complete PeopleCode segment to create a .tar.gz file for multiple input files is shown below:
Function AddBytesToFile(&archive, &fileName)
/* Read source file into input stream */
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileName);
/* 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 corresponding Archive Output Stream */
&archive.write(&filebuffer, 0, &byteCount);
&byteCount = &in.read(&filebuffer);
End-While;
/* Close input stream */
&in.close();
End-Function;
Function CreateTarEntry(&Tar, &fileNameToTar)
/* 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);
/* Invoke Add Bytes to File function to add the file to TAR archive */
AddBytesToFile(&Tar, &fileNameToTar);
End-Function;
MessageBox(0, "", 0, 0, "PeopleCode - Create TAR.gz File - Example");
/* Create Target TAR File */
Local JavaObject &Tar = CreateJavaObject("org.xeustechnologies.jtar.TarOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar", True));
CreateTarEntry(&Tar, "c:\dest.log");
CreateTarEntry(&Tar, "c:\dest.sql");
CreateTarEntry(&Tar, "c:\final.sql");
/* Close TarOutputStream*/
&Tar.close();
/* We will now create a GZIP file */
Local JavaObject &gzip = CreateJavaObject("java.util.zip.GZIPOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\myTarFile.tar.gz", True));
AddBytesToFile(&gzip, "c:\myTarFile.tar");
&gzip.close();
The output of this code segment creates a .TAR file and a .tar.gz file also. (You may want to delete .tar file after creation of .gz ). You can place this code in the peoplecode event of your choice, and examine the output. If you are stuck, you can post us a comment, we will have a look.
Thank you - nice article. I had similar problem to solve when working with vendor and getting images from them.
ReplyDelete