def test_compressor(self): with self.assertRaises(TypeError): zopfli.ZopfliCompressor(None) c = zopfli.ZopfliCompressor() with self.assertRaises(TypeError): c.compress(None) c = zopfli.ZopfliCompressor() self.assertEqual(c.flush(), b'\x03\x00') with self.assertRaises(ValueError): c.compress(b'') with self.assertRaises(ValueError): c.flush()
def zopfli_encode(data, level=None, out=None): """Compress Zopfli.""" c = zopfli.ZopfliCompressor(zopfli.ZOPFLI_FORMAT_ZLIB) return c.compress(data) + c.flush()
def _compress(self, original_file): c = zopfli.ZopfliCompressor(zopfli.ZOPFLI_FORMAT_GZIP) z = c.compress(original_file.read()) + c.flush() return File(BytesIO(z))
def compressZopfli( self, data: typing.Union[bytes, bytearray]) -> ProcessorContextStub: cO = zopfli.ZopfliCompressor(**self.zopfliCompressorParams) return ProcessorContextStub(cO.compress(data) + cO.flush())
def test_unknown(self): with self.assertRaises(ValueError): zopfli.ZopfliCompressor(-1) with self.assertRaises(ValueError): zopfli.ZopfliDecompressor(-1)
def _test_zopfli(self, fmt): for i in range(2): c = zopfli.ZopfliCompressor(fmt, block_splitting=i) b = b'Hello, world!' z = c.compress(b) + c.flush() self._test_decompress(fmt, z, b)
def main(): parser = argparse.ArgumentParser( description='Compresses ILBM IFF file with Deflate algorithm.') parser.add_argument('-m', '--method', type=str, choices=['none', 'deflate'], default='deflate', help='Compression method to use.') parser.add_argument('-q', '--quiet', action='store_true', help='Silence out diagnostic messages.') parser.add_argument( '-f', '--force', action='store_true', help='If the output file exists, the tool will overwrite it.') parser.add_argument('input', metavar='INPUT', type=str, help='Input ILBM IFF file name.') parser.add_argument('output', metavar='OUTPUT', type=str, nargs='?', help='Output ILBM IFF file name.') args = parser.parse_args() logLevel = [logging.INFO, logging.WARNING][args.quiet] logging.basicConfig(level=logLevel, format="%(levelname)s: %(message)s") if args.output is None: args.output = args.input if not os.path.isfile(args.input): raise SystemExit('File "%s" does not exists!' % args.input) if os.path.exists(args.output) and not args.force: raise SystemExit('File "%s" already exists (use "-f" to override).' % args.output) ilbm = ILBM() ilbm.ChunkBlackList.append('CRNG') if ilbm.load(args.input): bmhd = ilbm.get('BMHD') body = ilbm.get('BODY') size = ((bmhd.data.w + 15) & ~15) / 8 * bmhd.data.h * bmhd.data.nPlanes payload = body.data.read() logging.info(bmhd.data) logging.info('BODY size before compression: %d/%d' % (len(payload), size)) if bmhd.data.compression in [0, 1, 254, 255]: if bmhd.data.compression == 1: payload = UnRLE(payload) if bmhd.data.compression == 254: decompressor = zopfli.ZopfliDecompressor( zopfli.ZOPFLI_FORMAT_DEFLATE) payload = decompressor.decompress( payload) + decompressor.flush() compression = 0 if args.method == 'deflate': compressor = zopfli.ZopfliCompressor( zopfli.ZOPFLI_FORMAT_DEFLATE) payload = compressor.compress(payload) + compressor.flush() compression = 254 if args.method == 'none': compression = 0 body.data = payload bmhd.data = bmhd.data._replace(compression=compression) logging.info('BODY size after compression: %d/%d' % (len(body.data), size)) ilbm.save(args.output) else: logging.warning('Unknown compression: %d' % bmhd.data.compression)
def zlib_compress(data, level=-1): c = zopfli.ZopfliCompressor(zopfli.ZOPFLI_FORMAT_ZLIB, iterations=iterations) return c.compress(data) + c.flush()