Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: jpstoryHi All
I have a Zip Extract node that extracts the contents of a zipped file. In the node output it also spits out the file names.
The problem is that it does not spits out the files' full path, could you please help twit it a little bit to split out the full path, thanks
import braininfo
import os, zipfile
def setup(brainNodeControlObj, BrainNodeClass):
class BrainNode(BrainNodeClass):
def initialize(self):
super(BrainNode, self).initialize()
metadata = self.newMetadata()
metadata.append("FileName", "String")
self.outputs[0].metadata = metadata
self.isMultifile = False
if len(self.inputs) > 0:
self.isMultifile = True
self.FilenameField = self.properties.getString("ls.brain.node.ZipExtract.FileNameExpr")
else:
self.Filename = self.properties.getString("ls.brain.node.ZipExtract.FileName")
self.extractPath = self.properties.getString("ls.brain.node.ZipExtract.ExtractToPath")
self.deleteSourceFile = self.properties.getBool("ls.brain.node.ZipExtract.DeleteSourceFile")
# Check if Extract Directory Exists (Create)
if not os.path.exists(self.extractPath):
os.makedirs(self.extractPath)
def finalize(self, val):
super(BrainNode, self).finalize(val)
def pump(self, quant):
while quant.permitsRunning(self):
if self.isMultifile:
inRec = self.inputs[0].read()
if not inRec:
return False
# open Zip file
filename = inRec[self.FilenameField]
success = self.processZip(filename)
# Delete Zip File if necessary
if self.deleteSourceFile and success:
os.remove(filename)
return True
else:
# open the zip file
filename = self.Filename
success = self.processZip(filename)
# Delete Zip File if necessary
if self.deleteSourceFile and success:
os.remove(self.Filename)
return False
def processZip(self, filename):
success = False
try:
zip = zipfile.ZipFile(filename)
for name in zip.namelist():
if name.endswith('/') or name.endswith('\\'):
os.mkdir(os.path.join(self.extractPath,name))
else:
outfilePath = os.path.join(self.extractPath,os.path.basename(name))
outfile = open(outfilePath, 'wb')
outfile.write(zip.read(name))
outfile.close()
outRec = self.outputs[0].newRecord()
outRec["FileName"] = name
self.outputs[0].write(outRec)
success = True
zip.close()
except zipfile.error, e:
success = False
raise braininfo.BrainNodeException, 'ZIP ERROR: ' + str(e)
except Exception, e:
success = False
raise braininfo.BrainNodeException, 'ERROR: ' + str(e)
return BrainNode