blob: fd2bc283901e2af340add4492d9a9f74bd4f0051 [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
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.
Fred Drakec3486311998-04-03 06:48:16 +000015\withsubitem{(class in gzip)}{\ttindex{GzipFile}}
Guido van Rossum41884a91997-07-17 16:26:11 +000016
Fred Drakeb30d0161998-03-10 05:41:08 +000017
Fred Drakec3486311998-04-03 06:48:16 +000018\begin{funcdesc}{open}{fileobj\optional{, filename\optional{,
19 mode\optional{, compresslevel}}}}
Fred Drakeb30d0161998-03-10 05:41:08 +000020 Returns a new \class{GzipFile} object on top of \var{fileobj}, which
21 can be a regular file, a \class{StringIO} object, or any object which
Guido van Rossum41884a91997-07-17 16:26:11 +000022 simulates a file.
23
Fred Drakeb30d0161998-03-10 05:41:08 +000024 The \program{gzip} file format includes the original filename of the
25 uncompressed file; when opening a \class{GzipFile} object for
Guido van Rossum41884a91997-07-17 16:26:11 +000026 writing, it can be set by the \var{filename} argument. The default
Guido van Rossumeb0f0661997-12-30 20:38:16 +000027 value is an empty string.
Guido van Rossum41884a91997-07-17 16:26:11 +000028
29 \var{mode} can be either \code{'r'} or \code{'w'} depending on
30 whether the file will be read or written. \var{compresslevel} is an
Fred Drakeb30d0161998-03-10 05:41:08 +000031 integer from \code{1} to \code{9} controlling the level of
32 compression; \code{1} is fastest and produces the least compression,
33 and \code{9} is slowest and produces the most compression. The
34 default value of \var{compresslevel} is \code{9}.
Guido van Rossum41884a91997-07-17 16:26:11 +000035
Fred Drakeb30d0161998-03-10 05:41:08 +000036 Calling a \class{GzipFile} object's \method{close()} method does not
Guido van Rossum41884a91997-07-17 16:26:11 +000037 close \var{fileobj}, since you might wish to append more material
38 after the compressed data. This also allows you to pass a
Fred Drakeb30d0161998-03-10 05:41:08 +000039 \class{StringIO} object opened for writing as \var{fileobj}, and
40 retrieve the resulting memory buffer using the \class{StringIO}
41 object's \method{getvalue()} method.
Guido van Rossum41884a91997-07-17 16:26:11 +000042\end{funcdesc}
43
44\begin{seealso}
45\seemodule{zlib}{the basic data compression module}
46\end{seealso}
47