Search code examples
mulemule-studiomule-componentanypoint-studiomule-esb

GZip uncompress component issue


I am working on mule flow and running into difficulties

Requirement:

  1. Get the ZIP file from Payload

  2. Unzip it and get the list of file names

  3. Zip it back

  4. FTP the same

I am running into 2 issues

  1. Get the original FileName - I tried different combinations like #[message.inboundProperties.originalFileName] but getting null value.
  2. Unzip file

Below is my flow(I have not included FTP and ZIP flow as running into issues before FTP) enter image description here


Solution

  • I'm a tad confused by your requirements as described, the use of GZip compression, and you're flow screenshot.

    Your requirement suggests you're going to be loading a Zip archive (file), which is very different to a GZip stream - the GZip connector won't help you here. GZip simply compresses a stream of bytes, it doesn't have the concept of multiple files/folders within it - this is why in the *nix world it's usually combined with Tar (i.e. Tar joins together many files into a single, uncompressed archive and that archive is then GZip'd). Zip essentially combines both of these - archive and compress.

    Also, your screenshot shows a HTTP inbound endpoint - so I assume the file is being posted to you. #[message.inboundProperties.originalFileName] is only set by a File inbound endpoint, which is why you won't be seeing it. You also don't mention what content type you'll be working with. The most common use case I've seen is multipart/form-data - and if you're using that then you can access the file and it's name via Mule's inboundAttachments map.

    If my understanding of what you're doing is correct, the following is an example of how I would implement it (error checking/handling removed for brevity):

    Sample flow

    Source:

        <flow name="sampleFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/zip" allowedMethods="POST" doc:name="HTTP"/>
        <foreach collection="#[message.inboundAttachments]" doc:name="For Each Attachment (There could be more than 1)">
            <scripting:component doc:name="Groovy - examine attachment and set vars">
                <scripting:script engine="Groovy"><![CDATA[import java.io.*;
    import java.util.zip.*;
    
    // should be application/zip - could valudate
    flowVars.type = payload.dataSource.contentType;
    
    // datasource will be HttpPartDataSource
    flowVars.partName = payload.dataSource.part.name;
    flowVars.filename = payload.dataSource.part.fileName;
    flowVars.filesInZip = new java.util.ArrayList();
    
    payload = payload.dataSource.content;
    
    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(payload));
    ZipEntry ze;
    while ((ze = zis.getNextEntry()) != null)
        flowVars.filesInZip.add(ze.name);
    zis.close();
    
    return payload;
    
    ]]></scripting:script>
            </scripting:component>
            <ftp:outbound-endpoint host="localhost" port="21" path="/dest" passive="true" outputPattern="#[flowVars.filename]" responseTimeout="10000" doc:name="FTP"/>
            <dw:transform-message doc:name="Transform Message">
                <dw:set-session-variable variableName="results"><![CDATA[%dw 1.0
    %output application/java
    ---
    (sessionVars.results default []) ++ [{
        partName: flowVars.partName,
        fileName: flowVars.filename,
        filesInZip: flowVars.filesInZip joinBy ', '
    }]
    ]]></dw:set-session-variable>
            </dw:transform-message>
        </foreach>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
    %output application/json
    ---
    sessionVars.results
    ]]></dw:set-payload>
        </dw:transform-message>
    </flow>
    

    Postman Sample Call:

    Postman Sample

    Output:

    [
        {
            "partName": "zip2",
            "fileName": "OC.zip",
            "filesInZip": "OC/staging/, OC/staging/b2b-test/, OC/staging/Madrid/, OC/staging/Madrid/B1805259-1527220273.txt, OC/staging/Madrid/B1805259-1527221226.txt, OC/staging/Madrid/B1805259-1527226248.txt, OC/staging/Madrid/B1805259-1527226249.txt, OC/staging/Madrid/B1805259-1527226475.txt, OC/staging/Madrid/B1805259-1527226476.txt, OC/staging/Madrid/B1805299-1527544507.txt, OC/staging/Madrid/B1805299-1527545678.txt, OC/staging/Madrid/B1805299-1527545679.txt, OC/staging/Madrid/B1805299-1527548307.txt, OC/staging/Madrid/B1805299-1527550180.txt, OC/staging/Madrid/B1805299-1527553547.txt, OC/staging/Madrid/B1805299-1527553548.txt"
        },
        {
            "partName": "zip1",
            "fileName": "FirstZipFile.zip",
            "filesInZip": "LogitechWebcamFix.log, BGChanger.log"
        }
    ]