Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
Showing results for 
Search instead for 
Did you mean: 
0 Kudos
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 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<dirFiles.length; i++){ //Loop Iteration for Each File
String fileNameStr = dirFiles[i].getName(); //Get File Name
if (fileNameStr.startsWith(RequestDataStr)){ //Check if Filename starts with Key 'XYZ'

StartsWithStr_Found = true;
File inputFile = new File(FilePath_str + fileNameStr);

//Start of compression and archival ----------------------
If file found, the zip them
and move them to other folder
Here each file is been compressed to single zip file
and Zip Output File Name will be same as of InputfileName,
but replacing .CSV extension to .zip
String OUTPUT_FILE = dirFiles[i].getName() ;
OUTPUT_FILE = OUTPUT_FILE.replaceAll(".CSV", "");
OUTPUT_FILE = FileArchival_Path + OUTPUT_FILE + ".zip";

// Wrap a FileOutputStream around a ZipOutputStream to store the zip stream to a file. Note that this is not absolutely necessary
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

// a ZipEntry represents a file entry in the zip archive
// We name the ZipEntry after the original file's name
ZipEntry zipEntry = new ZipEntry(inputFile.getName());

FileInputStream fileInputStream = new FileInputStream(inputFile);
byte[] buf = new byte[1024];
int bytesRead;

// Read the input file by chucks of 1024 bytes
// and write the read bytes to the zip stream
while ((bytesRead = > 0){
zipOutputStream.write(buf, 0, bytesRead);

// close ZipEntry to store the stream to the file
}catch (IOException e) {
//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");

Labels in this area