cancel
Showing results for 
Search instead for 
Did you mean: 

SAP PI 7.3 - Unzip files using PayloadZipBean

arthur_fuscella
Participant
0 Kudos

Hello everyone,

I know there are several posts about it, but I already tried many of them. My problem starts when my Mail adapter receives a zip file with a xml inside.

When a compressed file is received I should unpack and use it. I noticed that my communication channel unpacks the files, but the interface retains the zip during the whole process.

I have the following modules in my communication channel:



It's good to highlight that an email may contain a xml and/or a zip file.



Best regards

Arthur Silva

Accepted Solutions (1)

Accepted Solutions (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Arthur,

"an email may contain a xml and/or a zip file".

You have to use Java Mapping, to handle this. Use InputAttachments class to loop through attachments and if attachments contain zip file, unzip it.

Please search SCN for related Java code. FYI.

Generated Documentation (Untitled)

Mail to File Scenario | SCN

arthur_fuscella
Participant
0 Kudos

Hello Raghu,

Thanks for the links provided. Yes, I am already using JavaMapping for my solution to handle with xml files, and now with zip files. I decided to ignore any configuration on communication channel and treat the attachments in execution time, directly in JavaMapping layer.

Here's a sample of how to use zip classes to unpack files in JavaMapping:


package br.com.sap.test;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.Collection;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.Attachment;

import com.sap.aii.mapping.api.InputAttachments;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class TreatZipAttachment extends AbstractTransformation {

     public ArrayList<String> al = new ArrayList<String>();

     public String attachID = "";

     public int i = 0;

     public static final int BUFFER_SIZE = 4096;

          @Override

          public void transform(TransformationInput arg0, TransformationOutput arg1)

                    throws StreamTransformationException {


               InputAttachments inAttachs = arg0.getInputAttachments();

          /*

           * The code below loops the attachments list looking for zip files.

           * Later, the code unpack them adding the content in an ArrayList for

           * later processing. 

           */

               Collection<String> Attachs = inAttachs.getAllContentIds(true);

               Object[] attachs = Attachs.toArray();

               for (i = 0; i < attachs.length; i++) {

                    attachID = (String) attachs[i];

                    // attachment name

                    Attachment a = inAttachs.getAttachment(attachID);

                    if (isZip(getAttachmentName(a.getContentType()))) {

                    // unpack file

                    ZipInputStream zis = new ZipInputStream(

                    new ByteArrayInputStream(a.getContent()));

                    try {

                         ZipEntry ze = zis.getNextEntry();

                         ze = zis.getNextEntry();

                         // create new byte array

                        ByteArrayOutputStream baos = new ByteArrayOutputStream();

                        while (ze != null) {

                             getTrace().addWarning("Reading file: " + ze.getName());

                             byte[] buffer = new byte[BUFFER_SIZE];

                             int read = 0;

                                  while ((read = zis.read(buffer, 0, buffer.length)) != -1) {

                                       baos.write(buffer, 0, read);

                                  }

                        baos.flush();

                        al.add(bytes2String(baos.toByteArray()));

                        zis.closeEntry();

                        ze = zis.getNextEntry();

                        }

                    } catch (IOException e) {

                    // TODO Auto-generated catch block

                         e.printStackTrace();

                    }

               }

          }

     }

     public String bytes2String(byte[] bytes)

               throws StreamTransformationException, UnsupportedEncodingException {

          if (bytes != null) {

               return new String(bytes, "UTF-8");

          }

          return null;

     }

     public String getAttachmentName(String content) {

          String fileName = "";

          fileName = content.substring(content.indexOf("name=\"") + 6);

          fileName = fileName.substring(0, fileName.length() - 1).toLowerCase();

          return fileName;

     }

      /*

       * only consider .zip extensions

       */

       public boolean isZip(String attachment) {

            if (attachment.matches("(.*)zip(.*)")) {

                 return true;

            }

            return false;

       }

}

RaghuVamseedhar
Active Contributor
0 Kudos

Arthur,

Thank you for sharing your solution. Please close this thread.

Answers (0)