Mule Tip: Unzipping files in Mule ESB
Mule runtime includes many connectors and transformers from basic to advanced level. We often need to consume zip files and there is no transformer to unzip files. Let’s see how we can unzip a file in Mule ESB flow. I will be using Mule Community Runtime 3.8.1 for this example.
|In general, when working with file system where network shared folders are involved, be careful about folder permissions. Sometime it gives unexpected results.|
Option 1: Use FileUtils class
Mule runtime has a very useful class
org.mule.utils.FileUtil which has many helper methods. One of those is
unzip(File archive, File directory). We can use expression component to call this method. This flow will read a sample.zip file and unzip it in the staging directory.
<flow name="mule-demosFlow"> <poll doc:name="Poll"> <set-payload value="#[new File('inbound/staging/sample.zip').exists();]" doc:name="Set Payload"/> </poll> <choice doc:name="Choice"> <when expression="#[payload == true]"> <expression-component doc:name="Expression"><![CDATA[org.mule.util.FileUtils.unzip(new File('inbound/staging/sample.zip'), new File('inbound/staging/')); new File('inbound/staging/sample.zip').delete();]]></expression-component> <logger message="#['Extracted file exists: '+ new File('inbound/staging/sample.txt').exists()]" level="INFO" doc:name="Logger"/> </when> <otherwise> <logger message="#['No input zip file']" level="INFO" doc:name="Logger"/> </otherwise> </choice> </flow>
Option 2: Use external library
There are many third party java libraries available that can unzip files. One of those I liked is zt-zip. This library has a utility class
org.zeroturnaround.zip.ZipUtil which defines multiple methods to zip/unzip files. To use this, first you will need to add the maven dependency in your project (you can check zt-zip for latest version):
<dependency> <groupId>org.zeroturnaround</groupId> <artifactId>zt-zip</artifactId> <version>1.11</version> <type>jar</type> </dependency>
Similar to above example you can then unzip a file in different ways, some examples are:
Unzips at given directory:
<expression-component doc:name="Expression"><![CDATA[org.zeroturnaround.zip.ZipUtil.unpack(new File('inbound/staging/sample2.zip'), new File('inbound/staging/sample2/')); new File('inbound/staging/sample2.zip').delete();]]></expression-component>`
Unzips in same folder as source zip:
<expression-component doc:name="Expression"><![CDATA[org.zeroturnaround.zip.ZipUtil.explode(new File('inbound/staging/sample3.zip')); new File('inbound/staging/sample3.zip').delete();]]></expression-component>
If you are reading zip with file inbound-connector, you may be able use a method that accepts
InputStream. You may need to verify that input file is moved or deleted as this may still keep the stream open, causing original file not to delete.
<expression-transformer expression="#[org.zeroturnaround.zip.ZipUtil.unwrap(payload,new File('inbound/staging2/')); return true;]" doc:name="Expression"/>
It is very easy to use java libraries in mule expression component. In this post, we saw an example of how we can unzip a file in mule flow. We will see more mule tips in future posts, so stay tuned.