Contact Us 1-800-596-4880

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.

  1. In Studio, drag a Scheduler trigger to the flow.

  2. Set Frequency to 15 and Time unit to SECONDS.

  3. Drag the List operation alongside the Scheduler trigger.

  4. Add the connector configuration.

  5. Set Directory path to input.

  6. Drag a Set payload component alongside the List operation.

  7. Set Value to the following DataWeave expression:

output application/java
(0 to sizeOf(payload) - 1) as Array
reduce (index, acc={}) ->
acc ++ { (payload[index].attributes.fileName): payload[index].payload}
  1. Drag the Archive operation alongside the Set payload component.

  2. Drag the Extract operation alongside the Archive operation.

  3. Drag the For Each scope.

  4. Set Collection to payload.

  5. Drag the Write operation into the For Each scope.

  6. Set Path to #[output application/json--- "output/" ++ (payload pluck $$)[0]].

  7. Set Content to [CDATA[#[output application/java --- ( payload pluck $ )[0]]].

  8. Drag a Logger component right to the For Each scope.

  9. Set Message to output application/json --- payload.

  10. Save and run your Mule app.

Compression Module Archive Extract Flow in Studio Canvas

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"/>
	<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={}) -&gt;
acc ++ { (payload[index].attributes.fileName): payload[index].payload}]' doc:name="Set Payload" />

	<compression:archive doc:name="Archive" >
			<compression:zip-archiver />

	<compression:extract doc:name="Extract"  >
		<compression:extractor >
			<compression:zip-extractor />

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

	<logger level="INFO" doc:name="Logger" message="#[output application/json --- payload]"/>
View on GitHub