|
| 1 | +:mod:`gzip` -- gzip compression & decompression |
| 2 | +=============================================== |
| 3 | + |
| 4 | +.. module:: gzip |
| 5 | + :synopsis: gzip compression & decompression |
| 6 | + |
| 7 | +|see_cpython_module| :mod:`python:gzip`. |
| 8 | + |
| 9 | +This module allows compression and decompression of binary data with the |
| 10 | +`DEFLATE algorithm <https://en.wikipedia.org/wiki/DEFLATE>`_ used by the gzip |
| 11 | +file format. |
| 12 | + |
| 13 | +.. note:: Prefer to use :class:`deflate.DeflateIO` instead of the functions in this |
| 14 | + module as it provides a streaming interface to compression and decompression |
| 15 | + which is convenient and more memory efficient when working with reading or |
| 16 | + writing compressed data to a file, socket, or stream. |
| 17 | + |
| 18 | +**Availability:** |
| 19 | + |
| 20 | +* This module is **not present by default** in official MicroPython firmware |
| 21 | + releases as it duplicates functionality available in the :mod:`deflate |
| 22 | + <deflate>` module. |
| 23 | + |
| 24 | +* A copy of this module can be installed (or frozen) |
| 25 | + from :term:`micropython-lib` (`source <https://github.com/micropython/micropython-lib/blob/master/python-stdlib/gzip/gzip.py>`_). |
| 26 | + See :ref:`packages` for more information. This documentation describes that module. |
| 27 | + |
| 28 | +* Compression support will only be available if compression support is enabled |
| 29 | + in the built-in :mod:`deflate <deflate>` module. |
| 30 | + |
| 31 | +Functions |
| 32 | +--------- |
| 33 | + |
| 34 | +.. function:: open(filename, mode, /) |
| 35 | + |
| 36 | + Wrapper around built-in :func:`open` returning a GzipFile instance. |
| 37 | + |
| 38 | +.. function:: decompress(data, /) |
| 39 | + |
| 40 | + Decompresses *data* into a bytes object. |
| 41 | + |
| 42 | +.. function:: compress(data, /) |
| 43 | + |
| 44 | + Compresses *data* into a bytes object. |
| 45 | + |
| 46 | +Classes |
| 47 | +------- |
| 48 | + |
| 49 | +.. class:: GzipFile(*, fileobj, mode) |
| 50 | + |
| 51 | + This class can be used to wrap a *fileobj* which is any |
| 52 | + :term:`stream-like <stream>` object such as a file, socket, or stream |
| 53 | + (including :class:`io.BytesIO`). It is itself a stream and implements the |
| 54 | + standard read/readinto/write/close methods. |
| 55 | + |
| 56 | + When the *mode* argument is ``"rb"``, reads from the GzipFile instance will |
| 57 | + decompress the data in the underlying stream and return decompressed data. |
| 58 | + |
| 59 | + If compression support is enabled then the *mode* argument can be set to |
| 60 | + ``"wb"``, and writes to the GzipFile instance will be compressed and written |
| 61 | + to the underlying stream. |
| 62 | + |
| 63 | + By default the GzipFile class will read and write data using the gzip file |
| 64 | + format, including a header and footer with checksum and a window size of 512 |
| 65 | + bytes. |
| 66 | + |
| 67 | + The **file**, **compresslevel**, and **mtime** arguments are not |
| 68 | + supported. **fileobj** and **mode** must always be specified as keyword |
| 69 | + arguments. |
| 70 | + |
| 71 | +Examples |
| 72 | +-------- |
| 73 | + |
| 74 | +A typical use case for :class:`gzip.GzipFile` is to read or write a compressed |
| 75 | +file from storage: |
| 76 | + |
| 77 | +.. code:: python |
| 78 | +
|
| 79 | + import gzip |
| 80 | +
|
| 81 | + # Reading: |
| 82 | + with open("data.gz", "rb") as f: |
| 83 | + with gzip.GzipFile(fileobj=f, mode="rb") as g: |
| 84 | + # Use g.read(), g.readinto(), etc. |
| 85 | +
|
| 86 | + # Same, but using gzip.open: |
| 87 | + with gzip.open("data.gz", "rb") as f: |
| 88 | + # Use f.read(), f.readinto(), etc. |
| 89 | +
|
| 90 | + # Writing: |
| 91 | + with open("data.gz", "wb") as f: |
| 92 | + with gzip.GzipFile(fileobj=f, mode="wb") as g: |
| 93 | + # Use g.write(...) etc |
| 94 | +
|
| 95 | + # Same, but using gzip.open: |
| 96 | + with gzip.open("data.gz", "wb") as f: |
| 97 | + # Use f.write(...) etc |
| 98 | +
|
| 99 | + # Write a dictionary as JSON in gzip format, with a |
| 100 | + # small (64 byte) window size. |
| 101 | + config = { ... } |
| 102 | + with gzip.open("config.gz", "wb") as f: |
| 103 | + json.dump(config, f) |
| 104 | +
|
| 105 | +For guidance on working with gzip sources and choosing the window size see the |
| 106 | +note at the :ref:`end of the deflate documentation <deflate_wbits>`. |
0 commit comments