4 Replies Latest reply: Feb 2, 2013 1:03 PM by Meapri RSS

    Call to Java Method to Zip directory fails with error Invalid parameter 1

    Meapri
      Hi all,

      I try to translate the following Java Code to PeopleCode and I do not succeed in it.
      Can somebody please help me out ?

      On that website http://www3.ntu.edu.sg/home/ehchua/programming/java/J5b_IO.html I extracted the Java Code:

      import java.io.File;
      public class ListDirectoryRecusive {
      public static void main(String[] args) {
      File dir = new File("d:\\myproject\\test"); // Escape sequence needed for '\'
      listRecursive(dir);
      }
      public static void listRecursive(File dir) {
      if (dir.isDirectory()) {
      File[] items = dir.listFiles();
      for (File item : items) {
      System.out.println(item.getAbsoluteFile());
      if (item.isDirectory()) listRecursive(item); // Recursive call
      }
      }
      }
      }

      PeopleCode
      I want to get all the files included in the directory


      Local string &SourceDirPath = S1_FS_RDCFG_AET.S1_CHMN_TODO;
      Local JavaObject &Javadir = CreateJavaObject("java.io.File", &SourceDirPath);

      Local array of any &StrArray;
      &ArrayOfAny = CreateArrayAny();

      Local JavaObject &JavaFilesArray = CreateJavaArray("java.lang.Object[]", 1024);
      &JavaFilesArray = &Javadir.listFiles();

      CopyFromJavaArray(&JavaFilesArray, &ArrayOfAny);


      ==> I alway get the error

      Message:
      Invalid parameter 1 for function CopyFromJavaArray. (2,116) S1_DSITE_ZIP.MAIN.GBL.default.1900-01-01 .DIRECTOR.OnExecute PCPC:511 Statement:9

      I thank you in avance

      Edited by: Meapri on Jan 23, 2013 9:33 AM
        • 1. Re: Call to Java Method to Zip directory fails with error Invalid parameter 1
          jim.marion
          Here is a fragment from my own code that iterates over files in a directory and adds them to a zip file:
             Local JavaObject &folder = CreateJavaObject("java.io.File", &srcFolder);
             Local JavaObject &jItems = &folder.list();
             Local array of any &items = CreateArrayAny();
             Local number &idx = 0;
             
             &items [&jItems.length] = Null;
             CopyFromJavaArray(&jItems, &items);
          You should check whether &JavaFilesArray is null or if it has any items. I suspect that is the issue.

          Are you running this PeopleCode in an App Engine locally through App Designer or on the app server? Is the path S1_FS_RDCFG_AET.S1_CHMN_TODO relative to the run location? You might try printing the value of S1_FS_RDCFG_AET.S1_CHMN_TODO just to make sure it contains what you expect.

          As a side note... Notice that I don't create an instance of java.lang.Object[]. The original Java didn't either. That is because the list and listFiles methods don't copy file names from one array to another. They just return a pointer to an existing array. Essentially, your java.lang.Object[] gets thrown away when you call listFiles.

          Here is a blog post I wrote last year that gives a lot of PeopleCode for creating Zip files: http://jjmpsj.blogspot.com/2012/10/manipulating-zip-files-with-peoplecode.html.
          • 2. Re: Call to Java Method to Zip directory fails with error Invalid parameter 1
            HakanBiroglu
            Hi,

            When working with java arrays in PeopleCode I have had more success using [url http://docs.oracle.com/javase/tutorial/reflect/index.html] Reflection
            See this thread on an example
            {message:id=10335100}

            Regards,

            Hakan
            • 3. Re: Call to Java Method to Zip directory fails with error Invalid parameter 1
              Meapri
              jim.marion wrote:
              Local JavaObject &folder = CreateJavaObject("java.io.File", &srcFolder);
              Local JavaObject &jItems = &folder.list();
              Local array of any &items = CreateArrayAny();
              Local number &idx = 0;
              
              &items [&jItems.length] = Null;
              CopyFromJavaArray(&jItems, &items);
              Thanks Jim. I copied and pasted your sample code and it worked just fine. The &folder.list() method returns a array of string containing the files/folder names and the CopyFromJavaArray(&jItems, &items).

              At this very moment I loop over the string names of the array to check wether if it is a file or a directory and nothing is recursive yet.
              Local string &SourceDirPath = S1_FS_RDCFG_AET.S1_CHMN_TODO;
              Local JavaObject &Folder = CreateJavaObject("java.io.File", &SourceDirPath);
              Local JavaObject &jItems = &Folder.list();
              /* Transformation en Array Pcode*/
              Local array of any &items = CreateArrayAny();
              Local number &idx = 0;
              &items [&jItems.length] = Null;
              CopyFromJavaArray(&jItems, &items);
              For &idx = 1 To &jItems.length
                 Local JavaObject &FileOrFolder = CreateJavaObject("java.io.File", &SourceDirPath | "/" | &items [&idx]);
                 If &FileOrFolder.isDirectory() Then
                    MessageBox(0, "", 99000, 0, "Directory : " | &items [&idx]);
                 Else
                    MessageBox(0, "", 99000, 0, "File : " | &items [&idx]);
                 End-If;
              End-For;
              • 4. Re: Call to Java Method to Zip directory fails with error Invalid parameter 1
                Meapri
                The Pcode I wrote to Zip Directories
                Function AddFileToZip(&sDirSeparator, &zipInternalPath, &fileNameToZip, &zipFileName, &outFileStream, &zipStreamOut)
                   Local JavaObject &file = CreateJavaObject("java.io.File", &fileNameToZip);
                   Local JavaObject &inFileStream = CreateJavaObject("java.io.FileInputStream", &fileNameToZip);
                   Local JavaObject &zipEntry = CreateJavaObject("java.util.zip.ZipEntry", &fileNameToZip);
                   REM **  Créer le flux de sortie de données pour le fichier ZIP final;
                   REM ** We will read &fileNameToZip into a buffer and write it out to &zip;
                   Local string &ZippedfileName = &zipInternalPath | &sDirSeparator | &zipFileName;
                   REM ** Make sure zip entry retains original modified date;
                   &zipEntry.setTime(&file.lastModified());
                   &zipStreamOut.putNextEntry(&zipEntry);
                   
                   Local JavaObject &buf = CreateJavaArray("byte[]", 1024);
                   Local number &byteCount;
                   
                   &byteCount = &inFileStream.read(&buf);
                   While &byteCount > 0
                      &zipStreamOut.write(&buf, 0, &byteCount);
                      &byteCount = &inFileStream.read(&buf);
                   End-While;
                   &inFileStream.close();
                   MessageBox(0, "", 99000, 0, "Zipped File : " | &fileNameToZip);
                End-Function;
                
                Function getDirContent(&sDirSeparator, &parentFolder, &zipFileName, &outFileStream, &zipStreamOut)
                   MessageBox(0, "", 99000, 0, "Directory to scan : " | &parentFolder);
                   Local JavaObject &Folder = CreateJavaObject("java.io.File", &parentFolder);
                   Local JavaObject &jItems = &Folder.list();
                   /* Transformation en Array Pcode*/
                   Local array of any &items = CreateArrayAny();
                   Local number &idx = 1;
                   &items [&jItems.length] = Null;
                   CopyFromJavaArray(&jItems, &items);
                   For &idx = 1 To &jItems.length
                      Local string &Entry = &parentFolder | &sDirSeparator | &items [&idx];
                      Local JavaObject &FileOrFolder = CreateJavaObject("java.io.File", &Entry);
                      If &FileOrFolder.isDirectory() Then
                         getDirContent(&sDirSeparator, &Entry, &zipFileName, &outFileStream, &zipStreamOut);
                      Else
                         AddFileToZip(&sDirSeparator, &parentFolder, &Entry, &zipFileName, &outFileStream, &zipStreamOut);
                      End-If;
                   End-For;
                End-Function;
                
                
                Local string &RootName = "C:\TEMP\TEST";
                Local string &sDirSeparator = "\";
                
                REM ** Create Output Zip File ;
                
                Local string &ZipFileName = &RootName | &sDirSeparator | "Temp2_Zip.zip";
                Local JavaObject &OutFileStream = CreateJavaObject("java.io.FileOutputStream", &ZipFileName, True);
                Local JavaObject &zipStreamOut = CreateJavaObject("java.util.zip.ZipOutputStream", &OutFileStream);
                REM ** Create Zip File Entries found in the directory;
                getDirContent(&sDirSeparator, &RootName, &ZipFileName, &OutFileStream, &zipStreamOut);
                REM Close Output Zip File ;
                &zipStreamOut.close();
                Edited by: Meapri on Feb 2, 2013 10:54 AM : copy and paste code that works!

                Edited by: Meapri on Feb 2, 2013 10:56 AM

                Edited by: Meapri on Feb 2, 2013 11:02 AM