Skip to content

hattya/zopflipy

Repository files navigation

ZopfliPy

A Python bindings for Zopfli.

image

image

image

image

Installation

$ pip install zopflipy

Requirements

  • Python 3.8+

Usage

ZopfliCompressor

>>> import zopfli
>>> c = zopfli.ZopfliCompressor(zopfli.ZOPFLI_FORMAT_DEFLATE)
>>> z = c.compress(b'Hello, world!') + c.flush()
>>> d = zopfli.ZopfliDecompressor(zopfli.ZOPFLI_FORMAT_DEFLATE)
>>> d.decompress(z) + d.flush()
b'Hello, world!''

ZopfliDeflater

>>> import zopfli
>>> c = zopfli.ZopfliDeflater()
>>> z = c.compress(b'Hello, world!') + c.flush()
>>> d = zopfli.ZopfliDecompressor(zopfli.ZOPFLI_FORMAT_DEFLATE)
>>> d.decompress(z) + d.flush()
b'Hello, world!''

ZopfliPNG

>>> import zopfli
>>> png = zopfli.ZopfliPNG()
>>> with open('in.png', 'rb') as fp:
...     data = fp.read()
>>> len(png.optimize(data)) < len(data)
True

ZipFile

A subclass of zipfile.ZipFile_ which uses ZopfliCompressor_ for the zipfile.ZIP_DEFLATED_ compression method.

>>> import zipfile
>>> import zopfli
>>> with zopfli.ZipFile('a.zip', 'w', zipfile.ZIP_DEFLATED) as zf:
...     zf.writestr('a.txt', b'Hello, world!')

License

ZopfliPy is distributed under the terms of the Apache License, Version 2.0.