def test_compression(): """ Once we have got a deflate stream, rzlib.compress() should allow us to compress bytes. """ stream = rzlib.deflateInit() bytes = rzlib.compress(stream, expanded) bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) rzlib.deflateEnd(stream) assert bytes == compressed
def flush(self, mode=rzlib.Z_FINISH): """ flush( [mode] ) -- Return a string containing any remaining compressed data. mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the default value used when mode is not specified is Z_FINISH. If mode == Z_FINISH, the compressor object can no longer be used after calling the flush() method. Otherwise, more data can still be compressed. """ try: self.lock() try: if not self.stream: raise zlib_error(self.space, "compressor object already flushed") result = rzlib.compress(self.stream, '', mode) if mode == rzlib.Z_FINISH: # release the data structures now rzlib.deflateEnd(self.stream) self.stream = rzlib.null_stream finally: self.unlock() except rzlib.RZlibError, e: raise zlib_error(self.space, e.msg)
def test_compression_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(expanded), '=>', len(compressed) stream = rzlib.deflateInit() bytes = rzlib.compress(stream, expanded, rzlib.Z_FINISH) rzlib.deflateEnd(stream) assert bytes == compressed
def test_cornercases(): """ Test degenerate arguments. """ stream = rzlib.deflateInit() bytes = rzlib.compress(stream, "") bytes += rzlib.compress(stream, "") bytes += rzlib.compress(stream, "", rzlib.Z_FINISH) assert zlib.decompress(bytes) == "" stream = rzlib.inflateInit() data, finished, unused = rzlib.decompress(stream, "") assert data == "" assert finished is False assert unused == 0 buf = compressed for i in range(10): data, finished, unused = rzlib.decompress(stream, buf, max_length=0) assert data == "" assert finished is False assert unused > 0 buf = buf[-unused:]
def compress(space, string, level=rzlib.Z_DEFAULT_COMPRESSION): """ compress(string[, level]) -- Returned compressed string. Optional arg level is the compression level, in 1-9. """ try: try: stream = rzlib.deflateInit(level) except ValueError: raise zlib_error(space, "Bad compression level") try: result = rzlib.compress(stream, string, rzlib.Z_FINISH) finally: rzlib.deflateEnd(stream) except rzlib.RZlibError, e: raise zlib_error(space, e.msg)
def compress(self, data): """ compress(data) -- Return a string containing data compressed. After calling this function, some of the input data may still be stored in internal buffers for later processing. Call the flush() method to clear these buffers. """ try: self.lock() try: if not self.stream: raise zlib_error(self.space, "compressor object already flushed") result = rzlib.compress(self.stream, data) finally: self.unlock() except rzlib.RZlibError, e: raise zlib_error(self.space, e.msg)