blob: f649a4d8df266f0cda0f831c547dd4d88040be51 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{gzip}}
Guido van Rossum41884a91997-07-17 16:26:11 +00002\label{module-gzip}
Fred Drakeb30d0161998-03-10 05:41:08 +00003\stmodindex{gzip}
Guido van Rossum41884a91997-07-17 16:26:11 +00004
5The data compression provided by the \code{zlib} module is compatible
Fred Drakeb30d0161998-03-10 05:41:08 +00006with that used by the GNU compression program \program{gzip}.
7Accordingly, the \module{gzip} module provides the \class{GzipFile}
8class to read and write \program{gzip}-format files, automatically
9compressing or decompressing the data so it looks like an ordinary
10file object.
Guido van Rossum41884a91997-07-17 16:26:11 +000011
Guido van Rossum7bda89f1998-07-06 20:47:40 +000012The module defines the following items:
Guido van Rossum41884a91997-07-17 16:26:11 +000013
Guido van Rossum7bda89f1998-07-06 20:47:40 +000014\begin{classdesc}{GzipFile}{\optional{filename\optional{, mode\optional{,
15 compresslevel\optional{, fileobj}}}}}
16Constructor for the \class{GzipFile} class, which simulates most of
17the methods of a file object, with the exception of the
18\method{seek()} and \method{tell()} methods. At least one of
19\var{fileobj} and \var{filename} must be given a non-trivial value.
Fred Drakeb30d0161998-03-10 05:41:08 +000020
Guido van Rossum7bda89f1998-07-06 20:47:40 +000021The new class instance is based on \var{fileobj}, which can be a
22regular file, a \class{StringIO} object, or any other object which
23simulates a file. It defaults to \code{None}, in which case
24\var{filename} is opened to provide a file object.
Guido van Rossum41884a91997-07-17 16:26:11 +000025
Guido van Rossum7bda89f1998-07-06 20:47:40 +000026When \var{fileobj} is not \code{None}, the \var{filename} argument is
27only used to be included in the \program{gzip} file header, which may
28includes the original filename of the uncompressed file. It defaults
29to the filename of \var{fileobj}, if discernible; otherwise, it
30defaults to the empty string, and in this case the original filename
31is not included in the header.
Guido van Rossum41884a91997-07-17 16:26:11 +000032
Guido van Rossum7bda89f1998-07-06 20:47:40 +000033The \var{mode} argument can be either \code{'r'} or \code{'w'},
34depending on whether the file will be read or written. The default is
35the mode of \var{fileobj} if discernible; otherwise, the default is
36\code{'r'}.
Guido van Rossum41884a91997-07-17 16:26:11 +000037
Guido van Rossum7bda89f1998-07-06 20:47:40 +000038The \var{compresslevel} argument is an integer from \code{1} to
39\code{9} controlling the level of compression; \code{1} is fastest and
40produces the least compression, and \code{9} is slowest and produces
41the most compression. The default is \code{9}.
42
43Calling a \class{GzipFile} object's \method{close()} method does not
44close \var{fileobj}, since you might wish to append more material
45after the compressed data. This also allows you to pass a
46\class{StringIO} object opened for writing as \var{fileobj}, and
47retrieve the resulting memory buffer using the \class{StringIO}
48object's \method{getvalue()} method.
49\end{classdesc}
50
51\begin{funcdesc}{open}{filename\optional{, mode\optional{, compresslevel}}}
52This is a shorthand for \code{GzipFile(\var{filename},}
53\code{\var{mode},} \code{\var{compresslevel})}. The \var{filename}
54argument is required; \var{mode} defaults to \code{'r'} and
55\var{compresslevel} defaults to \code{9}.
Guido van Rossum41884a91997-07-17 16:26:11 +000056\end{funcdesc}
57
58\begin{seealso}
59\seemodule{zlib}{the basic data compression module}
60\end{seealso}