2

I'm reading a file from File Component, example file name as TestCustomer.xml. I need to compress the file ( ZIP format) and place it on file component with the name TestCustomer.zip. While extracting the file only, it should reflects as TestCustomer.xml externally.

I have tried in multiple ways, but it is not working as expected outbound folder outputPattern.

  1. File pattern as 'TestCustomer.xml.zip' - Response as 'TestCustomer.xml.zip' ( But expected should be TestCustomer.zip)
  2. Tried as 'TestCustomer.zip' - Response as 'TestCustomer.zip' ( But when I extract it, it is in file format not in xml format).
  3. Tried as TestCustomer.xml - Response as TestCustomer.xml ( not able to extract the file since it is not in proper format).

    <flow name="testzipFlow1" doc:name="testzipFlow1">
    <file:inbound-endpoint path="c:/in" connector-ref="File" responseTimeout="10000" doc:name="File"/>
    <logger level="INFO" doc:name="Logger"/>
    <gzip-compress-transformer></gzip-compress-transformer>
      <file:outbound-endpoint outputPattern="TestCustomer.xml.zip" responseTimeout="10000" doc:name="File" path="c:/output">
       </file:outbound-endpoint>
    </flow>
    

Let me know if you have thoughts on this to achieve in desired fileName. Thanks in advance.

star
  • 1,493
  • 1
  • 28
  • 61

1 Answers1

1

Use a Regular Expression to change the filename. For example:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.6.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
    <flow name="stackoverflowplaygroundFlow">
        <file:inbound-endpoint path="/tmp/mule/in"
            responseTimeout="10000" doc:name="Inbox">
        </file:inbound-endpoint>
        <logger message="#[flowVars.originalFilename]" level="WARN"
            doc:name="Logger" />
        <gzip-compress-transformer />
        <file:outbound-endpoint path="/tmp/mule/out"
            outputPattern="#[regex('^(.*)\\.xml$', flowVars.originalFilename) + '.gz']"
            responseTimeout="10000" doc:name="File" />
    </flow>
</mule>

The important step is to set the outputPattern:

#[regex('^(.*)\\.xml$', flowVars.originalFilename) + '.gz']

It takes the original filename, removes the trailing .xml, and adds .gz indestead.

  • Thanks for the response. I have tried the above syntax for the below example. It is not working. Could you please give me the Regex expression. For example my file is `TestCustomer.xml.xml`and I need to zip it by `TestCustomer.xml.gz`. But when I externally extracting it should reappear like `TestCustomer.xml.xml` – star Aug 21 '15 at 00:34
  • I have tried the syntax `#[regex('^(.*)\\.xml.xml$', flowVars.originalFilename) + '.xml.gz']`, it is correctly giving me the zip file name as `TestCustomer.xml.zip` for the expression but when i extracting it , it appear like `TestCusomer.xml`. Your thoughts will be helpful. Because I have similar scenario's. – star Aug 21 '15 at 00:42