def command_unzip(FilesClass, filepath, password=None, flush_every=1000): """Add files from the zip file at filepath""" if not zipfile.is_zipfile(filepath): raise TypeError("Not a zipfile %s" % filepath) fs = FilesClass() zf = zipfile.ZipFile(filepath) if password is not None: zf.setpassword(password) datalen = 0 for i, name in enumerate(zf.namelist()): data = zf.read(name, pwd=password) datalen += len(data) hexdigest = fs.bulk_put(data) print("%s: %s" % (hexdigest, name)) if i % flush_every == 0: print("flush %s bytes of data..." % datalen) txdatalen = fs.bulk_flush() print("sent %s bytes of compressed data" % txdatalen) print("flush ...") fs.bulk_flush()
def archive_mime_type(path): """Return the archive type of <path> or None for non-archives.""" try: if os.path.isfile(path): if not os.access(path, os.R_OK): return None if czipfile.is_zipfile(path): return ZIP fd = open(path, 'rb') magic = fd.read(4) fd.close() if tarfile.is_tarfile(path) and os.path.getsize(path) > 0: if magic.startswith('BZh'): return BZIP2 if magic.startswith('\037\213'): return GZIP return TAR if magic == 'Rar!': return RAR except Exception: print '! Error while reading', path return None