Skip to main content
Solved

Triple zipped XML

  • February 22, 2015
  • 2 replies
  • 43 views

fmelizard
Safer
Forum|alt.badge.img+21
Hi ppl,

 

 

My input are XML files that are triple zipped (zip in a zip in a zip) I know I can read from a zip file, but is there anyway to read from a triple zipped input?

 

If not does anybody have a tip on how to handle such a situation? ( any python/ tcl savy's advice?)

 

Cheers,

 

Itay

Best answer by takashi

Hi Itay, current FME seems not to have the capability to unzip recursively.

 

This example is from my old PythonCaller implementation. FYI.

 

-----

 

import fmeobjects

 

import os, re, zipfile

 

 

class RecursiveUnzipper(object):

 

    def input(self, feature):

 

        def clearDirectory(path):

 

            if os.path.isdir(path):

 

                for t in ['%s/%s' % (path, s) for s in os.listdir(path)]:

 

                    clearDirectory(t)

 

                os.rmdir(path)

 

            else:

 

                os.remove(path)

 

                

 

        # Get zip file path and destinction directory path.

 

        zipPath = feature.getAttribute('_zip')

 

        outDir = feature.getAttribute('_out_dir')

 

        if os.path.exists(zipPath):

 

            # If specifid output directory exists, remove it beforehand.

 

            if os.path.exists(outDir):

 

                clearDirectory(outDir)

 

            self.index = 0

 

            self.unzip(feature, zipPath, outDir)

 

        self.pyoutput(feature)

 

        

 

    # Unzip recursively

 

    # Extracted file paths will be stored in a list called "_unzipped{}".

 

    def unzip(self, feature, path, outDir = None):

 

        if os.path.isfile(path):

 

            m = re.match(r'^(.+)\\.zip$', path, re.IGNORECASE)

 

            if m != None:

 

                zip = zipfile.ZipFile(path)

 

                if zip.testzip() == None:

 

                    dir = outDir if outDir else m.group(1)

 

                    zip.extractall(dir)

 

                    for t in ['%s/%s' % (dir, s) for s in os.listdir(dir)]:

 

                        self.unzip(feature, t)

 

            else:

 

                feature.setAttribute('_unzipped{%d}' % self.index, path)

 

                self.index += 1

 

        elif os.path.isdir(path):

 

            for t in ['%s/%s' % (path, s) for s in os.listdir(path)]:

 

                self.unzip(feature, t)

 

-----

 

 

Takashi
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

takashi
Celebrity
  • Best Answer
  • February 23, 2015
Hi Itay, current FME seems not to have the capability to unzip recursively.

 

This example is from my old PythonCaller implementation. FYI.

 

-----

 

import fmeobjects

 

import os, re, zipfile

 

 

class RecursiveUnzipper(object):

 

    def input(self, feature):

 

        def clearDirectory(path):

 

            if os.path.isdir(path):

 

                for t in ['%s/%s' % (path, s) for s in os.listdir(path)]:

 

                    clearDirectory(t)

 

                os.rmdir(path)

 

            else:

 

                os.remove(path)

 

                

 

        # Get zip file path and destinction directory path.

 

        zipPath = feature.getAttribute('_zip')

 

        outDir = feature.getAttribute('_out_dir')

 

        if os.path.exists(zipPath):

 

            # If specifid output directory exists, remove it beforehand.

 

            if os.path.exists(outDir):

 

                clearDirectory(outDir)

 

            self.index = 0

 

            self.unzip(feature, zipPath, outDir)

 

        self.pyoutput(feature)

 

        

 

    # Unzip recursively

 

    # Extracted file paths will be stored in a list called "_unzipped{}".

 

    def unzip(self, feature, path, outDir = None):

 

        if os.path.isfile(path):

 

            m = re.match(r'^(.+)\\.zip$', path, re.IGNORECASE)

 

            if m != None:

 

                zip = zipfile.ZipFile(path)

 

                if zip.testzip() == None:

 

                    dir = outDir if outDir else m.group(1)

 

                    zip.extractall(dir)

 

                    for t in ['%s/%s' % (dir, s) for s in os.listdir(dir)]:

 

                        self.unzip(feature, t)

 

            else:

 

                feature.setAttribute('_unzipped{%d}' % self.index, path)

 

                self.index += 1

 

        elif os.path.isdir(path):

 

            for t in ['%s/%s' % (path, s) for s in os.listdir(path)]:

 

                self.unzip(feature, t)

 

-----

 

 

Takashi

fmelizard
Safer
Forum|alt.badge.img+21
  • Author
  • Safer
  • February 23, 2015
Hi Takashi,

 

 

Fantastic! thanks for your help.

 

Itay