Skip to main content
Question

How to decode a .zip file sent over mqtt connector as an attribute?

  • August 16, 2023
  • 1 reply
  • 46 views

stlj
Contributor
Forum|alt.badge.img+5
  • Contributor

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

1 reply

geomancer
Evangelist
Forum|alt.badge.img+48
  • Evangelist
  • August 23, 2023

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 = names[0]
            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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings