Using the GZIP Compress and Uncompress Transformer With MuleSoft
There is often a requirement to compress or uncompress a message payload at the end of operations. MuleSoft has out-of-box GZIP Compress and Uncompress to achieve this.
Join the DZone community and get the full member experience.
Join For FreeGZIP Compress is used to compress a byte array and GZIP Uncompress is used to uncompress a byte array.
There is often a requirement that we need to compress or uncompress a message payload at the end of operations. MuleSoft provides out-of-box GZIP Compress and Uncompress to achieve this.
Now, we will walk through the GZIP Compress and Uncompress transformers.
Compress Message Payload Using GZIP
Place the file connector into the message source in the flow and configure it.
Now, you need to convert the message into a byte array. So, place the String to Byte Array transformer in the message processor region after the inbound file connector.
Place the GZIP Compress transformer after the String to Byte Array transformer.
Fields |
Type |
Description |
Display name |
String |
A unique name for GZIP Compress in your Mule application (no spaces). |
Encoding (optional) |
String |
A string encoding used for transformer output. |
mimeType (optional) |
String |
The mime type (i.e., text/plain or application/json). |
ignoreBadInput (optional) |
Boolean |
Many transformers only accept certain classes. Such transformers are never called with an inappropriate input (whatever the value of this attribute). If a transformer forms part of a chain and cannot accept the current message class, this flag controls whether the remaining part of the chain is evaluated. If true, the next transformer is called. If false, the chain ends, keeping the result generated up to that point. |
returnClass (optional) |
String | The class of the message generated by the transformer. This is used if transformers are auto-selected and to validate that the transformer returns the correct type. |
Finally, place the outbound file connector at the end of message processor. It will be used to store the compressed file.
Testing Application
Drop the plain file to the folder location that you have specified in inbound file connector. The outbound compress file can be seen at the folder location that you specified for outbound file connector.
Input message (1053 bytes):
<Employees>
<Employee>
<FirstName>Jitu</FirstName>
<LastName>Jain</LastName>
<Salary>100000</Salary>
</Employee>
<Employee>
<FirstName>Jospeh</FirstName>
<LastName>Adams</LastName>
<Salary>200000</Salary>
</Employee>
<Employee>
<FirstName>Steve</FirstName>
<LastName>Simon</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Brain</FirstName>
<LastName>Handscomb</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Rajeev</FirstName>
<LastName>Thandani</LastName>
<Salary>500000</Salary>
</Employee>
<Employee>
<FirstName>Jitu</FirstName>
<LastName>Jain</LastName>
<Salary>100000</Salary>
</Employee>
<Employee>
<FirstName>Jospeh</FirstName>
<LastName>Adams</LastName>
<Salary>200000</Salary>
</Employee>
<Employee>
<FirstName>Steve</FirstName>
<LastName>Simon</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Brain</FirstName>
<LastName>Handscomb</LastName>
<Salary>300000</Salary>
</Employee>
</Employees>
Output message (177 bytes):
The GZIP Compress transformer reduces the size of the original file by almost 5 times!
Code:
<Employees>
<Employee>
<FirstName>Jitu</FirstName>
<LastName>Jain</LastName>
<Salary>100000</Salary>
</Employee>
<Employee>
<FirstName>Jospeh</FirstName>
<LastName>Adams</LastName>
<Salary>200000</Salary>
</Employee>
<Employee>
<FirstName>Steve</FirstName>
<LastName>Simon</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Brain</FirstName>
<LastName>Handscomb</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Rajeev</FirstName>
<LastName>Thandani</LastName>
<Salary>500000</Salary>
</Employee>
<Employee>
<FirstName>Jitu</FirstName>
<LastName>Jain</LastName>
<Salary>100000</Salary>
</Employee>
<Employee>
<FirstName>Jospeh</FirstName>
<LastName>Adams</LastName>
<Salary>200000</Salary>
</Employee>
<Employee>
<FirstName>Steve</FirstName>
<LastName>Simon</LastName>
<Salary>300000</Salary>
</Employee>
<Employee>
<FirstName>Brain</FirstName>
<LastName>Handscomb</LastName>
<Salary>300000</Salary>
</Employee>
</Employees>
Uncompress Message Payload Using GZIP
For uncompressing the payload, you need to use GZIP Uncompress. The information for the display names, etc. is all the same.
Testing Application
Drop the compressed file to the folder location that you have specified in the inbound file connector. The outbound uncompress file can be seen at the folder location that you specified for outbound file connector.
Output:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="compressprojFlow">
<file:inbound-endpoint path="src/test/resources/compress" responseTimeout="10000" doc:name="File"/>
<file:file-to-byte-array-transformer doc:name="File to Byte Array"/>
<gzip-uncompress-transformer doc:name="Gzip Uncompress"/>
<file:outbound-endpoint path="src/test/resources/uncompress" responseTimeout="10000" doc:name="File"/>
</flow>
</mule>
Now, you know how to compress and uncompress the message payloads using GZIP with MuleSoft!
Opinions expressed by DZone contributors are their own.
Comments