Skip to Content
Author's profile photo Raghu Vamseedhar Reddy Kadipi Reddy

Preserve file names of input Zip file – using Java Mapping.

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.

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Eng Swee Yeoh
      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

      Author's profile photo Raghu Vamseedhar Reddy Kadipi Reddy
      Raghu Vamseedhar Reddy Kadipi Reddy
      Blog 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.

      Author's profile photo Former Member
      Former Member

      Hi Raghu,

      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.

      You said that we can have diferent fixed folder for this scenario.

      Can you please say the command for that one.

      Thanks & Regards,

      Mounika

      Author's profile photo Shawn Tan
      Shawn Tan

      Hi Raghu,

      I've tried your code but it seems like it is not working if i have 2 xml files in the same zipped folder. Somehow i only get only 1 output file after it was unzip. Can you please help me as i have the similar requirement? Thanks.

       

      Regards,
      Shawn