top of page

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


  1. Create or open a Mule project in Anypoint Studio 7.x.


  2. Log in with your Anypoint Platform credentials (if you aren’t already logged in).


  3. In the Mule Palette, add the "Compression Module - Mule 4" from Exchange as a dependency.





Step 2: Create Archive Mule Flow





  1. 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")


  1. 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.



  1. 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"
}  



  1. Run in web browser (i.e. Chrome) to auto download. You may run it via Postman but you have to manually save it.



  1. 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.



Github Repository


Nov 11

2 min read

0

11

1
2
bottom of page