def GetFilelist(self): """ Returns the list of files from the RaR archive """ tracer.debug("WinRaRArchive - GetFilelist") f = RarFile(self.filename) for fi in f.iterfiles(): self.namelist.append(fi.filename) del f
def Unpack(self, target=None): """Extract the files in the RaR Archive to the path specified in the mapping dictionary""" tracer.debug("WinRaRArchive - Unpack") global textfiles totalfiles = float(len(self.namelist)) # float to force non-integer division countfiles = 0 rfile = RarFile(self.filename) for rf in rfile.iterfiles(): # report progress countfiles += 1 if totalfiles > 1: WurmCommon.outProgressPercent(_("Unpacking RAR file"), countfiles/totalfiles) n = rf.filename if not self.IsFileRequired(n, self.filemap): continue if n[-1:] == '/': # is a directory try: os.makedirs(os.path.join(target, n)) except: pass # path already exists else: ext = os.path.splitext(n)[1].replace('.', '') # leaves only the extension, no leading dot mode = "wb" # default to binary rmode = "rb" # UnRAR needs to have read mode specified, too if ext.lower() in textfiles and not int(WurmCommon.getGlobalSetting(":PreserveNewline")): mode = "wt" rmode = "rt" filename = os.path.join(target, n) dirpath = os.path.dirname(filename) # Create additional directories - this is due to ambiguities in the file structure if dirpath and not os.path.exists(dirpath): os.makedirs(dirpath) if rf.size > 0: # must check size, since directories may appear as files in the RAR try: f = open(os.path.join(target, n), mode) f.write(rf.open(rmode).read()) f.close() except Exception, details: WurmCommon.outWarning(_("Could not extract file %(filename)s") % {'filename': n}) # Accept that some files can't be extracted, in case of read-only custom files WurmCommon.outDebug("^^^ - %s %s" % (str(Exception), str(details))) WurmCommon.outDebug("Successfully Extracted %s to %s" % (n, dirpath))
def ExtractFiles(self, filelist=None, target=None): """ Extracts the specified files to target dir (if target is None, it uses the unpack dir) without preserving directory structure Returns a map 'filename given in filelist' -> 'filename on disk' """ tracer.debug("WinRaRArchive - ExtractFiles") if not filelist: filelist = self.namelist if not target: target = WurmCommon.directories["unpack"] fmap = {} f = RarFile(self.filename) for fi in f.iterfiles(): if fl: # Ignore missing entries fl = fi.filename if fl not in filelist: continue (dirname, fname) = os.path.split(fl) ext = os.path.splitext(fl)[1].replace('.', '') # leaves only the extension, no leading dot mode = "wb" #default to binary rmode = "rb" if ext.lower() in textfiles and not int(WurmCommon.getGlobalSetting(":PreserveNewline")): mode = "wt" rmode = "rt" tfname = os.path.join(target, fname) tf = open(tfname, mode) tf.write(f.open(rmode).read()) tf.close() fmap[fl] = tfname WurmCommon.outDebug("Extracted %s from %s" % (fname, os.path.split(self.filename)[1])) del f return fmap