output application/java
---
(0 to sizeOf(payload) - 1) as Array
reduce (index, acc={}) ->
acc ++ { (payload[index].attributes.fileName): payload[index].payload}
Archive and Extract Example
The following Compression module example shows how to archive and extract files from a folder. The File Connector List operation lists all the files from a folder named input
. The Compress operation then creates a compressed archive by generating an object with the key set to the file name and the value set to the value of the listed files.
Subsequently, the Extract operation decompresses the archived file. Then, the For Each scope iterates over each extracted key/value pair and writes the file to a new output folder with the key as the new file name and the value as the corresponding extracted file content.
-
In Studio, drag a Scheduler trigger to the flow.
-
Set Frequency to
15
and Time unit toSECONDS
. -
Drag the List operation alongside the Scheduler trigger.
-
Add the connector configuration.
-
Set Directory path to
input
. -
Drag a Set payload component alongside the List operation.
-
Set Value to the following DataWeave expression:
-
Drag the Archive operation alongside the Set payload component.
-
Drag the Extract operation alongside the Archive operation.
-
Drag the For Each scope.
-
Set Collection to
payload
. -
Drag the Write operation into the For Each scope.
-
Set Path to
#[output application/json--- "output/" ++ (payload pluck $$)[0]]
. -
Set Content to
[CDATA[#[output application/java --- ( payload pluck $ )[0]]]
. -
Drag a Logger component right to the For Each scope.
-
Set Message to
output application/json --- payload
. -
Save and run your Mule app.
In the Configuration XML editor, the configuration looks like this:
<flow name="archive-extract-test" >
<scheduler doc:name="Scheduler">
<scheduling-strategy >
<fixed-frequency frequency="15" timeUnit="SECONDS"/>
</scheduling-strategy>
</scheduler>
<file:list doc:name="List" config-ref="File_Config" directoryPath="input"/>
<set-payload value='#[output application/java
---
(0 to sizeOf(payload) - 1) as Array
reduce (index, acc={}) ->
acc ++ { (payload[index].attributes.fileName): payload[index].payload}]' doc:name="Set Payload" />
<compression:archive doc:name="Archive" >
<compression:archiver>
<compression:zip-archiver />
</compression:archiver>
</compression:archive>
<compression:extract doc:name="Extract" >
<compression:extractor >
<compression:zip-extractor />
</compression:extractor>
</compression:extract>
<foreach doc:name="For Each" collection="payload">
<file:write doc:name="Write" config-ref="File_Config" path='#[output application/json
---
"output/" ++ (payload pluck $$)[0]]' >
<file:content ><![CDATA[#[output application/java --- ( payload pluck $ )[0]]]]></file:content>
</file:write>
</foreach>
<logger level="INFO" doc:name="Logger" message="#[output application/json --- payload]"/>
</flow>
</mule>