Skip to Content
Author's profile photo Xavier San Sebastián

How to zip and encode a field as base64 with a UDF in a Message Mapping

It’s possible that in some scenarios you should compress  as a ZIP and send a document through  a WebService in base64. For achieve this we can use UDF and some libraries.

The UDF will be this:


public String invoice2zip(String invoice, String filename, Container container) throws StreamTransformationException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos); 
 BASE64Encoder encoder = new BASE64Encoder();
String encoded = "";
try{
     zos.putNextEntry(new ZipEntry(filename)); 
    zos.write(invoice.getBytes());
     zos.close();
      encoded = encoder.encode( baos.toByteArray());
}catch(Exception e){
return "";
}
     return encoded;
}

We need to import a library for Base64 encoding:  sun.misc.BASE64Encoder

Mapping:

mapping.PNG

UDF:

/wp-content/uploads/2014/07/test_490640.png

Regards

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo NarayanaSwamy Mariyala
      NarayanaSwamy Mariyala

      Hi,

      Very useful blog!

      Any Ideas on Unzip and Decode and populate the stream into multiple fields in the target structure with UDF?

      Author's profile photo Evgeniy Kolmakov
      Evgeniy Kolmakov

      Hi Narayana!

      And what's the problem with this? Just do the same in reverse order: decode your Base64 string into byte array containing your zipped content and unzip it.

      Regards, Evgeniy.

      Author's profile photo NarayanaSwamy Mariyala
      NarayanaSwamy Mariyala

      Hi Evgeniy,
      I have achieved long back but didn't responded with my comment since i feel this is not a forum to respond.