Showing posts with label zip. Show all posts
Showing posts with label zip. Show all posts

Monday, March 6, 2023

Mass upload/download files

Recently I was presented with a requirement wherein the user should be able to mass upload files from their local workstation to PeopleSoft and then mass download the files from PeopleSoft to their local workstation. The files would be stored in the database record.

I am running PT 8.59.x and PeopleSoft does provide a couple of functions MAddAttachment and DetachAttachment for this exact requirement. 

The MAddAttachment function is pretty straight forward. I was able to call it via FieldChange event, where in a dialog box is presented to the user to select a single file or multiple files and the files are loaded to the URL value provided as part of the function parameter. The function also has a parameter which can limit the number of files uploaded at a time. Just like the AddAttachment function this function also converts the filenames which have special characters like a space, amersand, plus sign etc. to underscores. More info about this under the "Understanding the File Attachment Functions" and "File Name Considersations" section in Peoplebooks.

For my requirement I was displaying the files loaded via MAddAttachment in a grid on the page. Now I provided another button, and on FieldChange of this button the plan was to invoke DetachAttachment to run through the files in the grid and download them locally to the user's workstation. For some reason this does not work. The code would run through the grid but would download only the last file in the grid, no errors anywhere.  So following is what I did to get around this limitation.

I am using GetAttachment first to read through the grid rowset and download the files to a temporary location on the server. I am programmatically creating a folder structure to download the files to. Then I am using java clases to compress or zip up the files in the temporary folder. Process to compress files available here. Then I am using PutAttachment function to upload the single compressed/zip file back into the database. Finally I am calling DetachAttachment to download the compressed/zip file to the user's local workstation. As part of post-cleanup I am using DeleteAttachment to delete the compressed/zip file from the database and then using RemoveDirectory to delete the temporary location created on the sevrer to download the files to.

Monday, February 20, 2023

Peoplecode to zip/archive files

Following is a way to archive, compress or zip up files via peoplecode. Alternative would be to use command line calls to products like winzip, 7-zip etc. For this test I am running PT 8.59.x. The same should also be possible in slightly older Peopletools releases too, like PT 8.56.x

Oracle provides java classes that can be used for this task. This method will create a new archive file and add files to the root location of the archive or zip file. 

The solution uses the following three java classes that are delivered with PeopleTools

1. java.io.FileOutputStream

2. java.util.zip.ZipOutputStream

3. java.util.zip.ZipEntry


/* initialize Java Obejct &myArchive */
Local JavaObject &myArchive = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", &zipFileName, True));

In the above line of code &zipFileName is the complete path of the archive file that we will create.

&fileNametoAdd - will have the complete path to the file that we will be adding to the archive. 

In order to get the filename value from the file path do the following.

&FilePath = Split(&fileNametoAdd, "\");
&fname = &FilePath [&FilePath.Len];

Local JavaObject &ArchiveEntry = CreateJavaObject("java.util.zip.ZipEntry", &fname);

/* Add Archive entry to OutputStream */
&myArchive.putNextEntry(&ArchiveEntry);

/* now add the file to the archive */
/* Read source file into input stream */
Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNametoAdd);

/* 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 */
      &myArchive.write(&filebuffer, 0, &byteCount);
      &byteCount = &in.read(&filebuffer);
End-While;
   
/* Close input stream */
&in.close();

/* Close archive */
&myArchive.close();