DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Using the GZIP Compress and Uncompress Transformer With MuleSoft

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.

Jitendra Bafna user avatar by
Jitendra Bafna
CORE ·
Apr. 04, 17 · Tutorial
Like (5)
Save
Tweet
Share
17.18K Views

Join the DZone community and get the full member experience.

Join For Free

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!

MuleSoft Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Mind Map Reuse in Software Groups
  • Taming Cloud Costs With Infracost
  • Hidden Classes in Java 15
  • What Is Policy-as-Code? An Introduction to Open Policy Agent

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: