blob: 0ae23d2d6b7a4bcfacf8b41aa8769293ff781526 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`gzip` --- Support for :program:`gzip` files
2=================================================
3
4.. module:: gzip
5 :synopsis: Interfaces for gzip compression and decompression using file objects.
6
Christian Heimesbbe741d2008-03-28 10:53:29 +00007This module provides a simple interface to compress and decompress files just
8like the GNU programs :program:`gzip` and :program:`gunzip` would.
Georg Brandl116aa622007-08-15 14:28:22 +00009
Georg Brandl1f01deb2009-01-03 22:47:39 +000010The data compression is provided by the :mod:`zlib` module.
Christian Heimesbbe741d2008-03-28 10:53:29 +000011
Antoine Pitrou11cb9612010-09-15 11:11:28 +000012The :mod:`gzip` module provides the :class:`GzipFile` class. The :class:`GzipFile`
13class reads and writes :program:`gzip`\ -format files, automatically compressing
14or decompressing the data so that it looks like an ordinary :term:`file object`.
Christian Heimesbbe741d2008-03-28 10:53:29 +000015
16Note that additional file formats which can be decompressed by the
17:program:`gzip` and :program:`gunzip` programs, such as those produced by
18:program:`compress` and :program:`pack`, are not supported by this module.
Georg Brandl116aa622007-08-15 14:28:22 +000019
Guido van Rossum77677112007-11-05 19:43:04 +000020For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
21:mod:`tarfile` modules.
22
Georg Brandl116aa622007-08-15 14:28:22 +000023The module defines the following items:
24
25
Georg Brandl036490d2009-05-17 13:00:36 +000026.. class:: GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
Georg Brandl116aa622007-08-15 14:28:22 +000027
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000028 Constructor for the :class:`GzipFile` class, which simulates most of the
29 methods of a :term:`file object`, with the exception of the :meth:`truncate`
30 method. At least one of *fileobj* and *filename* must be given a non-trivial
31 value.
Georg Brandl116aa622007-08-15 14:28:22 +000032
33 The new class instance is based on *fileobj*, which can be a regular file, a
34 :class:`StringIO` object, or any other object which simulates a file. It
35 defaults to ``None``, in which case *filename* is opened to provide a file
36 object.
37
38 When *fileobj* is not ``None``, the *filename* argument is only used to be
39 included in the :program:`gzip` file header, which may includes the original
40 filename of the uncompressed file. It defaults to the filename of *fileobj*, if
41 discernible; otherwise, it defaults to the empty string, and in this case the
42 original filename is not included in the header.
43
44 The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``,
45 or ``'wb'``, depending on whether the file will be read or written. The default
46 is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If
47 not given, the 'b' flag will be added to the mode to ensure the file is opened
48 in binary mode for cross-platform portability.
49
50 The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the
51 level of compression; ``1`` is fastest and produces the least compression, and
52 ``9`` is slowest and produces the most compression. The default is ``9``.
53
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000054 The *mtime* argument is an optional numeric timestamp to be written to
Benjamin Petersone0124bd2009-03-09 21:04:33 +000055 the stream when compressing. All :program:`gzip` compressed streams are
Antoine Pitrou42db3ef2009-01-04 21:37:59 +000056 required to contain a timestamp. If omitted or ``None``, the current
57 time is used. This module ignores the timestamp when decompressing;
58 however, some programs, such as :program:`gunzip`\ , make use of it.
59 The format of the timestamp is the same as that of the return value of
60 ``time.time()`` and of the ``st_mtime`` member of the object returned
61 by ``os.stat()``.
62
Georg Brandl116aa622007-08-15 14:28:22 +000063 Calling a :class:`GzipFile` object's :meth:`close` method does not close
64 *fileobj*, since you might wish to append more material after the compressed
Antoine Pitroue5768cf2010-09-23 16:45:17 +000065 data. This also allows you to pass a :class:`io.BytesIO` object opened for
Georg Brandl116aa622007-08-15 14:28:22 +000066 writing as *fileobj*, and retrieve the resulting memory buffer using the
Antoine Pitroue5768cf2010-09-23 16:45:17 +000067 :class:`io.BytesIO` object's :meth:`~io.BytesIO.getvalue` method.
Georg Brandl116aa622007-08-15 14:28:22 +000068
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000069 :class:`GzipFile` supports the :class:`io.BufferedIOBase` interface,
70 including iteration and the :keyword:`with` statement. Only the
71 :meth:`truncate` method isn't implemented.
Benjamin Petersone0124bd2009-03-09 21:04:33 +000072
Benjamin Peterson10745a92009-03-09 21:08:47 +000073 .. versionchanged:: 3.1
Benjamin Petersone0124bd2009-03-09 21:04:33 +000074 Support for the :keyword:`with` statement was added.
75
Antoine Pitrou8e33fd72010-01-13 14:37:26 +000076 .. versionchanged:: 3.2
77 Support for zero-padded files was added.
78
Antoine Pitrou7b969842010-09-23 16:22:51 +000079 .. versionchanged:: 3.2
80 Support for unseekable files was added.
81
Antoine Pitrouc3ed2e72010-09-29 10:49:46 +000082 .. versionchanged:: 3.2
83 The :meth:`peek` method was implemented.
84
Georg Brandl116aa622007-08-15 14:28:22 +000085
Georg Brandl036490d2009-05-17 13:00:36 +000086.. function:: open(filename, mode='rb', compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +000087
88 This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``.
89 The *filename* argument is required; *mode* defaults to ``'rb'`` and
90 *compresslevel* defaults to ``9``.
91
Antoine Pitrou79c5ef12010-08-17 21:10:05 +000092.. function:: compress(data, compresslevel=9)
93
94 Compress the *data*, returning a :class:`bytes` object containing
95 the compressed data. *compresslevel* has the same meaning as in
96 the :class:`GzipFile` constructor above.
97
Antoine Pitroucdfe1c52010-08-17 21:15:00 +000098 .. versionadded:: 3.2
99
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000100.. function:: decompress(data)
101
102 Decompress the *data*, returning a :class:`bytes` object containing the
103 uncompressed data.
104
Antoine Pitroucdfe1c52010-08-17 21:15:00 +0000105 .. versionadded:: 3.2
106
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Christian Heimesbbe741d2008-03-28 10:53:29 +0000108.. _gzip-usage-examples:
109
110Examples of usage
111-----------------
112
113Example of how to read a compressed file::
114
115 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000116 with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
117 file_content = f.read()
Christian Heimesbbe741d2008-03-28 10:53:29 +0000118
119Example of how to create a compressed GZIP file::
120
121 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000122 content = b"Lots of content here"
123 with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
124 f.write(content)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000125
126Example of how to GZIP compress an existing file::
127
128 import gzip
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000129 with open('/home/joe/file.txt', 'rb') as f_in:
Éric Araujof5be0902010-08-17 21:24:05 +0000130 with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
Antoine Pitroubf1a0182010-08-17 21:11:49 +0000131 f_out.writelines(f_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000132
Antoine Pitrou79c5ef12010-08-17 21:10:05 +0000133Example of how to GZIP compress a binary string::
134
135 import gzip
136 s_in = b"Lots of content here"
137 s_out = gzip.compress(s_in)
Christian Heimesbbe741d2008-03-28 10:53:29 +0000138
Georg Brandl116aa622007-08-15 14:28:22 +0000139.. seealso::
140
141 Module :mod:`zlib`
142 The basic data compression module needed to support the :program:`gzip` file
143 format.
144