Technical Articles
Handling ZIP files in SAP Cloud Platform Integration
Introduction:
In below blog would like to share how to deal with File Archives ( ZIP ) in SAP Cloud Platform using ZIP Splitter and ZIP Aggregator.
Scenarios:
In following sections will cover scenarios involving SFTP,SOAP and HTTP adapter with ZIP file content in detail.
Unzipping input ZIP file from SFTP to have multiple files at target size keeping same source file name :
Integration Scenario where we will be getting ZIP file SFTP server and need to unzip the file in SAP CPI keeping the same source name of the files using ZIP Splitter step.
Design :
Step 1: Configure your SFTP Sender with Server Name / Source File Path with file to be read.
Step 2: Configure ZIP Splitter to unzip incoming ZIP .
Step 3: Content modifier to parse the incoming file name to keep same file name at the target side.we will be getting file name in camel header CamelFileNameOnly with value ZIPNAME/Filename in order to get only file name we can use below expression.
Value:
${header.CamelFileNameOnly.replaceAll("[0-9A-Za-z]+\/","")}
Step 4: Configure SFTP receiver to get place all the unzipped files and we can use ${property.FName} in receiver SFTP channel to get dynamic file name.
Unzipping input ZIP file from SOAP request to have multiple files at target size keeping same source file name :
Instead of loading zip file from SFTP we will be sending the ZIP file as attachment to SOAP request using SOAP UI.
Design:
All the steps remain same as use case-1 except below highlighted extra step to parse the incoming attachment
Script to parse SOAP attachment:
import com.sap.gateway.ip.core.customdev.util.Message
import java.util.Map
import java.util.Iterator
import javax.activation.DataHandler
def Message processData(Message message) {
Map<String, DataHandler> attachments = message.getAttachments()
if (attachments.isEmpty()) {
message.setBody("No attachment Found")
return message;
} else {
Iterator<DataHandler> it = attachments.values().iterator()
DataHandler attachment = it.next()
message.setBody(attachment.getContent())
}
return message
}
Unzipping input ZIP file from HTTP request to have multiple files at target size keeping same source file name :
Instead of loading zip file from SFTP we will be sending the ZIP file as attachment to HTTP request ( multipart/form-data ) using POSTMAN
Design:
All the steps remain same as use case-1 except below highlighted extra step to parse the incoming attachment
Add MIME Multipart Decoder to parse the incoming multipart/form-data having ZIP file as attachment.
Creating multiple files in Integration Flow and zipping it using Gather ( ZIP Aggregator ):
We will be creating simple iflow where we will be creating 3 different types of files ( CSV , Text and XML ) and sending as ZIP to target system
Design:
Step 1: Set body containing 3 different types of content inside it.
<Root>
<file1>
<Root>
<Record>
<Name>Sriprasad1</Name>
<CompanyName>SAP</CompanyName>
<EmpID>E1</EmpID>
<Designation>Consultant</Designation>
</Record>
<Record>
<Name>Sriprasad2</Name>
<CompanyName>SAP</CompanyName>
<EmpID>E2</EmpID>
<Designation>Consultant</Designation>
</Record>
</Root>
</file1>
<file2>
This is sample Text file and it should be saved as TestText.txt in ZIP file.
This is sample Text file and it should be saved as TestText.txt in ZIP file.
This is sample Text file and it should be saved as TestText.txt in ZIP file.
This is sample Text file and it should be saved as TestText.txt in ZIP file.
This is sample Text file and it should be saved as TestText.txt in ZIP file.
This is sample Text file and it should be saved as TestText.txt in ZIP file.
</file2>
<file3>
Name,CompanyName,EmpID,Designation
Sriprasad1,SAP,E1,Consultant
Sriprasad2,SAP,E2,Consultant
Sriprasad3,SAP,E3,Consultant
</file3>
</Root>
Step 2.a:Parallel Multicast branch to get XML content for File1.
Step 2.b: Content filter to get XML content for File1
/Root/file1
Step 2.c: Content Modifier to set the header for file name.
Step 3.a:Parallel Multicast branch to get text content for File2.
Step 3.b: Content filter to get text content for File2
/Root/file2
Step 3.c: Content Modifier to set the header for file name.
Step 4.a:Parallel Multicast branch to get CSV content for File3.
Step 4.b: Content filter to get CSV content for File3
/Root/file3
Step 4.c: Content Modifier to set the header for file name.
Step 6: Configure SFTP receiver to get single Zipped file having 3 different types of files inside it.
Finally deploy the IFlow to received Zip file in target SFTP server.
Hope above details helps to handle ZIP files coming from different adapters in SAP CPI.
References:
[1] : SAP CPI Splitter-Aggregator Capability By Mandy Krimmel
Really useful functionality and a nice blog post, Sriprasad – thank you.
Regards,
Morten
Hi Morten Wittrock,
I have seen your blog regarding reading attachments but i am unable to post binary file data from attachment using setBody() method to HTTP receiver URL.
Can you please help me with the issue highlighted in below link.
https://answers.sap.com/questions/13110404/unable-to-post-files-to-http-receiver-using-groovy.html
Regards,
Sachin Belide
Hi Sri,
Very useful feature and thanks for the well explained blog.
thanks and regards
Praveen T
Hi Sri,
Is this feature is available now ? i am using the trail vesrion and i am not able to see the option Any in incoming format .
Warm Regards,
Madhu
Hi Sri,
Thanks for the detailed explanation with different scenarios.
Regards,
Pavan G
Thanks for the sharing.. very informative..
Hi Sri,
Thanks for the blog. I have a requirement to pick 3 files from SFTP and send all 3 files in one mail. But i am getting 3 different mails with each file. Can you please help me on how to send all 3 files as attachments in one mail?
Thanks,
Imran
Hi Sri,
Yet another brilliant article from you. I do have a question though.
If we want to create the zip file, with some files in the root and then a folder with another files. In short, zip file with files and folder inside that. Do you think we can do this via standard design elements? Or do we have to depend on groovy for this?
Best Regards,
Anoop
Hello Anup,
As per my understanding its not possible with standard step available .You have to depend on the Script and any open source JARs to achieve it.
Regards,
Sriprasad Shivaram Bhat
Hi Sri,
Thanks for sharing this..
I am trying to implement using this ZIP splitter but i cant seem to work with .7z (7zip archives)..Would you know if this works with 7zip as well?
Hi Sriprasad,
This another nice blog. Would it be possible to generate Password protected zip file and send to SFTP.
Thanks,
Krishna
Hi Sriprasad,
Is it possible to change the format of files inside a zip file during the split. Sriprasad S Bhat .
Thanks,
Pranav
Thanks Sir Sriprasad!
Hi Sriprasad,
Thanks for the wonderful blog. Could you please paste the script the code you are using in Log payload step. I want to check if ZIP file contians all the 3 files. I do not have FTP server.
Hi Sriprasad Shivaram Bhat,
I have a scenario in SAP CPI like Sender is SFTP which contains multiple files (.txt) so I have to pick all those files and send them to the target in a single .zip file, So if we unzip the file we have to get all the sender files.
Kindly please help me with this, As I tried multiple ways.
Thanks
DG