Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{gzip}} |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 2 | \label{module-gzip} |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 3 | \stmodindex{gzip} |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 4 | |
| 5 | The data compression provided by the \code{zlib} module is compatible |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 6 | with that used by the GNU compression program \program{gzip}. |
| 7 | Accordingly, the \module{gzip} module provides the \class{GzipFile} |
| 8 | class to read and write \program{gzip}-format files, automatically |
| 9 | compressing or decompressing the data so it looks like an ordinary |
| 10 | file object. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 11 | |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 12 | \class{GzipFile} objects simulate most of the methods of a file |
| 13 | object, though it's not possible to use the \method{seek()} and |
| 14 | \method{tell()} methods to access the file randomly. |
Fred Drake | c348631 | 1998-04-03 06:48:16 +0000 | [diff] [blame] | 15 | \withsubitem{(class in gzip)}{\ttindex{GzipFile}} |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 16 | |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 17 | |
Fred Drake | c348631 | 1998-04-03 06:48:16 +0000 | [diff] [blame] | 18 | \begin{funcdesc}{open}{fileobj\optional{, filename\optional{, |
| 19 | mode\optional{, compresslevel}}}} |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 20 | 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 Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 22 | simulates a file. |
| 23 | |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 24 | The \program{gzip} file format includes the original filename of the |
| 25 | uncompressed file; when opening a \class{GzipFile} object for |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 26 | writing, it can be set by the \var{filename} argument. The default |
Guido van Rossum | eb0f066 | 1997-12-30 20:38:16 +0000 | [diff] [blame] | 27 | value is an empty string. |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 28 | |
| 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 Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 31 | 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 Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 35 | |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 36 | Calling a \class{GzipFile} object's \method{close()} method does not |
Guido van Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 37 | close \var{fileobj}, since you might wish to append more material |
| 38 | after the compressed data. This also allows you to pass a |
Fred Drake | b30d016 | 1998-03-10 05:41:08 +0000 | [diff] [blame] | 39 | \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 Rossum | 41884a9 | 1997-07-17 16:26:11 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
| 44 | \begin{seealso} |
| 45 | \seemodule{zlib}{the basic data compression module} |
| 46 | \end{seealso} |
| 47 | |