PeopleCode 处理压缩文件

 PeopleSoft中对文件附件的处理都是单个文件处理的,虽然在8.52版本新增了MAddAttachment(URLDestination, DirAndFilePrefix, Prompts, &UserFileArray, &ActualSizeArray, &DetailedReturnCodeArrayName [, MaxSize [, PreserveCase[, UploadPageTitle[, AllowLargeChunks[, StopOnError]]]]]) Function 实现了一次上传多个附件的功能,但是在下载附件的时候,还是只能单个下载,

这样就给客户的操作带来的很多不便,这篇文章来说明一下如何在PeopleCode中调用Java类来实现对文件的打包

REM ** The file I want to compress;
 
Local string &fileNameToZip = "c:\temp\blah.txt";
 
 
REM ** The internal zip file's structure -- internal location of blah.txt;
 
Local string &zipInternalPath = "my/internal/zip/folder/structure";
 
 
Local JavaObject &zip = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\temp\compressed.zip", True));
 
 
Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);
 
REM ** We will read &fileNameToZip into a buffer and write it out to &zip;
 
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());
 
 
REM ** Make sure zip entry retains original modified date;
 
&zipEntry.setTime(&file.lastModified());
 
 
&zip.putNextEntry(&zipEntry);
 
 
&byteCount = &in.read(&buf);
 
 
While &byteCount > 0
 
&zip.write(&buf, 0, &byteCount);
 
&byteCount = &in.read(&buf);
 
End-While;
 
 
&in.close();
 
&zip.flush();
 

&zip.close();
这样就是实现了对文件的打包,不过为了代码重用,我们可以将这段代码写成一个Function

  1. Function AddFileToZip(&zipInternalPath, &fileNameToZip, &zip)
  2. Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);
  3. REM ** We will read &fileNameToZip into a buffer and write it out to &zip;
  4. Local JavaObject &buf = CreateJavaArray("byte[]", 1024);
  5. Local number &byteCount;
  6. Local JavaObject &in = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);
  7. Local JavaObject &zipEntry = CreateJavaObject("java.util.zip.ZipEntry", &zipInternalPath | "/" | &file.getName());
  8. REM ** Make sure zip entry retains original modified date;
  9. &zipEntry.setTime(&file.lastModified());
  10. &zip.putNextEntry(&zipEntry);
  11. &byteCount = &in.read(&buf);
  12. While &byteCount > 0
  13. &zip.write(&buf, 0, &byteCount);
  14. &byteCount = &in.read(&buf);
  15. End-While;
  16. &in.close();
  17. End-Function; 

 然后每次使用的时候,只需要调用这个Function就可以了。

  1. Local JavaObject &zip = CreateJavaObject("java.util.zip.ZipOutputStream", CreateJavaObject("java.io.FileOutputStream", "c:\temp\compressed.zip", True));
  2. AddFileToZip("folder1", "c:\temp\file1.txt", &zip);
  3. AddFileToZip("folder1", "c:\temp\file2.txt", &zip);
  4. AddFileToZip("folder2", "c:\temp\file1.txt", &zip);
  5. AddFileToZip("folder2", "c:\temp\file2.txt", &zip);
  6. &zip.flush();
  7. &zip.close();
posted @ 2013-10-24 11:54  Bryan chen  阅读(665)  评论(0编辑  收藏  举报