LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Zip Extract

    Employee
    Posted 05-01-2015 11:52

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jpstory

    Hi 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


  • 2.  RE: Zip Extract

    Employee
    Posted 05-04-2015 06:41

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: rpigneri

    Hello, JP Story,

    Try the following code on for size. The change is on line 71.

    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"] = outfilePath
                            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
    I wasn't able to test this as I haven't the rest of the node, but this should help you towards your goal.

    Hope that helps,

    Rocco


  • 3.  RE: Zip Extract

    Employee
    Posted 05-04-2015 11:32

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: jpstory

    Thanks, it worked perfectly!