Requirement: –

Input Zip file has Order123.txt and Order123.JPG files in it i.e. combination of text(csv, xml) and binary(image, pdf) files. Input Zip file should be unzipped and file names should be preserved i.e., output folder should have Order123.txt (with or without transformation) and Order123.JPN.

Solution: –

A1.png

Design: –

Create dummy Data Type, Message Type and Outbound/Inbound Service Interfaces.

A2.png

A3.png

Parametrized Java Mapping: –


package com.map; 
import com.sap.aii.mapping.api.*; 
public class UnZipParticularFile extends AbstractTransformation { 
    @Override 
    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException { 
        try { 
            java.io.InputStream inputstream = transformationInput.getInputPayload().getInputStream(); 
            java.io.OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream(); 
            String fileNameP = transformationInput.getInputParameters().getString("FileNameP"); // Get Parameter. 
            // A. Unzip and write required file to output. 
            java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(inputstream); 
            java.util.zip.ZipEntry ze; 
            while ((ze = zis.getNextEntry()) != null) {
                //You can use String class methods like endsWith, toLowerCase, contains, matches. Update entry in Interface Determination of ICO accordingly.
                if (ze.getName().matches(fileNameP)) { //ze.getName() is File Name. 
                    int len; 
                    byte b[] = new byte[2048]; 
                    while ((len = zis.read(b, 0, 2048)) != -1) { 
                        outputstream.write(b, 0, len); 
                    } 
                    break; 
                } 
            } 
            // B. Set target File Name and Folder. 
            java.util.Map mapParameters = transformationInput.getInputHeader().getAll(); 
            mapParameters.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/Dynamic", StreamTransformationConstants.DYNAMIC_CONFIGURATION), ""); 
            DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION); 
            conf.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName"), ze.getName()); //ze.getName() is File Name. You can add Date prefix or some-other string according to requirement. 
            conf.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "Directory"), "/usr/sap/interfaces/MWS/out");  //You can have different fixed folder or dynamic folder according to requirement.
        } catch (Exception exception) { 
            getTrace().addDebugMessage(exception.getMessage()); 
            throw new StreamTransformationException(exception.toString()); 
        } 
    } 
}

Configuration: –

Remove SWCV as input is binary file (Zip file).

A4.png

A5.png

If transformation is required for text file, create new Operational Mapping and add Message Map after Java Map.

A6.png

Result: –

Input Zip file is unzipped and output file names are same as input file names.

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Eng Swee Yeoh

    Hi Raghu

    Just a suggestion for enhancement.

    I think the matches() method is case sensitive. You might want to consider translating both the actual filename and the parameter value to the same upper/lower case (using temp variables) to compare.

    Rgds

    Eng Swee

    (0) 
    1. Raghu Vamseedhar Reddy KadipiReddy Post author

      Eng,

      Thank you for suggestion. I have added comment in code about using other methods of String class.

      @ Readers,

      boolean matches(String regex), method of String class takes regular expression as an input. This is the reason for .*txt (not *.txt), period matches a characters.

      (0) 

Leave a Reply