Skip to Content

Business requirement is like:

  • While triggering SAP-PI’s SOAP web-service from client system, zip attachments will be send along with SOAP Request Payload. Here, please note, one zip per web-service call is in scope.
  • This zip attachments has multiple CSV files, which needs to be extracted in SAP’s application directory
  • Once unzip is successful, return unzipped zip-File in SOAP-Response Message

To accomplish above business requirements, one Java UDF will be written in Request Message Mapping of SAP-PI’s respective (SOAP-to-File Synchronous Inbound) interface

Here, given example of SAP PI Java UDF (user defined function), has below functionalities:

  • To read zip attachment from Request Payload
  • Unzip attachment to one SAP application server directory
  • Using Dynamic-configuration, return unzipped zip file name

UDF Imports:

public String UnZip(String upzipPath, Container container)
 throws StreamTransformationException{

UDF to:
            1. Get ZipFile Attachment (i.e. get the input attachment from the source message)
            2. Unzip/extract ZipFile to SAP-Application directory
            3. Set unzipped attachment name to "FileName" variable using 'DynamicConfiguration'

//File destDir = new File("/folde1/folde2/folde3/"); 
File destDir = new File(upzipPath); 
String resultStr = "";

//To add trace info
AbstractTrace trace;
trace = container.getTrace();
trace.addInfo("UDF to unzip ZipFile atatchment to the directory " + destDir );

//Get the input attachment from the source message)
GlobalContainer  globalContainer    = container.getGlobalContainer();
InputAttachments inputAttachments = globalContainer.getInputAttachments();
 //checks for the availability of attachments in the input message
 trace.addInfo("Attachments Available");
 //Get the attachmentIds and store it in an Object array
 Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);
 Object[] arrayObj = CollectionIDs.toArray();
 String attachmentID = null;													
 String contentIDS = "";
 //Loops at the input attachments to get the content of the attachment
 for(int i =0;i<arrayObj.length;i++){
  attachmentID =(String)arrayObj[i];   																				  
  resultStr = 	attachmentID;        //Get ZipAttachment Name

  Attachment attachment = inputAttachments.getAttachment(attachmentID);      
  ZipInputStream zipIs = new ZipInputStream(new ByteArrayInputStream(attachment.getContent()));
  ZipEntry zEntry = null;
  try {
   while ((zEntry = zipIs.getNextEntry()) != null) {							
    // Write each file into directory -------------------------------
    trace.addInfo("zipFile content: " + zEntry.getName());  // Get each Zip content names		

    File destFile = new File(destDir, zEntry.getName());
    FileOutputStream fos = new FileOutputStream(destFile); // It creates blank file in dir
    OutputStream output = new BufferedOutputStream(fos);
    int len;
    byte[] buffer = new byte[2048];
    while ((len = > 0) {
     output.write(buffer, 0, len);
    // End of Write each file into directory ------------------------
  } catch (FileNotFoundException e) {
      trace.addInfo(" UnZip UDF Error: " +  e.getMessage());
  } catch (IOException e) {
      trace.addInfo(" UnZip UDF Error: " +  e.getMessage());
catch (Exception e)
  trace.addInfo(" UnZip UDF Error: " +  e.getMessage());

//Return original name of unzipped attachment (zipFile name)
DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("","FileName");
conf.put(key, resultStr);
catch (Exception e)
   trace.addInfo(" StoreAttachment UDF Error: " +  e.getMessage());
return   resultStr;
}//end of fn UnZip


To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply