blob: 3bae73417474a930bef2d8df388448654de0b7b2 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{gzip} ---
2 \program{gzip} compression and decompression using files.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{gzip}
4
Fred Drakeee7fd691998-07-27 22:30:15 +00005\modulesynopsis{Interfaces for \program{gzip} compression and
6decompression using file objects.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum41884a91997-07-17 16:26:11 +00008
9The data compression provided by the \code{zlib} module is compatible
Fred Drakeb30d0161998-03-10 05:41:08 +000010with that used by the GNU compression program \program{gzip}.
11Accordingly, the \module{gzip} module provides the \class{GzipFile}
12class to read and write \program{gzip}-format files, automatically
13compressing or decompressing the data so it looks like an ordinary
14file object.
Guido van Rossum41884a91997-07-17 16:26:11 +000015
Guido van Rossum7bda89f1998-07-06 20:47:40 +000016The module defines the following items:
Guido van Rossum41884a91997-07-17 16:26:11 +000017
Guido van Rossum7bda89f1998-07-06 20:47:40 +000018\begin{classdesc}{GzipFile}{\optional{filename\optional{, mode\optional{,
19 compresslevel\optional{, fileobj}}}}}
20Constructor for the \class{GzipFile} class, which simulates most of
21the methods of a file object, with the exception of the
22\method{seek()} and \method{tell()} methods. At least one of
23\var{fileobj} and \var{filename} must be given a non-trivial value.
Fred Drakeb30d0161998-03-10 05:41:08 +000024
Guido van Rossum7bda89f1998-07-06 20:47:40 +000025The new class instance is based on \var{fileobj}, which can be a
26regular file, a \class{StringIO} object, or any other object which
27simulates a file. It defaults to \code{None}, in which case
28\var{filename} is opened to provide a file object.
Guido van Rossum41884a91997-07-17 16:26:11 +000029
Guido van Rossum7bda89f1998-07-06 20:47:40 +000030When \var{fileobj} is not \code{None}, the \var{filename} argument is
31only used to be included in the \program{gzip} file header, which may
32includes the original filename of the uncompressed file. It defaults
33to the filename of \var{fileobj}, if discernible; otherwise, it
34defaults to the empty string, and in this case the original filename
35is not included in the header.
Guido van Rossum41884a91997-07-17 16:26:11 +000036
Guido van Rossum7bda89f1998-07-06 20:47:40 +000037The \var{mode} argument can be either \code{'r'} or \code{'w'},
38depending on whether the file will be read or written. The default is
39the mode of \var{fileobj} if discernible; otherwise, the default is
40\code{'r'}.
Guido van Rossum41884a91997-07-17 16:26:11 +000041
Guido van Rossum7bda89f1998-07-06 20:47:40 +000042The \var{compresslevel} argument is an integer from \code{1} to
43\code{9} controlling the level of compression; \code{1} is fastest and
44produces the least compression, and \code{9} is slowest and produces
45the most compression. The default is \code{9}.
46
47Calling a \class{GzipFile} object's \method{close()} method does not
48close \var{fileobj}, since you might wish to append more material
49after the compressed data. This also allows you to pass a
50\class{StringIO} object opened for writing as \var{fileobj}, and
51retrieve the resulting memory buffer using the \class{StringIO}
52object's \method{getvalue()} method.
53\end{classdesc}
54
55\begin{funcdesc}{open}{filename\optional{, mode\optional{, compresslevel}}}
56This is a shorthand for \code{GzipFile(\var{filename},}
57\code{\var{mode},} \code{\var{compresslevel})}. The \var{filename}
58argument is required; \var{mode} defaults to \code{'r'} and
59\var{compresslevel} defaults to \code{9}.
Guido van Rossum41884a91997-07-17 16:26:11 +000060\end{funcdesc}
61
62\begin{seealso}
63\seemodule{zlib}{the basic data compression module}
64\end{seealso}