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:
UDF:
Regards
Hi,
Very useful blog!
Any Ideas on Unzip and Decode and populate the stream into multiple fields in the target structure with UDF?
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.
Hi Evgeniy,
I have achieved long back but didn't responded with my comment since i feel this is not a forum to respond.