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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • Time to First Token: How Long Does It Take for a Large Model to Start Responding?
  • Tracing Dangerous Python Code Paths With rce-path
  • Prompt Caching for LLMs, Part 2: Semantic Caching in Production
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Compression Module Mule 4

By 
Riddhi Thacker
·
Vikalp Bhalia
·
Sep. 16, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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