|
| 1 | +\section{Built-in Module \sectcode{gzip}} |
| 2 | +\label{module-gzip} |
| 3 | +\bimodindex{gzip} |
| 4 | + |
| 5 | +The data compression provided by the \code{zlib} module is compatible |
| 6 | +with that used by the GNU compression program \file{gzip}. |
| 7 | +Accordingly, the \code{gzip} module provides the \code{GzipFile} class |
| 8 | +to read and write \file{gzip}-format files, automatically compressing |
| 9 | +or decompressing the data so it looks like an ordinary file object. |
| 10 | + |
| 11 | +\code{GzipFile} objects simulate most of the methods of a file |
| 12 | +object, though it's not possible to use the \code{seek()} and |
| 13 | +\code{tell()} methods to access the file randomly. |
| 14 | + |
| 15 | +\renewcommand{\indexsubitem}{(in module gzip)} |
| 16 | +\begin{funcdesc}{open}{fileobj\optional{\, filename\optional{\, mode\, compresslevel}}} |
| 17 | + Returns a new \code{GzipFile} object on top of \var{fileobj}, which |
| 18 | + can be a regular file, a \code{StringIO} object, or any object which |
| 19 | + simulates a file. |
| 20 | + |
| 21 | + The \file{gzip} file format includes the original filename of the |
| 22 | + uncompressed file; when opening a \code{GzipFile} object for |
| 23 | + writing, it can be set by the \var{filename} argument. The default |
| 24 | + value is \code{"GzippedFile"}. |
| 25 | + |
| 26 | + \var{mode} can be either \code{'r'} or \code{'w'} depending on |
| 27 | + whether the file will be read or written. \var{compresslevel} is an |
| 28 | + integer from 1 to 9 controlling the level of compression; 1 is |
| 29 | + fastest and produces the least compression, and 9 is slowest and |
| 30 | + produces the most compression. The default value of |
| 31 | + \var{compresslevel} is 9. |
| 32 | + |
| 33 | + Calling a \code{GzipFile} object's \code{close()} method does not |
| 34 | + close \var{fileobj}, since you might wish to append more material |
| 35 | + after the compressed data. This also allows you to pass a |
| 36 | + \code{StringIO} object opened for writing as \var{fileobj}, and |
| 37 | + retrieve the resulting memory buffer using the \code{StringIO} |
| 38 | + object's \code{getvalue()} method. |
| 39 | +\end{funcdesc} |
| 40 | + |
| 41 | +\begin{seealso} |
| 42 | +\seemodule{zlib}{the basic data compression module} |
| 43 | +\end{seealso} |
| 44 | + |
0 commit comments