7.10 Standard Module gzip

 

The data compression provided by the zlib module is compatible with that used by the GNU compression program gzip. Accordingly, the gzip module provides the GzipFile class to read and write gzip-format files, automatically compressing or decompressing the data so it looks like an ordinary file object.

GzipFile objects simulate most of the methods of a file object, though it's not possible to use the seek() and tell() methods to access the file randomly.  

open (fileobj[, filename[, mode[, compresslevel]]])
Returns a new GzipFile object on top of fileobj, which can be a regular file, a StringIO object, or any object which simulates a file.

The gzip file format includes the original filename of the uncompressed file; when opening a GzipFile object for writing, it can be set by the filename argument. The default value is an empty string.

mode can be either 'r' or 'w' depending on whether the file will be read or written. compresslevel is an integer from 1 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. The default value of compresslevel is 9.

Calling a GzipFile object's close() method does not close fileobj, since you might wish to append more material after the compressed data. This also allows you to pass a StringIO object opened for writing as fileobj, and retrieve the resulting memory buffer using the StringIO object's getvalue() method.

See Also:

Module zlib   (the basic data compression module)

guido@python.org