blob: e074bfc7032e58d954d0cfaf2a52ba7c820d0db6 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Éric Araujo29a0b572011-08-19 02:14:03 +02007**Source code:** :source:`Lib/gzip.py`
8
9--------------
10
Georg Brandl621cd262008-03-28 08:06:56 +000011This module provides a simple interface to compress and decompress files just
12like the GNU programs :program:`gzip` and :program:`gunzip` would.
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Georg Brandlfc29f272009-01-02 20:25:14 +000014The data compression is provided by the :mod:`zlib` module.
Georg Brandl621cd262008-03-28 08:06:56 +000015
16The :mod:`gzip` module provides the :class:`GzipFile` class which is modeled
17after Python's File Object. The :class:`GzipFile` class reads and writes
Georg Brandl8ec7f652007-08-15 14:28:01 +000018:program:`gzip`\ -format files, automatically compressing or decompressing the
Georg Brandl621cd262008-03-28 08:06:56 +000019data so that it looks like an ordinary file object.
20
21Note that additional file formats which can be decompressed by the
22:program:`gzip` and :program:`gunzip` programs, such as those produced by
23:program:`compress` and :program:`pack`, are not supported by this module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000024
Mark Summerfieldaea6e592007-11-05 09:22:48 +000025For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
26:mod:`tarfile` modules.
27
Georg Brandl8ec7f652007-08-15 14:28:01 +000028The module defines the following items:
29
30
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +000031.. class:: GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])
Georg Brandl8ec7f652007-08-15 14:28:01 +000032
33 Constructor for the :class:`GzipFile` class, which simulates most of the methods
34 of a file object, with the exception of the :meth:`readinto` and
35 :meth:`truncate` methods. At least one of *fileobj* and *filename* must be
36 given a non-trivial value.
37
38 The new class instance is based on *fileobj*, which can be a regular file, a
39 :class:`StringIO` object, or any other object which simulates a file. It
40 defaults to ``None``, in which case *filename* is opened to provide a file
41 object.
42
43 When *fileobj* is not ``None``, the *filename* argument is only used to be
44 included in the :program:`gzip` file header, which may includes the original
45 filename of the uncompressed file. It defaults to the filename of *fileobj*, if
46 discernible; otherwise, it defaults to the empty string, and in this case the
47 original filename is not included in the header.
48
49 The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``,
50 or ``'wb'``, depending on whether the file will be read or written. The default
51 is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If
52 not given, the 'b' flag will be added to the mode to ensure the file is opened
53 in binary mode for cross-platform portability.
54
55 The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the
56 level of compression; ``1`` is fastest and produces the least compression, and
57 ``9`` is slowest and produces the most compression. The default is ``9``.
58
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +000059 The *mtime* argument is an optional numeric timestamp to be written to
Georg Brandl38f1bf62009-03-09 16:35:48 +000060 the stream when compressing. All :program:`gzip` compressed streams are
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +000061 required to contain a timestamp. If omitted or ``None``, the current
62 time is used. This module ignores the timestamp when decompressing;
63 however, some programs, such as :program:`gunzip`\ , make use of it.
64 The format of the timestamp is the same as that of the return value of
Senthil Kumaran6f18b982011-07-04 12:50:02 -070065 ``time.time()`` and of the ``st_mtime`` attribute of the object returned
Antoine Pitrouf0d2c3f2009-01-04 21:29:23 +000066 by ``os.stat()``.
67
Georg Brandl8ec7f652007-08-15 14:28:01 +000068 Calling a :class:`GzipFile` object's :meth:`close` method does not close
69 *fileobj*, since you might wish to append more material after the compressed
70 data. This also allows you to pass a :class:`StringIO` object opened for
71 writing as *fileobj*, and retrieve the resulting memory buffer using the
72 :class:`StringIO` object's :meth:`getvalue` method.
73
Georg Brandld1068be2010-03-21 09:09:38 +000074 :class:`GzipFile` supports iteration and the :keyword:`with` statement.
Benjamin Peterson6d834292009-03-09 20:38:56 +000075
76 .. versionchanged:: 2.7
77 Support for the :keyword:`with` statement was added.
78
Antoine Pitrou5a9112c2010-01-13 14:32:10 +000079 .. versionchanged:: 2.7
80 Support for zero-padded files was added.
81
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83.. function:: open(filename[, mode[, compresslevel]])
84
85 This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``.
86 The *filename* argument is required; *mode* defaults to ``'rb'`` and
87 *compresslevel* defaults to ``9``.
88
89
Georg Brandl621cd262008-03-28 08:06:56 +000090.. _gzip-usage-examples:
91
92Examples of usage
93-----------------
94
95Example of how to read a compressed file::
96
97 import gzip
98 f = gzip.open('/home/joe/file.txt.gz', 'rb')
99 file_content = f.read()
100 f.close()
101
102Example of how to create a compressed GZIP file::
103
104 import gzip
105 content = "Lots of content here"
106 f = gzip.open('/home/joe/file.txt.gz', 'wb')
107 f.write(content)
108 f.close()
109
110Example of how to GZIP compress an existing file::
111
112 import gzip
113 f_in = open('/home/joe/file.txt', 'rb')
114 f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
115 f_out.writelines(f_in)
116 f_out.close()
117 f_in.close()
118
119
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120.. seealso::
121
122 Module :mod:`zlib`
123 The basic data compression module needed to support the :program:`gzip` file
124 format.
125