The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method. This class only supports opening files in binary mode. If you need to open a compressed file in text mode, use the gzip.open() function.
| 119 | |
| 120 | |
| 121 | class GzipFile(_compression.BaseStream): |
| 122 | """The GzipFile class simulates most of the methods of a file object with |
| 123 | the exception of the truncate() method. |
| 124 | |
| 125 | This class only supports opening files in binary mode. If you need to open a |
| 126 | compressed file in text mode, use the gzip.open() function. |
| 127 | |
| 128 | """ |
| 129 | |
| 130 | # Overridden with internal file object to be closed, if only a filename |
| 131 | # is passed in |
| 132 | myfileobj = None |
| 133 | |
| 134 | def __init__(self, filename=None, mode=None, |
| 135 | compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None): |
| 136 | """Constructor for the GzipFile class. |
| 137 | |
| 138 | At least one of fileobj and filename must be given a |
| 139 | non-trivial value. |
| 140 | |
| 141 | The new class instance is based on fileobj, which can be a regular |
| 142 | file, an io.BytesIO object, or any other object which simulates a file. |
| 143 | It defaults to None, in which case filename is opened to provide |
| 144 | a file object. |
| 145 | |
| 146 | When fileobj is not None, the filename argument is only used to be |
| 147 | included in the gzip file header, which may include the original |
| 148 | filename of the uncompressed file. It defaults to the filename of |
| 149 | fileobj, if discernible; otherwise, it defaults to the empty string, |
| 150 | and in this case the original filename is not included in the header. |
| 151 | |
| 152 | The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or |
| 153 | 'xb' depending on whether the file will be read or written. The default |
| 154 | is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
| 155 | A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and |
| 156 | 'wb', 'a' and 'ab', and 'x' and 'xb'. |
| 157 | |
| 158 | The compresslevel argument is an integer from 0 to 9 controlling the |
| 159 | level of compression; 1 is fastest and produces the least compression, |
| 160 | and 9 is slowest and produces the most compression. 0 is no compression |
| 161 | at all. The default is 9. |
| 162 | |
| 163 | The mtime argument is an optional numeric timestamp to be written |
| 164 | to the last modification time field in the stream when compressing. |
| 165 | If omitted or None, the current time is used. |
| 166 | |
| 167 | """ |
| 168 | |
| 169 | if mode and ('t' in mode or 'U' in mode): |
| 170 | raise ValueError("Invalid mode: {!r}".format(mode)) |
| 171 | if mode and 'b' not in mode: |
| 172 | mode += 'b' |
| 173 | if fileobj is None: |
| 174 | fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') |
| 175 | if filename is None: |
| 176 | filename = getattr(fileobj, 'name', '') |
| 177 | if not isinstance(filename, (str, bytes)): |
| 178 | filename = '' |