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
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • AWS Kiro: The Agentic IDE That Makes Specs the Unit of Work
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • The Cost of Knowing: When Observability Becomes the Outage
  • The 7 Pillars of Meeting Design: Transforming Expensive Conversations into Decision Assets

Compression Module Mule 4

By 
Riddhi Thacker user avatar
Riddhi Thacker
·
Vikalp Bhalia user avatar
Vikalp Bhalia
·
Sep. 16, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
15.0K Views

Join the DZone community and get the full member experience.

Join For Free

Whenever there is a large file or more than one file, we use tools like 7zip, WinZip to compress or create a zip file, and similarly, we decompress or unzip the file.

But what if we have a similar requirement in an Integration project?

Mule 4 comes with a Compression Module which can:

  • Compress/Decompress a file or payload
  • Zip/Unzip files

NOTE: Github repository with the Mule Project can be found at the end of the post. 

Configuration

To add the Compression Module in the Mule Palate:

  • Click on "Search on Exchange" 
  • Search for Compression Module
  • Click on add and finish.
    Adding dependency to project

Once you complete the above steps you will be able to see the Compression Module in the Mule Palate

Mule Palate

Operations

Archive/Extract

Archive and Extract are the functions used to zip and unzip the files respectively.

Archive

This operation accepts a map where the key is the file name and value is content.

Example:

JSON
x
 
1
{
2
    "files/csv.txt": vars.csvText,
3
    "files/html.txt": vars.htmlText,
4
    "files/json.txt": vars.jsonText,
5
    "files/zip.txt": vars.zipText
6
}


Below is the screenshot of the flow.

Application workflow
The above flow will create an archive having 4 files in a files folder.

Plain Text
 




xxxxxxxxxx
1


 
1
+ zipped-file
2
  + files
3
    - csv.txt
4
    - html.txt
5
    - json.txt
6
    - zip.txt



Extract

This operation accepts an archived file and returns a map.

Plain Text
 




x


 
1
{
2
    "files/csv.txt": "File content",
3
    "files/html.txt": "File content",
4
    "files/json.txt": "File content",
5
    "files/zip.txt": "File content"
6
}



And the below code will convert the object in an array.

Java
 




xxxxxxxxxx
1


 
1
payload pluck {
2
    fileName: $$,
3
    content: $
4
}



Below is the screenshot of the flow.

unzip workflow

Errors

Archive

  • COMPRESSION:COULD_NOT_COMPRESS – if any error occurs while compressing the content

Extract

  • COMPRESSION:INVALID_ARCHIVE – If the content is not in the configured format
  • COMPRESSION:COULD_NOT_DECOMPRESS – Other errors occurred during compression process


Compress/Decompress

The compress and decompress are straight forward.

Compress

The module provides 2 compressor techniques:

  • Zip Compressor.
  • GZip Compressor.

Below is the sample flow:

Compression workflow

Decompress

Select the same compressor technique which was used to compress the file.

Below is the sample flow:

Decompression workflow


Conclusion

If you are dealing with a large payload or if you have a size limit on payload transfer or you need to extract some file or create a zip file, the Compression module is a good fit.

Github Repository

compression-module-demo

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook