From 0a4949deaa56c199162acbab5e8dbf2dc28b490c Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 2 Mar 2008 00:17:46 -0800 Subject: [PATCH] add rarfile support... should probably make this more modular so that if rarfile isn't installed, it isn't an issue.. [git-p4: depot-paths = "//depot/": change = 1133] --- ZipStorage.py | 15 ++++++++++----- iterrarfile.py | 33 ++++----------------------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/ZipStorage.py b/ZipStorage.py index 36f8d61..55cf4a0 100644 --- a/ZipStorage.py +++ b/ZipStorage.py @@ -12,6 +12,8 @@ import iterzipfile zipfile = iterzipfile import itertarfile tarfile = itertarfile +import iterrarfile +rarfile = iterrarfile import FileDIDL from DIDLLite import StorageFolder, Item, VideoItem, AudioItem, TextItem, ImageItem, Resource @@ -227,16 +229,19 @@ def canHandle(path): if zipfile.is_zipfile(path): return True + print 'rar:', rarfile.is_rarfile(path) + if rarfile.is_rarfile(path): + return True + #tar is cheaper on __init__ than zipfile return tryTar(path) def genZipFile(path): - try: + if zipfile.is_zipfile(path): return zipfile.ZipFile(path) - except: - #import traceback - #traceback.print_exc(file=log.logfile) - pass + + if rarfile.is_rarfile(path): + return rarfile.RarFile(path) try: return tryTar(path) diff --git a/iterrarfile.py b/iterrarfile.py index 5248ca9..f06189f 100644 --- a/iterrarfile.py +++ b/iterrarfile.py @@ -4,34 +4,9 @@ __version__ = '$Change$' # $Id$ -import tarfile -from tarfile import * - -TAR_PLAIN = tarfile.TAR_PLAIN -TAR_GZIPPED = tarfile.TAR_GZIPPED -TAR_BZ2 = 'bz2' - -__all__ = tarfile.__all__ - -class TarFileCompat(tarfile.TarFileCompat): - def __init__(self, file, mode="r", compression=TAR_PLAIN): - if compression != TAR_BZ2: - tarfile.TarFileCompat.__init__(self, file, mode, compression) - return - - self.tarfile = TarFile.bz2open(file, mode) - if mode[0:1] == "r": - members = self.tarfile.getmembers() - for i in xrange(len(members)): - m = members[i] - m.filename = m.name - m.file_size = m.size - m.date_time = time.gmtime(m.mtime)[:6] +import rarfile +from rarfile import * +class RarFile(rarfile.RarFile): def readiter(self, name, blksize=16384): - f = self.tarfile.extractfile(self.tarfile.getmember(name)) - while True: - data = f.read(blksize) - if data == '': - break - yield data + yield self.read(name)