Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: awilliamsWe've encountered a slight issue with this node which reduces the usability in some circumstances.
When we've provided the node a list of files to archive which include the full path it attempts to store the full path in the zip file, and depending on how the file path string is constructed it can produce a folder in the Zip file named with a space, as in " ".
Zip applications such as WinZip and 7Zip handle these as expected, but Windows's inbuilt zip file handling shows the zip file as having no contents.
We modified our zip node to avoid storing the path, this resolves the issue but it would be good to have this as a selectable parameter
Here is the updated node:
node:Zip_Archive
bretype:core::Python
editor:Label=Zip Archive
editor:sortkey=49f571285c1f6a71_3
editor:icon=clear_status.ico
editor:apistop=1
output:49f572ff5c2d3792/Archived Files=
prop:Append=false
prop:DeleteSourceFile=false
prop:Python2Implementation=<<EOX
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")
metadata.append("Archive 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.ZipArchive.FileNameExpr")
else:
self.Filename = self.properties.getString("ls.brain.node.ZipArchive.FileName")
self.archiveFilename = self.properties.getString("ls.brain.node.ZipArchive.ArchiveFilename")
self.append = self.properties.getBool("ls.brain.node.ZipArchive.append")
self.deleteSourceFile = self.properties.getBool("ls.brain.node.ZipArchive.DeleteSourceFile")
# Check if Archive Directory Exists (Create)
d = os.path.dirname(self.archiveFilename)
if not os.path.exists(d):
os.makedirs(d)
if self.append:
self.arcFile = zipfile.ZipFile(self.archiveFilename, 'a', zipfile.ZIP_DEFLATED)
else:
self.arcFile = zipfile.ZipFile(self.archiveFilename, 'w', zipfile.ZIP_DEFLATED)
def finalize(self, val):
super(BrainNode, self).finalize(val)
self.arcFile.close()
def pump(self, quant):
while quant.permitsRunning(self):
if self.isMultifile:
inRec = self.inputs[0].read()
if not inRec:
return False
#Zip File
filename = inRec[self.FilenameField]
if self.properties.getBool("ls.brain.node.ZipArchive.StorePath"):
self.arcfile.write(filename)
else:
self.arcFile.write(filename, os.path.basename(filename))
# Delete Existing File if necessary
if self.deleteSourceFile:
os.remove(filename)
# Build Output
outRec = self.outputs[0].newRecord()
outRec["FileName"] = filename
outRec["Archive FileName"] = self.archiveFilename
self.outputs[0].write(outRec)
return True
else:
#Zip File
self.arcFile.write(self.Filename)
# Delete Existing File if necessary
if self.deleteSourceFile:
os.remove(self.Filename)
# Build Output
outRec = self.outputs[0].newRecord()
outRec["FileName"] = self.Filename
outRec["Archive FileName"] = self.archiveFilename
self.outputs[0].write(outRec)
return False
return BrainNode
EOX
editor:XY=140,440
editor:UsageDoc=This node will archive files in a .zip compressed archive file.
editor:propdef=FileName|filename|1|ls.brain.node.ZipArchive.FileName|None
editor:propdef=FileNameExpr|string|1|ls.brain.node.ZipArchive.FileNameExpr|None
editor:propdef=ArchiveFilename|string|1|ls.brain.node.ZipArchive.ArchiveFilename|None
editor:propdef=Append|boolean|1|ls.brain.node.ZipArchive.append|None
editor:propdef=DeleteSourceFile|boolean|1|ls.brain.node.ZipArchive.DeleteSourceFile|None
editor:propdef=StorePath|boolean|1|ls.brain.node.ZipArchive.StorePath|None
editor:propoverride=Python2Implementation|0
editor:propdoc=FileName=To archive a single file, enter the full path and file name here
editor:propdoc=FileNameExpr=To archive multiple files, enter the name of the input field containing the file paths here. Should be used in conjunction with a DirList node or similar.
editor:propdoc=ArchiveFilename=Full path to resulting archive (Zip) file
editor:propdoc=Append=If true, then if the archive file already exists, file(s) will be added. Otherwise, if archive exists, it will be deleted and overwriten.
editor:propdoc=DeleteSourceFile=If true, the original source file will be deleted after archiving.
editor:propdoc=StorePath=If true, the full file path will be stored in the Zip file.
end:Zip_Archive