From 5149869959c48ecbc7a0c7c9033b9718dc355aa9 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sat, 1 Mar 2008 01:51:00 -0800 Subject: [PATCH] make it so we don't have to fully open a zip file to know it is one.. This speeds up detection of large zip files.. [git-p4: depot-paths = "//depot/": change = 1129] --- ZipStorage.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ZipStorage.py b/ZipStorage.py index 8e5b981..36f8d61 100644 --- a/ZipStorage.py +++ b/ZipStorage.py @@ -213,6 +213,23 @@ class ZipChildDir(ZipItem, StorageFolder): def __repr__(self): return '' % len(self.pathObjmap) +def tryTar(path): + # Try to see if it's a tar file + if path[-2:] == 'gz': + comp = tarfile.TAR_GZIPPED + elif path[-3:] == 'bz2': + comp = tarfile.TAR_BZ2 + else: + comp = tarfile.TAR_PLAIN + return tarfile.TarFileCompat(path, compression=comp) + +def canHandle(path): + if zipfile.is_zipfile(path): + return True + + #tar is cheaper on __init__ than zipfile + return tryTar(path) + def genZipFile(path): try: return zipfile.ZipFile(path) @@ -222,14 +239,7 @@ def genZipFile(path): pass try: - # Try to see if it's a tar file - if path[-2:] == 'gz': - comp = tarfile.TAR_GZIPPED - elif path[-3:] == 'bz2': - comp = tarfile.TAR_BZ2 - else: - comp = tarfile.TAR_PLAIN - return tarfile.TarFileCompat(path, compression=comp) + return tryTar(path) except: #import traceback #traceback.print_exc(file=log.logfile) @@ -252,6 +262,7 @@ class ZipObject(FSObject, StorageFolder): self.zip = genZipFile(self.FSpath) hier = buildNameHier(self.zip.namelist(), self.zip.infolist()) + print 'zip len:', len(hier) doupdate = False children = sets.Set(hier.keys()) for i in self.pathObjmap.keys(): @@ -288,7 +299,7 @@ class ZipObject(FSObject, StorageFolder): def detectzipfile(path, fobj): try: - genZipFile(path) + canHandle(path) except: return None, None