/Doc/library/gzip.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 104 lines · 73 code · 31 blank · 0 comment · 0 complexity · ac0ecb8ddbf8a7f6317207579e045f32 MD5 · raw file

  1. :mod:`gzip` --- Support for :program:`gzip` files
  2. =================================================
  3. .. module:: gzip
  4. :synopsis: Interfaces for gzip compression and decompression using file objects.
  5. This module provides a simple interface to compress and decompress files just
  6. like the GNU programs :program:`gzip` and :program:`gunzip` would.
  7. The data compression is provided by the :mod:`zlib` module.
  8. The :mod:`gzip` module provides the :class:`GzipFile` class which is modeled
  9. after Python's File Object. The :class:`GzipFile` class reads and writes
  10. :program:`gzip`\ -format files, automatically compressing or decompressing the
  11. data so that it looks like an ordinary file object.
  12. Note that additional file formats which can be decompressed by the
  13. :program:`gzip` and :program:`gunzip` programs, such as those produced by
  14. :program:`compress` and :program:`pack`, are not supported by this module.
  15. For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
  16. :mod:`tarfile` modules.
  17. The module defines the following items:
  18. .. class:: GzipFile([filename[, mode[, compresslevel[, fileobj]]]])
  19. Constructor for the :class:`GzipFile` class, which simulates most of the methods
  20. of a file object, with the exception of the :meth:`readinto` and
  21. :meth:`truncate` methods. At least one of *fileobj* and *filename* must be
  22. given a non-trivial value.
  23. The new class instance is based on *fileobj*, which can be a regular file, a
  24. :class:`StringIO` object, or any other object which simulates a file. It
  25. defaults to ``None``, in which case *filename* is opened to provide a file
  26. object.
  27. When *fileobj* is not ``None``, the *filename* argument is only used to be
  28. included in the :program:`gzip` file header, which may includes the original
  29. filename of the uncompressed file. It defaults to the filename of *fileobj*, if
  30. discernible; otherwise, it defaults to the empty string, and in this case the
  31. original filename is not included in the header.
  32. The *mode* argument can be any of ``'r'``, ``'rb'``, ``'a'``, ``'ab'``, ``'w'``,
  33. or ``'wb'``, depending on whether the file will be read or written. The default
  34. is the mode of *fileobj* if discernible; otherwise, the default is ``'rb'``. If
  35. not given, the 'b' flag will be added to the mode to ensure the file is opened
  36. in binary mode for cross-platform portability.
  37. The *compresslevel* argument is an integer from ``1`` to ``9`` controlling the
  38. level of compression; ``1`` is fastest and produces the least compression, and
  39. ``9`` is slowest and produces the most compression. The default is ``9``.
  40. Calling a :class:`GzipFile` object's :meth:`close` method does not close
  41. *fileobj*, since you might wish to append more material after the compressed
  42. data. This also allows you to pass a :class:`StringIO` object opened for
  43. writing as *fileobj*, and retrieve the resulting memory buffer using the
  44. :class:`StringIO` object's :meth:`getvalue` method.
  45. .. function:: open(filename[, mode[, compresslevel]])
  46. This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``.
  47. The *filename* argument is required; *mode* defaults to ``'rb'`` and
  48. *compresslevel* defaults to ``9``.
  49. .. _gzip-usage-examples:
  50. Examples of usage
  51. -----------------
  52. Example of how to read a compressed file::
  53. import gzip
  54. f = gzip.open('/home/joe/file.txt.gz', 'rb')
  55. file_content = f.read()
  56. f.close()
  57. Example of how to create a compressed GZIP file::
  58. import gzip
  59. content = "Lots of content here"
  60. f = gzip.open('/home/joe/file.txt.gz', 'wb')
  61. f.write(content)
  62. f.close()
  63. Example of how to GZIP compress an existing file::
  64. import gzip
  65. f_in = open('/home/joe/file.txt', 'rb')
  66. f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
  67. f_out.writelines(f_in)
  68. f_out.close()
  69. f_in.close()
  70. .. seealso::
  71. Module :mod:`zlib`
  72. The basic data compression module needed to support the :program:`gzip` file
  73. format.