Technical Articles
Zip file the right way in SAP Cloud Integration!
This blog describes the use of CamelFileName header while zipping the files using Zip function in SAP Cloud Integration ,Build number 6.27.9 !
When using zip function in sap cloud integration make sure to set the header CamelFileName before the Zip function.Also CamelFileName header should not be set explicitly in FileName parameter in SFTP Receiver Adapter. [ If set Dynamic Value of ${header.CamelFileName}is lost and rather file name is just ” ${header.CamelFileName}”]
1.What if CamelFileName header is not set before the Zip function function.
1.a) Set Header FileName = Test_${date-with-timezone:now:CET:dd-MM-yyyy}.csv
Using FileName as ${header.FileName} without using .zip extension in the sftp adapter.
IFlow without CamelFileName header set before ZIP function
Let us try to understand the first case. I have used a sample flow where a Xml file is converted to csv and we zip the csv file and put it into the SFTP folder.
Using the Content Modifier : Setting the FileName we have created a header FileName
FileName Header in Content Modifier
We set the FileName in sftp receiver channel as ${header.FileName} [ Note we have not used the .zip extension]
FileName in SFTP Receiver Channel
When we try to hit the IFlow with XML, we have the following csv file in the sftp target folder.
ZIP Compressed CSV File in SFTP Folder
We can see that the .zip type is missing in the file as well as when i tried to open the file it was corrupt. Actual Size of the Proper CSV file was about 237 Kb .. which we will see later .
1.b) Set Header FileName = Test_${date-with-timezone:now:CET:dd-MM-yyyy}.csv
Using FileName as ${header.FileName}.zip in the sftp adapter
FileName in SFTP Receiver Channel
In this case you will have a zip file in the SFTP Folder, but compressed file would have automatically assigned name with type as “File”.
ZIP Compressed CSV File with type as File
When the target system unzips, looks up for a specific naming format , this could lead to problems as now the actual intended filename pattern would become a folder under which the compressed file would be saved as type “File” with a automatically assigned file name.
2.What if CamelFileName header is set before the Zip function
IFlow with CamelFileName header set before ZIP function
Now, i have changed the IFlow to set the header ‘ CamelFileName‘ before the ZIP function .
CamelFileName set in Content Modifier before ZIP Function
No FileName in SFTP Receiver Channel
When we try to hit the IFlow with XML, we have following zip file in the sftp target folder. Also the unzip file contains the same file name pattern as intended.
The explanation for above behavior could be due to the fact that CamelFileName header is generated after zip compression and assigned a random value if not assigned before. I leave it upto the readers to build a basic flow and use simulation to understand the same.
One more behaviour is that CamelFileName header is lost when used in SFTP Adapter. I don’t have a concrete answer for that atleast now.
Update 1: CamelFileName header when set in Iflow is chosen as filename for SFTP receiver adapter when filename parameter is blank .When CamelFileName header is explicitly set as filename in SFTP Adapter, dynamic value of CamelFileName header is lost!
Govardanam Venkatesan , interesting blog post. I believe the explanation of the behaviour is explained in the Apache Camel documentation...
Zip File documentation: The name of the Zip entry inside the created Zip file is based on the incoming
CamelFileName
message header, which is the standard message header used by the file component. Additionally, the outgoingCamelFileName
message header is automatically set to the value of the incomingCamelFileName
message header, with the ".zip" suffix. So for example, if the following route finds a file named "test.txt" in the input directory, the output will be a Zip file named "test.txt.zip" containing a single Zip entry named "test.txt"In the SFTP component documentation, it explains how setting up a value takes precedence over what is set in CamelFileName header - https://camel.apache.org/components/3.17.x/sftp-component.html#_endpoint_query_option_fileName.
Thanks Antonio Maradiaga ! Behaviour with respect to ZIP function is understood in SAP CI . But further to enrich the CamelFileName header topic, let us take the below basic case, you can see that using content Modifier, before the zip function , CamelFileName header is set and also used in the SFTP Adapter configuration! So no overwritten case here using Filename header.
When I try to hit the IFlow with XML, I have following file in SFTP ..${header.CamelFileName} is not carrying any dynamic value to the SFTP folder.
I tried two others headers in place of CamelFileName header in both Content Modifier & SFTP adapter
Though the headers carried dynamic Value to the SFTP Folder, we have issues with ZIP function as if CamelFileName header is not set before zip function, it generated a filename with {message-id}.zip
As you've created the CamelFileName header in the content modifier, there is no need to specify a FileName in the SFTP receiver adapter, see doc. You can leave it blank. Have you tried that? That said, you've specified an expression and it should evaluate it but it seems like it is not doing that for a reason.
Seems to Work with Blank Filename parameter in SFTP Receiver Adapter. Updated the Blog accordingly.