In this blog, SAP PI UDF example is been provided for below functionalities:

  1. To check file names in sap application directory with some starting key words
  2. If file is present, then compress it with zip extension and move it to other directory
  3. Post Archival, delete the file from source path

Java Imports required in UDF as

  • java.io.BufferedReader
    java.io.FileReader
    java.io.IOException
    java.util.zip.ZipEntry
    java.util.zip.ZipOutputStream

Java UDF Execution Type is “All Values of Queue”

Java UDF Code is as below:

public void file_Read_Move_Delete(ResultList result, Container container) throws StreamTransformationException{ String RequestDataStr = "XYZ"; //File Name Starting Key String FilePath_str = "/folder1/folder2/"; //File directory String FileArchival_Path = "/folder1/folder2/folder2/"; //File Archival Path String fileContentStr = ""; boolean StartsWithStr_Found = false; File[] dirFiles = new File(FilePath_str).listFiles(); if (dirFiles.length > 0){ //Check if Directory is not empty for (int i=0; i 0){ zipOutputStream.write(buf, 0, bytesRead); } // close ZipEntry to store the stream to the file zipOutputStream.closeEntry(); zipOutputStream.close(); fileOutputStream.close(); }catch (IOException e) { e.printStackTrace(); } //End of compression and archival ------------------------ result.addValue(fileNameStr + " has been comressed and archived"); } //End of "fileName check }// End of Folder for loop }// end of directory length check if (StartsWithStr_Found = false){ result.addValue("File not found"); } }

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !