Zip and Email large file in Application engine PeopleCode

Zip and Email large file:

&qryFile="Vendor_details";

&zip_name="Zip_File_name";

Local string &ZipFile = %FilePath | &zip_name;

Local string &Zip_Cmd = "D:BatchServerBatchPKzip25 " | &ZipFile | " -add " | &qryFile | "." | &ext;

CommitWork();

&ExitCode = Exec(&Zip_Cmd, %Exec_Synchronous + %FilePath_Absolute);

&email.AddAttachment(&ZipFile, %FilePath_Absolute, &zip_name, &attachDescr, "", "");


Using Java code to zip the file:

   &buffer = CreateJavaArray("byte[]", 18024);

    &zipStream = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", &outDir | &outZip));

    For &i = 1 To &inFiles.Len

      &zipStream.putNextEntry(CreateJavaObject("java.util.zip.ZipEntry", &inFiles [&i]));

      &inStream = CreateJavaObject("java.io.FileInputStream", &outDir | &inFiles [&i]);

      &len = &inStream.read(&buffer);

      While &len > 0;

        &zipStream.write(&buffer, 0, &len);

        &len = &inStream.read(&buffer);

      End-While;

      &zipStream.closeEntry();

      &inStream.close();

    End-For;

   &zipStream.close();


Here is an IScript that demonstrates this:

Function IScript_GetAttachment()

   Local any &data;

   Local string &file_name = "attachment.doc";   

   Local SQL &cursor = CreateSQL("SELECT FILE_DATA FROM PS_EO_PE_MENU_FILE WHERE ATTACHSYSFILENAME = :1 ORDER BY FILE_SEQ", &file_name);


   REM Set the following header if you want a download prompt. Don't set it if you want inline content;

   %Response.SetHeader("content-disposition", "attachment;filename=" | &file_name);


   While &cursor.Fetch(&data);

      %Response.WriteBinary(&data);

   End-While;

End-Function;



Extracting ZIP files through PeopleCode:

Sometimes we need to extract the ZIP files and then load the extracted file data into PeopleSoft tables using interfaces.

Now, in this scenario will see how to extract the ZIP File using PeopleCode.

Find the below sample code to unzip the ZIP files.


Local string &SourceZipFilePath, &targetPath;


&SourceZipFilePath= “D:\PSOFT\PSREPORTS\FIN92TST\FILES\Expenses.zip”;

&targetPath= “D:\PSOFT\PSREPORTS\FIN92TST\FILES\”;

Local JavaObject &zipFileInput = CreateJavaObject(“java.io.FileInputStream”, &SourceZipFilePath);

Local JavaObject &zipInputStream = CreateJavaObject(“java.util.zip.ZipInputStream”, &zipFileInput);

Local JavaObject &zipEntry = &zipInputStream.getNextEntry();

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

Local number &byteCount;

While &zipEntry <> Null

If (&zipEntry.isDirectory()) Then

/****Nothing to process****/

Else

Local JavaObject &OutputFile = CreateJavaObject(“java.io.File”, &targetPath| &zipEntry.getName());

&OutputFile.getParentFile().mkdirs();

Local JavaObject &out = CreateJavaObject(“java.io.FileOutputStream”, &OutputFile );

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

While &byteCount > 0

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

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

End-While;


No comments:

Post a Comment

PeopleCode to retrieve Google map between two addresses

  PeopleCode Example: /* Define constants for the API request */ Local string &origin = "123 Main St, Anytown, USA";   /* ...