"gzip" — **gzip** ファイルのサポート
************************************

**ソースコード:** Lib/gzip.py

======================================================================

このモジュールは、GNU の **gzip** や **gunzip** のようにファイルを圧縮
、展開するシンプルなインターフェイスを提供しています。

データ圧縮は "zlib" モジュールで提供されています。

The "gzip" module provides the "GzipFile" class which is modeled after
Python’s File Object. The "GzipFile" class reads and writes
**gzip**-format files, automatically compressing or decompressing the
data so that it looks like an ordinary file object.

**compress** や **pack** 等によって作成され、**gzip** や **gunzip** が
展開できる他のファイル形式についてはこのモジュールは対応していないので
注意してください。

このモジュールは以下の項目を定義しています:

class gzip.GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])

   Constructor for the "GzipFile" class, which simulates most of the
   methods of a file object, with the exception of the "readinto()"
   and "truncate()" methods.  At least one of *fileobj* and *filename*
   must be given a non-trivial value.

   The new class instance is based on *fileobj*, which can be a
   regular file, a "StringIO" object, or any other object which
   simulates a file.  It defaults to "None", in which case *filename*
   is opened to provide a file object.

   *fileobj* が "None" でない場合、*filename* 引数は **gzip** ファイル
   ヘッダにインクルードされることのみに使用されます。**gzip** ファイル
   ヘッダは圧縮されていないファイルの元の名前をインクルードするかもし
   れません。認識可能な場合、規定値は *fileobj* のファイル名です。そう
   でない場合、規定値は空の文字列で、元のファイル名はヘッダにはインク
   ルードされません。

   The *mode* argument can be any of "'r'", "'rb'", "'a'", "'ab'",
   "'w'", or "'wb'", depending on whether the file will be read or
   written.  The default is the mode of *fileobj* if discernible;
   otherwise, the default is "'rb'". If not given, the 『b』 flag will
   be added to the mode to ensure the file is opened in binary mode
   for cross-platform portability.

   引数 *compresslevel* は "0" から "9" の整数を取り、圧縮レベルを制御
   します; "1" は最も高速で最小限の圧縮を行い、"9" は最も低速ですが最
   大限の圧縮を行います。"0" は圧縮しません。デフォルトは "9" です。

   The *mtime* argument is an optional numeric timestamp to be written
   to the stream when compressing.  All **gzip** compressed streams
   are required to contain a timestamp.  If omitted or "None", the
   current time is used.  This module ignores the timestamp when
   decompressing; however, some programs, such as **gunzip**, make use
   of it. The format of the timestamp is the same as that of the
   return value of "time.time()" and of the "st_mtime" attribute of
   the object returned by "os.stat()".

   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.

   "GzipFile" supports iteration and the "with" statement.

   バージョン 2.7 で変更: Support for the "with" statement was added.

   バージョン 2.7 で変更: Support for zero-padded files was added.

   バージョン 2.7 で追加: The *mtime* argument.

gzip.open(filename[, mode[, compresslevel]])

   This is a shorthand for "GzipFile(filename," "mode,"
   "compresslevel)". The *filename* argument is required; *mode*
   defaults to "'rb'" and *compresslevel* defaults to "9".


使い方の例
==========

圧縮されたファイルを読み込む例:

   import gzip
   with gzip.open('file.txt.gz', 'rb') as f:
       file_content = f.read()

GZIP 圧縮されたファイルを作成する例:

   import gzip
   content = "Lots of content here"
   with gzip.open('file.txt.gz', 'wb') as f:
       f.write(content)

既存のファイルを GZIP 圧縮する例:

   import gzip
   import shutil
   with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out:
       shutil.copyfileobj(f_in, f_out)

参考:

  "zlib" モジュール
     **gzip** ファイル形式のサポートを行うために必要な基本ライブラリモ
     ジュール。
