Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

Compressing & decompressing in mule

  1. By Anirban Sen Chowdhary
  2. .
  3. Fortunately we have gzip-compress-transformer and gzip- uncompress-transformer available in our Mule
  4. 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
  5. Our corresponding Mule flow will be as follows :- <flow name="GZipCompress" doc:name="GZipCompress"> <file:inbound-endpoint path="E:backuptest" responseTimeout="10000" doc:name="File"> <file:filename-regex-filter pattern="abc.doc" caseSensitive="false" /> </file:inbound-endpoint> <string-to-byte-array-transformer doc:name="String to Byte Array" /> <logger message="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 /> <logger message="Payload size after compression : #[Integer.parseInt(payload.size())/1024] KB" level="INFO" doc:name="Logger" /> <file:outbound-endpoint path="E:backuptestnewfolder" responseTimeout="10000" doc:name="File" /> </flow>
  6. As you can see in the code it will pick a file called abc.doc from E:backuptest location , compress it and put the compressed file to E:backuptestnewfolder 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
  7. Let run our application and we can see, the file has been transferred to location E:backuptestnewfolder 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
  8. We can see our compressed file here in location E:backuptestnewfolder :-
  9. Now what about decompressing the file back to the original size ???
  10. 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:backuptestnewfolder and put it into a new location E:backuptestoriginalFileSize
  11. The corresponding our Mule flow will be :- <flow name="GZipUnCompress" doc:name="GZipUnCompress" initialState="started"> <file:inbound-endpoint path="E:backuptestnewfolder" responseTimeout="10000" doc:name="File"> <file:filename-regex-filter pattern="abc.doc" caseSensitive="false" /> </file:inbound-endpoint> <gzip-uncompress-transformer /> <byte-array-to-string-transformer doc:name="Byte Array to String" /> <file:outbound-endpoint path="E:backuptestoriginalFileSize" responseTimeout="10000" doc:name="File" /> </flow>
  12. 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:backuptestoriginalFileSize
  13. 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
Advertisement