Skip to main content

Hi. I'm trying to decode a .zip file sent over mqtt connector. I recieve the message payload attribute with the zipped string and want to decode that string to plain text. I'ts not the mqtt message that is the problem but how to decode this zipped attribute into plain text on the fly inside a workspace?

Thanks, Stefan

This is only a partial solution: it only works when the zipped data in the payload attribute contains exactly one zipped file, that is placed in the root of the zipped data. However it is not necessary to know the name of this file.

 

All that is needed is a PythonCaller.

The attribute with the zipped data is 'file_content', the results are written to attribute 'contents'.

import fme
import fmeobjects
import io
import zipfile
# Template Function interface:
# When using this function, make sure its name is set as the value of
# the 'Class or Function to Process Features' transformer parameter
def processFeature(feature):
    pass
 
class FeatureProcessor(object):
    def __init__(self):
        pass
        
    def input(self, feature):
        bin = feature.getAttribute('file_content')
        buff = io.BytesIO(bin)
        with zipfile.ZipFile(buff, 'r') as z:
            z.open
            names = z.namelist()
            first_name = namesb0]
            contents = z.read(first_name).decode('utf-8')
            feature.setAttribute('contents', contents)
            z.close
            pass
 
        self.pyoutput(feature)
        
    def close(self):
        pass

Zipped and unzipped data from PythonCaller


Reply