SAP PI UDF to set a zip Attachment in Response Message Payload

Suppose business requirement is like:

  • Read multiple CSV files in to single Zip File and send it as an attachment to third Party

Then for such business requirement one sap-pi scenario can be created like:

  • Soap to File Inbound Synchronous Interface
  • that means one sap-pi soap web-service synchronous scenario where:
    1. in Response Message Mapping, one UDF will be required to Read files and set them as an zip attachment
    2. Scenario will have Communication Channels like Soap Sender and File Receiver
    3. File Receiver channel’s job will be only to create a dummy empty file in SAP’s app directory and that file will be overwritten for each time. This File channel is required only to complete scenario.

SAP PI UDF example which is “To Set multiple CSV file as an zip Attachment to Response Message Payload”, will have below functionality :

  • To read multiple CSV files from folder
  • compress them to a single Zip file. Here limit 10 CSV files per zip
  • Archive each zipped CSV file to other directory
  • Set zip file as an attachment
  • Set Zip File name to Dynamic Configuration variable, so that same FileName can be used further

//=======================================================================

public String zipToAttachment(String filePath, String archivePath, Container container) throws StreamTransformationException{ /* UDF for below functionalities: To get current Date time To Read 10 files from a directory of SAP-APP server (only .CSV files and should not starts with "ARC") To zip 10 files in a single zip file and rename zip as "zipFileName_currentDateTime" To archive 10 files to other directory To set single zip file as attachment to HTTP response message To set attachment header as "application/zip" */ //Start of :Get Current Date Time ............................................ //Current DateTime to be used in ZipFileNaming conventions DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd : HHmmssSSS"); Date date = new Date(); String cDate = dateFormat.format(date).toString(); cDate = cDate.replaceAll(" : ", "T"); //End of : Get Current Date Time ............................................. String Input_filePath = filePath; String Archive_filePath = archivePath; String Ouput_Zip = "zipFileName_" + cDate + ".zip";//zipFileName_20170610T112255987.zip String resultStr = ""; Boolean csvFound = false; String errorStr = ""; //Start of CsvFile Zipping and archinving to other folder .................... try{ File[] fileDir = new File(Input_filePath).listFiles(); if (fileDir.length > 0) { FileOutputStream fos = new FileOutputStream(Ouput_Zip); ZipOutputStream zos = new ZipOutputStream(fos); int fileCount = 0; //Read each file in th directory for (int i=0; i

 

//=======================================================================

New NetWeaver Information at SAP.com

Very Helpfull

User Rating: Be the first one !

Comments (0)
Add Comment