blob: 5dff3d175e5b2c62b17ab7bb8887d6ded87f9743 [file] [log] [blame]
Fred Drakeb30d0161998-03-10 05:41:08 +00001\section{Standard Module \sectcode{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
Fred Drakeb30d0161998-03-10 05:41:08 +000012\class{GzipFile} objects simulate most of the methods of a file
13object, though it's not possible to use the \method{seek()} and
14\method{tell()} methods to access the file randomly.
Guido van Rossum41884a91997-07-17 16:26:11 +000015
Fred Drakeb30d0161998-03-10 05:41:08 +000016
17\begin{funcdesc}{open}{fileobj\optional{, filename\optional{, mode\optional{, compresslevel}}}}
18 Returns a new \class{GzipFile} object on top of \var{fileobj}, which
19 can be a regular file, a \class{StringIO} object, or any object which
Guido van Rossum41884a91997-07-17 16:26:11 +000020 simulates a file.
21
Fred Drakeb30d0161998-03-10 05:41:08 +000022 The \program{gzip} file format includes the original filename of the
23 uncompressed file; when opening a \class{GzipFile} object for
Guido van Rossum41884a91997-07-17 16:26:11 +000024 writing, it can be set by the \var{filename} argument. The default
Guido van Rossumeb0f0661997-12-30 20:38:16 +000025 value is an empty string.
Guido van Rossum41884a91997-07-17 16:26:11 +000026
27 \var{mode} can be either \code{'r'} or \code{'w'} depending on
28 whether the file will be read or written. \var{compresslevel} is an
Fred Drakeb30d0161998-03-10 05:41:08 +000029 integer from \code{1} to \code{9} controlling the level of
30 compression; \code{1} is fastest and produces the least compression,
31 and \code{9} is slowest and produces the most compression. The
32 default value of \var{compresslevel} is \code{9}.
Guido van Rossum41884a91997-07-17 16:26:11 +000033
Fred Drakeb30d0161998-03-10 05:41:08 +000034 Calling a \class{GzipFile} object's \method{close()} method does not
Guido van Rossum41884a91997-07-17 16:26:11 +000035 close \var{fileobj}, since you might wish to append more material
36 after the compressed data. This also allows you to pass a
Fred Drakeb30d0161998-03-10 05:41:08 +000037 \class{StringIO} object opened for writing as \var{fileobj}, and
38 retrieve the resulting memory buffer using the \class{StringIO}
39 object's \method{getvalue()} method.
Guido van Rossum41884a91997-07-17 16:26:11 +000040\end{funcdesc}
41
42\begin{seealso}
43\seemodule{zlib}{the basic data compression module}
44\end{seealso}
45