我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用zipfile.read()。
def decompress_bz2(filepath): download_photo(filepath) try: # Open the BZ2 file zipfile = bz2.BZ2File(filepath) # Get the decompressed data data = zipfile.read() # Assuming the filepath ends with .bz2 newfilepath = filepath[:-4] # Write an uncompressed file open(newfilepath, 'wb').write(data) return newfilepath except IOError as e: print "[!] Please verifiy you entered the correct cookie value.\n" print e
def parsed_wheel_info(self): """Parse wheel metadata (the .data/WHEEL file)""" return read_pkg_info_bytes(self.zipfile.read(self.wheelinfo_name))
def searchUnpackKnownAr(filename, tempdir=None, scanenv={}, debug=False): ## first check if the file actually could be a valid ar file arfile = open(filename, 'rb') arfile.seek(0) arheader = arfile.read(7) arfile.close() if arheader != fsmagic.fsmagic['ar']: return ([], [], [], {}) ## then try unpacking it. res = searchUnpackAr(filename, tempdir, [], {'ar': [0]}, scanenv, debug) (diroffsets, blacklist, newtags, hints) = res failed = False ## there were results, so check if they were successful if diroffsets != []: if len(diroffsets) != 1: failed = True else: (dirpath, startoffset, endoffset) = diroffsets[0] if startoffset != 0 or endoffset != os.stat(filename).st_size: failed = True if failed: for i in diroffsets: (dirpath, startoffset, endoffset) = i try: shutil.rmtree(dirpath) except: pass return ([], [], [], {}) else: return (diroffsets, blacklist, newtags, hints) return ([], [], [], {})
def gzipcrc32(filename): datafile = open(filename, 'rb') datafile.seek(0) databuffer = datafile.read(10000000) crc32 = binascii.crc32('') while databuffer != '': crc32 = binascii.crc32(databuffer, crc32) databuffer = datafile.read(10000000) datafile.close() crc32 = crc32 & 0xffffffff return crc32
def searchUnpackKnownGzip(filename, tempdir=None, scanenv={}, debug=False): ## first check if the file actually could be a valid gzip file gzipfile = open(filename, 'rb') gzipfile.seek(0) gzipheader = gzipfile.read(3) gzipfile.close() if gzipheader != fsmagic.fsmagic['gzip']: return ([], [], [], {}) ## then try unpacking it. res = searchUnpackGzip(filename, tempdir, [], {'gzip': [0]}, scanenv, debug) (diroffsets, blacklist, newtags, hints) = res failed = False ## there were results, so check if they were successful if diroffsets != []: if len(diroffsets) != 1: failed = True else: (dirpath, startoffset, endoffset) = diroffsets[0] if startoffset != 0 or endoffset != os.stat(filename).st_size: failed = True if failed: for i in diroffsets: (dirpath, startoffset, endoffset) = i try: shutil.rmtree(dirpath) except: pass return ([], [], [], {}) else: return (diroffsets, blacklist, newtags, hints) return ([], [], [], {})
def searchUnpackRZIP(filename, tempdir=None, blacklist=[], offsets={}, scanenv={}, debug=False): hints = {} if not 'rzip' in offsets: return ([], blacklist, [], hints) if offsets['rzip'] == []: return ([], blacklist, [], hints) if offsets['rzip'][0] != 0: return ([], blacklist, [], hints) if os.stat(filename).st_size < 10: return ([], blacklist, [], hints) diroffsets = [] tags = [] offset = 0 rzipfile = open(filename, 'rb') rzipfile.seek(0) rzipdata = rzipfile.read(10) rzipfile.close() rzipsize = struct.unpack('>L', rzipdata[6:10])[0] blacklistoffset = extractor.inblacklist(offset, blacklist) if blacklistoffset != None: return (diroffsets, blacklist, tags, hints) tmpdir = dirsetup(tempdir, filename, "rzip", 1) res = unpackRZIP(filename, offset, rzipsize, tmpdir) if res != None: rzipdir = res diroffsets.append((rzipdir, offset, 0)) #blacklist.append((offset, offset + unpackrzipsize)) #if offset == 0: # tags.append("compressed") # tags.append("rzip") else: ## cleanup os.rmdir(tmpdir) return (diroffsets, blacklist, tags, hints)
def searchUnpackAndroidSparse(filename, tempdir=None, blacklist=[], offsets={}, scanenv={}, debug=False): hints = {} if not 'android-sparse' in offsets: return ([], blacklist, [], hints) if offsets['android-sparse'] == []: return ([], blacklist, [], hints) diroffsets = [] counter = 1 tags = [] for offset in offsets['android-sparse']: blacklistoffset = extractor.inblacklist(offset, blacklist) if blacklistoffset != None: continue ## first see if the major version is correct sparsefile = open(filename, 'rb') sparsefile.seek(offset+4) sparsedata = sparsefile.read(2) sparsefile.close() if len(sparsedata) != 2: break majorversion = struct.unpack('<H', sparsedata)[0] if not majorversion == 1: continue tmpdir = dirsetup(tempdir, filename, "android-sparse", counter) res = unpackAndroidSparse(filename, offset, tmpdir) if res != None: (sparsesize, sparsedir) = res diroffsets.append((sparsedir, offset, sparsesize)) blacklist.append((offset, offset + sparsesize)) counter = counter + 1 else: ## cleanup os.rmdir(tmpdir) return (diroffsets, blacklist, tags, hints)
def searchUnpackUbi(filename, tempdir=None, blacklist=[], offsets={}, scanenv={}, debug=False): hints = {} if not 'ubi' in offsets: return ([], blacklist, [], hints) if offsets['ubi'] == []: return ([], blacklist, [], hints) datafile = open(filename, 'rb') ## We can use the values of offset and ubisize where offset != -1 ## to determine the ranges for the blacklist. diroffsets = [] counter = 1 ## TODO: big file fixes data = datafile.read() datafile.close() for offset in offsets['ubi']: blacklistoffset = extractor.inblacklist(offset, blacklist) if blacklistoffset != None: continue tmpdir = dirsetup(tempdir, filename, "ubi", counter) res = unpackUbi(data, offset, tmpdir) if res != None: (ubitmpdir, ubisize) = res diroffsets.append((ubitmpdir, offset, ubisize)) blacklist.append((offset, offset+ubisize)) ## TODO use ubisize to set the blacklist correctly counter = counter + 1 else: ## cleanup os.rmdir(tmpdir) return (diroffsets, blacklist, [], hints)