compressing & decompressing in mule
TRANSCRIPT
Compressing/Decompressing Mule
By Anirban Sen Chowdhary
We often require to compress our payload in our flow and again decompress it back to it’s original size at the end of the operation
So, How can we compress/decompress payloads in Mule ??
.
Fortunately we have gzip-compress-transformer and gzip-uncompress-transformer available in our Mule
So, to compress a payload in our Mule flow, we can use gzip-compress-transformer as follows :-
As you can see we have used a file inbound to pick a file in our flow, and then we compress it with gzip-compress-transformer and the output of the flow will produce a compressed file
Our corresponding Mule flow will be as follows :- <flow name="GZipCompress" doc:name="GZipCompress">
<file:inbound-endpoint path="E:\backup\test"responseTimeout="10000" doc:name="File"><file:filename-regex-filter pattern="abc.doc"caseSensitive="false" /></file:inbound-endpoint>
<string-to-byte-array-transformerdoc:name="String to Byte Array" /><loggermessage="Payload size before compression : #[Integer.parseInt(payload.size())/1024] KB"level="INFO" doc:name="Logger" />
<!-- If you send gzip a String then it gets serialized and mess ends up in the gzip file. To avoid this convert to byte[] first --><gzip-compress-transformer />
<loggermessage="Payload size after compression : #[Integer.parseInt(payload.size())/1024] KB"level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="E:\backup\test\newfolder"responseTimeout="10000" doc:name="File" /></flow>
As you can see in the code it will pick a file called abc.doc from E:\backup\test location , compress it and put the compressed file to E:\backup\test\newfolder
So, let us place a file abc.doc in the source folder as follows :-
You can see the file size is about 83 KB before compression
Let run our application and we can see, the file has been transferred to location E:\backup\test\newfolder
You can also see in the log that the payload size before compression was 83.0 KB and size after compression is 21.9912 KB
We can see our compressed file here in location E:\backup\test\newfolder :-
Now what about decompressing the file back to the original size ???
In case of decompressing a file back to original size ( Size before the compression) we will be using gzip-uncompress-transformer as follows:-
As you can see, it will pick up the compressed file abc.doc from the location E:\backup\test\newfolder and put it into a new location E:\backup\test\originalFileSize
The corresponding our Mule flow will be :-
<flow name="GZipUnCompress" doc:name="GZipUnCompress"initialState="started"><file:inbound-endpoint path="E:\backup\test\newfolder"responseTimeout="10000" doc:name="File"><file:filename-regex-filter pattern="abc.doc"caseSensitive="false" /></file:inbound-endpoint><gzip-uncompress-transformer />
<byte-array-to-string-transformerdoc:name="Byte Array to String" /><file:outbound-endpoint path="E:\backup\test\originalFileSize"responseTimeout="10000" doc:name="File" /></flow>
Now if we run the flow, we will get the following in our console :-
The file has been decompressed back to it’s original size into the location E:\backup\test\originalFileSize
Hope you enjoyed the tips of compressing and decompressing using gzip-compress-transformer/gzip-uncompress-transformer in Mule
Next slide I will bring some more simple yet interesting topic
Thank You