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
Hi Takashi,
Fantastic! thanks for your help.
Itay