Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{gzip} --- |
| 2 | \program{gzip} compression and decompression using files.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{gzip} |
| 4 | |
Fred Drake | ee7fd69 | 1998-07-27 22:30:15 +0000 | [diff] [blame] | 5 | \modulesynopsis{Interfaces for \program{gzip} compression and |
| 6 | decompression using file objects.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 8 | |
| 9 | The data compression provided by the \code{zlib} module is compatible |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 10 | with that used by the GNU compression program \program{gzip}. |
| 11 | Accordingly, the \module{gzip} module provides the \class{GzipFile} |
| 12 | class to read and write \program{gzip}-format files, automatically |
| 13 | compressing or decompressing the data so it looks like an ordinary |
| 14 | file object. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 16 | The module defines the following items: |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 18 | \begin{classdesc}{GzipFile}{\optional{filename\optional{, mode\optional{, |
| 19 | compresslevel\optional{, fileobj}}}}} |
| 20 | Constructor for the \class{GzipFile} class, which simulates most of |
| 21 | the 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 Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 25 | The new class instance is based on \var{fileobj}, which can be a |
| 26 | regular file, a \class{StringIO} object, or any other object which |
| 27 | simulates a file. It defaults to \code{None}, in which case |
| 28 | \var{filename} is opened to provide a file object. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 30 | When \var{fileobj} is not \code{None}, the \var{filename} argument is |
| 31 | only used to be included in the \program{gzip} file header, which may |
| 32 | includes the original filename of the uncompressed file. It defaults |
| 33 | to the filename of \var{fileobj}, if discernible; otherwise, it |
| 34 | defaults to the empty string, and in this case the original filename |
| 35 | is not included in the header. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 37 | The \var{mode} argument can be either \code{'r'} or \code{'w'}, |
| 38 | depending on whether the file will be read or written. The default is |
| 39 | the mode of \var{fileobj} if discernible; otherwise, the default is |
| 40 | \code{'r'}. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 7bda89f | 1998-07-06 20:47:40 +0000 | [diff] [blame] | 42 | The \var{compresslevel} argument is an integer from \code{1} to |
| 43 | \code{9} controlling the level of compression; \code{1} is fastest and |
| 44 | produces the least compression, and \code{9} is slowest and produces |
| 45 | the most compression. The default is \code{9}. |
| 46 | |
| 47 | Calling a \class{GzipFile} object's \method{close()} method does not |
| 48 | close \var{fileobj}, since you might wish to append more material |
| 49 | after the compressed data. This also allows you to pass a |
| 50 | \class{StringIO} object opened for writing as \var{fileobj}, and |
| 51 | retrieve the resulting memory buffer using the \class{StringIO} |
| 52 | object's \method{getvalue()} method. |
| 53 | \end{classdesc} |
| 54 | |
| 55 | \begin{funcdesc}{open}{filename\optional{, mode\optional{, compresslevel}}} |
| 56 | This is a shorthand for \code{GzipFile(\var{filename},} |
| 57 | \code{\var{mode},} \code{\var{compresslevel})}. The \var{filename} |
| 58 | argument is required; \var{mode} defaults to \code{'r'} and |
| 59 | \var{compresslevel} defaults to \code{9}. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 60 | \end{funcdesc} |
| 61 | |
| 62 | \begin{seealso} |
| 63 | \seemodule{zlib}{the basic data compression module} |
| 64 | \end{seealso} |