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 Video Library
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
View Events Video Library
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

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Containers Trend Report: Explore the current state of containers, containerization strategies, and modernizing architecture.

Low-Code Development: Learn the concepts of low code, features + use cases for professional devs, and the low-code implementation process.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Error Handling Wikipedia in MuleSoft
  • Implementing NetSuite Add Records Operation With MuleSoft - Part II
  • Implementing MUnit And MUnit Matchers With MuleSoft
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial

Trending

  • Announcing DZone Core 2.0!
  • What’s New Between Java 17 and Java 21?
  • Apply Strangler Pattern To Decompose Legacy System Into Microservices: Part 1
  • Navigating NoSQL: A Pragmatic Approach for Java Developers
  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
DZone Core CORE ·
Apr. 04, 17 · Tutorial
Like (5)
Save
Tweet
Share
18.8K 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.

Related

  • Error Handling Wikipedia in MuleSoft
  • Implementing NetSuite Add Records Operation With MuleSoft - Part II
  • Implementing MUnit And MUnit Matchers With MuleSoft
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial

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
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: