use python zipfile

This commit is contained in:
dockes 2009-10-24 06:17:34 +00:00
parent 2cd0171ce6
commit 96855e3aea
2 changed files with 22 additions and 28 deletions

View File

@ -74,8 +74,10 @@ class RclExecM:
# If we're given a file name, open it. # If we're given a file name, open it.
if len(params["filename:"]) != 0: if len(params["filename:"]) != 0:
processor.openfile(params) if not processor.openfile(params):
self.answer("", "", True)
return
# If we have an ipath, that's what we look for, else ask for next entry # If we have an ipath, that's what we look for, else ask for next entry
ipath = "" ipath = ""
if params.has_key("ipath:") and len(params["ipath:"]): if params.has_key("ipath:") and len(params["ipath:"]):

View File

@ -4,54 +4,46 @@
import os import os
import rclexecm import rclexecm
from zipfile import ZipFile, error
class ZipExtractor: class ZipExtractor:
def __init__(self, em): def __init__(self, em):
self.currentindex = 0 self.currentindex = 0
self.contents = []
self.zipfile = ""
self.em = em self.em = em
# Execute unzip to retrieve TOC list. Store in self.contents
def listzip(self):
cmd = "unzip -Z -1 '%s'"%(self.zipfile)
f = os.popen(cmd, "r")
self.contents = f.readlines()
status = f.close()
if status:
return False
return True
# Execute unzip to extract and return data for single entry in zip file
def extractzipentry(self, name): def extractzipentry(self, name):
cmd = "unzip -p '%s' '%s'"%(self.zipfile, name) return (ret, data)
f = os.popen(cmd, "r")
data = f.read()
# self.em.rclog("data: %s" % data)
status = f.close()
if status:
return (False, "")
return (True, data)
def extractone(self, ipath): def extractone(self, ipath):
(ok, docdata) = self.extractzipentry(ipath) #self.em.rclog("extractone: [%s]" % ipath)
eof = (self.currentindex >= len(self.contents) -1) docdata = ""
try:
docdata = self.zip.read(ipath)
ok = True
except error, err:
self.em.rclog("extractone: failed: [%s]" % err)
ok = False
eof = (self.currentindex >= len(self.zip.namelist()) -1)
return (ok, docdata, ipath, eof) return (ok, docdata, ipath, eof)
###### File type handler api, used by rclexecm ----------> ###### File type handler api, used by rclexecm ---------->
def openfile(self, params): def openfile(self, params):
self.zipfile = params["filename:"]
self.currentindex = 0 self.currentindex = 0
return self.listzip() try:
self.zip = ZipFile(params["filename:"])
return True
except:
return False
def getipath(self, params): def getipath(self, params):
return self.extractone(params["ipath:"]) return self.extractone(params["ipath:"])
def getnext(self, params): def getnext(self, params):
if self.currentindex >= len(self.contents): if self.currentindex >= len(self.zip.namelist()):
#self.em.rclog("getnext: EOF hit") #self.em.rclog("getnext: EOF hit")
return (False, "", "", 1) return (False, "", "", 1)
else: else:
ret= self.extractone(self.contents[self.currentindex].rstrip("\n")) ret= self.extractone(self.zip.namelist()[self.currentindex])
self.currentindex += 1 self.currentindex += 1
return ret return ret