def read(self, filename): zinfo = self.getinfo(filename) fp = self.get_fp() try: filepos = fp.tell() fp.seek(zinfo.file_offset, 0) bytes = fp.read(intmask(zinfo.compress_size)) fp.seek(filepos, 0) if zinfo.compress_type == ZIP_STORED: pass elif zinfo.compress_type == ZIP_DEFLATED and rminiz_oxide is not None: stream = rminiz_oxide.inflateInit(wbits=-15) try: bytes, _, _ = rminiz_oxide.decompress(stream, bytes) # need to feed in unused pad byte so that zlib won't choke ex, _, _ = rminiz_oxide.decompress(stream, 'Z') if ex: bytes = bytes + ex finally: rminiz_oxide.inflateEnd(stream) elif zinfo.compress_type == ZIP_DEFLATED: raise BadZipfile("Cannot decompress file, zlib not installed") else: raise BadZipfile("Unsupported compression method %d for " "file %s" % (zinfo.compress_type, filename)) crc = crc32(bytes) if crc != zinfo.CRC: raise BadZipfile("Bad CRC-32 for file %s" % filename) return bytes finally: fp.close()
def test_cornercases(): """ Test degenerate arguments. """ stream = rminiz_oxide.deflateInit() bytes = rminiz_oxide.compress(stream, "") bytes += rminiz_oxide.compress(stream, "") bytes += rminiz_oxide.compress(stream, "", rminiz_oxide.Z_FINISH) assert zlib.decompress(bytes) == "" rminiz_oxide.deflateEnd(stream) stream = rminiz_oxide.inflateInit() data, finished, unused = rminiz_oxide.decompress(stream, "") assert data == "" assert finished is False assert unused == 0 buf = compressed for i in range(10): data, finished, unused = rminiz_oxide.decompress(stream, buf, max_length=0) assert data == "" assert finished is False assert unused > 0 buf = buf[-unused:] rminiz_oxide.inflateEnd(stream)
def test_decompression_truncated_input(): """ Test that we can accept incomplete input when inflating, but also detect this situation when using Z_FINISH. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(compressed), '=>', len(expanded) stream = rminiz_oxide.inflateInit() data, finished1, unused1 = rminiz_oxide.decompress(stream, compressed[:1000]) assert expanded.startswith(data) assert finished1 is False assert unused1 == 0 data2, finished2, unused2 = rminiz_oxide.decompress( stream, compressed[1000:2000]) data += data2 assert finished2 is False assert unused2 == 0 assert expanded.startswith(data) exc = py.test.raises(rminiz_oxide.RZlibError, rminiz_oxide.decompress, stream, compressed[2000:-500], rminiz_oxide.Z_FINISH) msg = "Error -5 while decompressing data: incomplete or truncated stream" assert str(exc.value) == msg rminiz_oxide.inflateEnd(stream)
def test_decompression_lots_of_data(): """ Test compression of more data that fits in a single internal output buffer. """ expanded = repr(range(20000)) compressed = zlib.compress(expanded) print len(compressed), '=>', len(expanded) stream = rminiz_oxide.inflateInit() bytes, finished, unused = rminiz_oxide.decompress(stream, compressed, rminiz_oxide.Z_FINISH) rminiz_oxide.inflateEnd(stream) assert bytes == expanded assert finished is True assert unused == 0
def test_decompression(): """ Once we have got a inflate stream, rminiz_oxide.decompress() should allow us to decompress bytes. """ stream = rminiz_oxide.inflateInit() bytes1, finished1, unused1 = rminiz_oxide.decompress(stream, compressed) bytes2, finished2, unused2 = rminiz_oxide.decompress( stream, "", rminiz_oxide.Z_FINISH) rminiz_oxide.inflateEnd(stream) assert bytes1 + bytes2 == expanded assert finished1 is True assert finished2 is True assert unused1 == 0 assert unused2 == 0
def test_decompress_max_length(): """ Test the max_length argument of decompress(). """ stream = rminiz_oxide.inflateInit() data1, finished1, unused1 = rminiz_oxide.decompress(stream, compressed, max_length=17) assert data1 == expanded[:17] assert finished1 is False assert unused1 > 0 data2, finished2, unused2 = rminiz_oxide.decompress( stream, compressed[-unused1:]) assert data2 == expanded[17:] assert finished2 is True assert unused2 == 0 rminiz_oxide.inflateEnd(stream)
def decompress(space, string, wbits=rminiz_oxide.MAX_WBITS, bufsize=0): """ decompress(string[, wbits[, bufsize]]) -- Return decompressed string. Optional arg wbits is the window buffer size. Optional arg bufsize is only for compatibility with CPython and is ignored. """ try: try: stream = rminiz_oxide.inflateInit(wbits) except ValueError: raise zlib_error(space, "Bad window buffer size") try: result, _, _ = rminiz_oxide.decompress(stream, string, rminiz_oxide.Z_FINISH) finally: rminiz_oxide.inflateEnd(stream) except rminiz_oxide.RZlibError as e: raise zlib_error(space, e.msg) return space.newbytes(result)
def __init__(self, space, wbits=rminiz_oxide.MAX_WBITS): """ Initialize a new decompression object. wbits is an integer between 8 and MAX_WBITS or -8 and -MAX_WBITS (inclusive) giving the number of "window bits" to use for compression and decompression. See the documentation for deflateInit2 and inflateInit2. """ ZLibObject.__init__(self, space) self.unused_data = '' self.unconsumed_tail = '' try: self.stream = rminiz_oxide.inflateInit(wbits) except rminiz_oxide.RZlibError as e: raise zlib_error(space, e.msg) except ValueError: raise oefmt(space.w_ValueError, "Invalid initialization option") self.register_finalizer(space)
def test_decompression_too_much_input(): """ Check the case where we feed extra data to decompress(). """ stream = rminiz_oxide.inflateInit() data1, finished1, unused1 = rminiz_oxide.decompress( stream, compressed[:-5]) assert finished1 is False assert unused1 == 0 data2, finished2, unused2 = rminiz_oxide.decompress( stream, compressed[-5:] + 'garbage') assert finished2 is True assert unused2 == len('garbage') assert data1 + data2 == expanded data3, finished3, unused3 = rminiz_oxide.decompress(stream, 'more_garbage') assert finished3 is True assert unused3 == len('more_garbage') assert data3 == '' rminiz_oxide.inflateEnd(stream)
def test_mismatch(): """ Test the mismatch init and end. """ stream = rminiz_oxide.inflateInit() rminiz_oxide.deflateEnd(stream)
def test_inflate_init_end(): """ inflateInit() followed by inflateEnd() should work and do nothing. """ stream = rminiz_oxide.inflateInit() rminiz_oxide.inflateEnd(stream)