blob: b4cc6594498339e2adb1c8ec5712170b47114165 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{gzip} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 Support for \program{gzip} files}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{standard}{gzip}
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
Fred Drake1dc3a501999-04-05 19:00:54 +000014file object. Note that additional file formats which can be
15decompressed by the \program{gzip} and \program{gunzip} programs, such
16as those produced by \program{compress} and \program{pack}, are not
17supported by this module.
Guido van Rossum41884a91997-07-17 16:26:11 +000018
Guido van Rossum7bda89f1998-07-06 20:47:40 +000019The module defines the following items:
Guido van Rossum41884a91997-07-17 16:26:11 +000020
Guido van Rossum7bda89f1998-07-06 20:47:40 +000021\begin{classdesc}{GzipFile}{\optional{filename\optional{, mode\optional{,
22 compresslevel\optional{, fileobj}}}}}
23Constructor for the \class{GzipFile} class, which simulates most of
Guido van Rossum97c5fcc2002-08-06 17:03:25 +000024the methods of a file object, with the exception of the \method{readinto()}
25and \method{truncate()} methods. At least one of
Guido van Rossum7bda89f1998-07-06 20:47:40 +000026\var{fileobj} and \var{filename} must be given a non-trivial value.
Fred Drakeb30d0161998-03-10 05:41:08 +000027
Guido van Rossum7bda89f1998-07-06 20:47:40 +000028The new class instance is based on \var{fileobj}, which can be a
29regular file, a \class{StringIO} object, or any other object which
30simulates a file. It defaults to \code{None}, in which case
31\var{filename} is opened to provide a file object.
Guido van Rossum41884a91997-07-17 16:26:11 +000032
Guido van Rossum7bda89f1998-07-06 20:47:40 +000033When \var{fileobj} is not \code{None}, the \var{filename} argument is
34only used to be included in the \program{gzip} file header, which may
35includes the original filename of the uncompressed file. It defaults
36to the filename of \var{fileobj}, if discernible; otherwise, it
37defaults to the empty string, and in this case the original filename
38is not included in the header.
Guido van Rossum41884a91997-07-17 16:26:11 +000039
Fred Drake1dc3a501999-04-05 19:00:54 +000040The \var{mode} argument can be any of \code{'r'}, \code{'rb'},
41\code{'a'}, \code{'ab'}, \code{'w'}, or \code{'wb'}, depending on
42whether the file will be read or written. The default is the mode of
43\var{fileobj} if discernible; otherwise, the default is \code{'rb'}.
Skip Montanarode994d92002-08-02 17:20:46 +000044If not given, the 'b' flag will be added to the mode to ensure the
45file is opened in binary mode for cross-platform portability.
Guido van Rossum41884a91997-07-17 16:26:11 +000046
Guido van Rossum7bda89f1998-07-06 20:47:40 +000047The \var{compresslevel} argument is an integer from \code{1} to
48\code{9} controlling the level of compression; \code{1} is fastest and
49produces the least compression, and \code{9} is slowest and produces
50the most compression. The default is \code{9}.
51
52Calling a \class{GzipFile} object's \method{close()} method does not
53close \var{fileobj}, since you might wish to append more material
54after the compressed data. This also allows you to pass a
55\class{StringIO} object opened for writing as \var{fileobj}, and
56retrieve the resulting memory buffer using the \class{StringIO}
57object's \method{getvalue()} method.
58\end{classdesc}
59
60\begin{funcdesc}{open}{filename\optional{, mode\optional{, compresslevel}}}
61This is a shorthand for \code{GzipFile(\var{filename},}
62\code{\var{mode},} \code{\var{compresslevel})}. The \var{filename}
Fred Drake1dc3a501999-04-05 19:00:54 +000063argument is required; \var{mode} defaults to \code{'rb'} and
Guido van Rossum7bda89f1998-07-06 20:47:40 +000064\var{compresslevel} defaults to \code{9}.
Guido van Rossum41884a91997-07-17 16:26:11 +000065\end{funcdesc}
66
67\begin{seealso}
Fred Drakeba0a9892000-10-18 17:43:06 +000068 \seemodule{zlib}{The basic data compression module needed to support
69 the \program{gzip} file format.}
Guido van Rossum41884a91997-07-17 16:26:11 +000070\end{seealso}