Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using the GZIP Compress and Uncompress Transformer With MuleSoft

DZone's Guide to

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.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

GZIP 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.

Image title

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.

Image title

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):

Image title

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.

Image title

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:

Image title

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!

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mulesoft ,message payload ,compress ,uncompress ,integration ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}