7 Replies Latest reply on Oct 9, 2019 9:06 PM by 4005727

    Saving multiple attachments to one zip file

    4005727

      Using Peoplesoft HCM 9.2, PT 8.56.03...   Our users have the ability to save and view documents - as attachments - related to employees in the database.  Management would like to have the ability to save all of those documents (attachments) at the click of a button to a zip file as a download to the user desktop.  Can this be done?  If so, how?  Can an example be provided?

        • 1. Re: Saving multiple attachments to one zip file
          BobLilly

          I created an app engine program to extract attachments into zip files for all vouchers in a pay cycle in Financials. You should be able to adapt the logic to work online. I'm pretty sure the logic that uses java classes originally came from Jim Marion's blog site (Jim's PeopleSoft Journal ).

           

          Function AddFileToZip(&zipInternalPath, &fileNameToZip, &zip)

             REM ** We will read &fileNameToZip into a buffer and write it out to &zip;

             Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);

             Local JavaObject &buf = CreateJavaArray("byte[]", 1024);

             Local number &byteCount;

             Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);

             Local JavaObject &zipEntry = CreateJavaObject("java.util.zip.ZipEntry", &zipInternalPath | "/" | &file.getName());

            

             &zip.putNextEntry(&zipEntry);

             &byteCount = &in.read(&buf);

             While &byteCount > 0

                &zip.write(&buf, 0, &byteCount);

                &byteCount = &in.read(&buf);

             End-While;

             &in.close();

            

             rem Delete the extracted file attachment after adding to the zip;

             &file.delete();

          End-Function;

           

           

           

          Local Record &recAtt = CreateRecord(Record.ATT_VW);

          Local Rowset &rsAtt = CreateRowset(Record.ATT_VW);

          Local integer &nAtt = &rsAtt.Fill("WHERE ...");

            

          SQLExec("SELECT PRCSOUTPUTDIR FROM PSPRCSPARMS WHERE PRCSINSTANCE=:1", &nPrcsInst, &sOutDir);

          Local JavaObject &zip = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", &sOutDir | "\Voucher_Attachments.zip", True));

            

          For &i = 1 To &nAtt

             &recAtt = &rsAtt(&i).ATT_VW;

             &sSysFile = &recAtt.ATTACHSYSFILENAME.Value;

             &sUserFile = &recAtt.ATTACHUSERFILE.Value;

             &sInvoiceId = &recAtt.INVOICE_ID.Value;

             &sVendorName = &recAtt.NAME1.Value;

             &sTmpPath = GetEnv("PS_FILEDIR");

             &sUserFile = &sInvoiceId | " - " | &sUserFile;

             &retcode = GetAttachment("record://PSFILE_ATTDET", &sSysFile, &sTmpPath | "\" | &sUserFile);

             If &retcode = 0 Then

                AddFileToZip(&sVendorName, &sTmpPath | "\" | &sUserFile, &zip);

             Else

                &sMsg = "WARNING: Extraction failed with rc = " | &retcode;

                MessageBox(0, "", 0, 0, &sMsg);

             End-If;

          End-For;

           

           

           

          &zip.flush();

          &zip.close();

          I stripped out some of the code to make it easier to follow so it may not run successfully as is, but the essential pieces are there.

           

          Regards,

          Bob

          • 2. Re: Saving multiple attachments to one zip file
            4005727

            Thanks Bob, I'll give this a try.

            • 3. Re: Saving multiple attachments to one zip file
              4005727

              Hi Bob...

               

              Thanks again for your code.  I've had to put this project on the back-burner due to other work being done.  But I've started work on this one now and I'm running into a problem.  I'm getting an "file not located" error on the following code in the function...

               

                 Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);

               

              I'm wondering what value the input is expecting to see if the the attachments are being saved inside the database?
              Any insight would be greatly appreciated.

              • 4. Re: Saving multiple attachments to one zip file
                Jim Marion

                Thanks Bob! I did blog about this a while back. Another fun option today is to use the Java Zip file system.

                 

                For attachments stored in the database, the first step would be to export the content to the real file system. Otherwise you will have to get Java to read the database. Possible, but more work. If the data is sensitive and can't be  temporarily moved to a protected location, then you may have to resort to a store procedure.

                • 5. Re: Saving multiple attachments to one zip file
                  4005727

                  Thanks for the post Jim.  It got me to thinking about directory permissions and realized that may be my problem.  So I've pretty much got my code working now.  It does create the zip file, drops the attachments into the directory, attempts to write the attachments to the zip file, and completes just fine (deleting the attachments and closing the zip file).  But then I grab the zip file and it's empty.  So I just need to figure out why the "write" statement isn't working. 

                   

                  I'm a lot further along on this project now, with just a little more work to do. 
                  I appreciate your (and Bob's) input!  Thanks.

                  • 6. Re: Saving multiple attachments to one zip file
                    Jim Marion

                    Here is the original blog post. Are you flushing and closing resources? That could be it

                     

                    Jim's PeopleSoft Journal: Manipulating Zip Files with PeopleCode

                    • 7. Re: Saving multiple attachments to one zip file
                      4005727

                      Yep, I was flushing and closing files fine.

                      Problem turned out to be with one of the variables that was being passed to the function.  Once I discovered and sorted that out, the code works great.  Thank you Jim, and Bob, for your assistance on this.  The users are going to love this new functionality.