Gzip Compress & Gzip Uncompress Transformer in Mule

Gzip Compress & Gzip Uncompress Transformer in Mule

How to compress & uncompress a file in mule ?

We can do it by Gzip Compress & Gzip Uncompress Transformers in Mulesoft.

Gzip Compress Transformer : It compress byte array or file.We can reduce file size by this transformer.

Gzip Uncompress Transformer : It is used to uncompress byte array or a file and restored it back in original size.

The Compression module supports and provides access to the most-used file archiver and compression formats through a Mule app.

A file archiver is a computer program that combines a number of files together into one archive file for easier transportation or storage, and most archivers employ data compression in their archive formats to reduce the size of the final archive. However, not all compression algorithms can archive multiple files. Some are limited to single-file data compression.

This module compresses and decompresses files for each of the available formats without losing any of the features that the format associated algorithm provides.
To achieve this capability, the module provides two pairs of operations, one for compressing and decompressing a single file, another for archiving and extracting a bundle of files. Each of these operations can be configured with the compression or archiving strategies of your choice.

flow of Compress & Gzip Uncompress Transformer :

In above flow, a file is compressed by Gzip Compress transformer and stored in output folder of src/main/resources. In second flow, file connector picks that compressed file and uncompress that by Gzip Uncompress transformer.We can compare file size of both the files in file's properties.

All files are in src/main/resources :

XML project code :

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
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/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
    <file:connector name="File" outputPattern="output.json" writeToDirectory="src/main/resources/output" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>
    <file:connector name="File2" outputPattern="original_input.json" autoDelete="false" streaming="false" validateConnections="true" doc:name="File" writeToDirectory="src/main/resources/uncompressed"/>
    <file:connector name="File1" outputPattern="input.json" writeToDirectory="src/main/resources/input" autoDelete="false" streaming="false" validateConnections="true" doc:name="File"/>
    <flow name="test_objecttostringFlow">
        <file:inbound-endpoint path="src/main/resources/input" connector-ref="File1" responseTimeout="10000" doc:name="File"/>
        <gzip-compress-transformer doc:name="Gzip Compress"/>
        <file:outbound-endpoint path="src/main/resources/output" outputPattern="output.json" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <flow name="test_gzipcompressFlow">
        <file:inbound-endpoint path="src/main/resources/output" connector-ref="File" responseTimeout="10000" doc:name="File"/>
        <gzip-uncompress-transformer doc:name="Gzip Uncompress"/>
        <file:outbound-endpoint path="src/main/resources/uncompressed" outputPattern="original_input.json" connector-ref="File2" responseTimeout="10000" doc:name="File"/>