See other Articles
File Management: How to Archive or Zip Files in MuleSoft
Nov 11
2 min read
0
11
Introduction
Managing files effectively is important for smooth system integrations. Archiving helps organize and store files more efficiently.
This blog will show you how to archive files in MuleSoft, step by step, to make file management easier.
Prerequisites
Create sample raw files (e.g., a .txt file) containing your desired content.
Add Compression Module to your Mule Palette.
Archive Files in Anypoint Studio
Step 1: Add Compression Module
Create or open a Mule project in Anypoint Studio 7.x.
Log in with your Anypoint Platform credentials (if you aren’t already logged in).
In the Mule Palette, add the "Compression Module - Mule 4" from Exchange as a dependency.
Step 2: Create Archive Mule Flow
Read any file using DataWeave function readUrl. This can read files located under src/main/resources in a Mule project. These are parameters of the readUrl function - readUrl(url: String, contentType: String = "application/dw", readerProperties: Object = {}): Any.
%dw 2.0
output text/plain
---
readUrl("classpath://sample-file.txt", "text/plain")
Archive the file using the Archive operation of Compression Connector module.
In this example, we will demonstrate how to archive two (2) files: one sourced from an existing file and another created from a hardcoded string.
We will also create a folder named "files" to store these archived files.
Set the outbound headers using Transform Message and DataWeave.
Define a variable (i.e. outboundHeaders).
Make sure the variable name is similar to what is defined in the response configuration of your HTTP Listener.
{
"Content-Disposition": "attachment; filename=\"my-archive.zip\"",
"Content-Type": "application/zip"
}
Run in web browser (i.e. Chrome) to auto download. You may run it via Postman but you have to manually save it.
Examine the .zip file if it contains the files.
Conclusion
Archiving files in MuleSoft is a simple but effective way to keep your data organized and manageable. By following the steps in this guide, you can streamline your file management process and improve the efficiency of your integrations.