SAP PI UDF to compress file and archive
SAP PI UDF to compress file and archive: In this blog, SAP PI UDF example is been provided for below functionalities:
- To check file names in sap application directory with some starting key words
- If file is present, then compress it with zip extension and move it to other directory
- 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<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"; try{ // 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()); zipOutputStream.putNextEntry(zipEntry); 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 = fileInputStream.read(buf)) > 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