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.
| 145 | |
| 146 | |
| 147 | class GzipFile(_streams.BaseStream): |
| 148 | """The GzipFile class simulates most of the methods of a file object with |
| 149 | the exception of the truncate() method. |
| 150 | |
| 151 | This class only supports opening files in binary mode. If you need to open a |
| 152 | compressed file in text mode, use the gzip.open() function. |
| 153 | |
| 154 | """ |
| 155 | |
| 156 | # Overridden with internal file object to be closed, if only a filename |
| 157 | # is passed in |
| 158 | myfileobj = None |
| 159 | |
| 160 | def __init__(self, filename=None, mode=None, |
| 161 | compresslevel=_COMPRESS_LEVEL_TRADEOFF, fileobj=None, mtime=None): |
| 162 | """Constructor for the GzipFile class. |
| 163 | |
| 164 | At least one of fileobj and filename must be given a |
| 165 | non-trivial value. |
| 166 | |
| 167 | The new class instance is based on fileobj, which can be a regular |
| 168 | file, an io.BytesIO object, or any other object which simulates a file. |
| 169 | It defaults to None, in which case filename is opened to provide |
| 170 | a file object. |
| 171 | |
| 172 | When fileobj is not None, the filename argument is only used to be |
| 173 | included in the gzip file header, which may include the original |
| 174 | filename of the uncompressed file. It defaults to the filename of |
| 175 | fileobj, if discernible; otherwise, it defaults to the empty string, |
| 176 | and in this case the original filename is not included in the header. |
| 177 | |
| 178 | The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', 'wb', 'x', or |
| 179 | 'xb' depending on whether the file will be read or written. The default |
| 180 | is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
| 181 | A mode of 'r' is equivalent to one of 'rb', and similarly for 'w' and |
| 182 | 'wb', 'a' and 'ab', and 'x' and 'xb'. |
| 183 | |
| 184 | The compresslevel argument is an integer from 0 to 9 controlling the |
| 185 | level of compression; 1 is fastest and produces the least compression, |
| 186 | and 9 is slowest and produces the most compression. 0 is no compression |
| 187 | at all. The default is 9. |
| 188 | |
| 189 | The optional mtime argument is the timestamp requested by gzip. The time |
| 190 | is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970. |
| 191 | If mtime is omitted or None, the current time is used. Use mtime = 0 |
| 192 | to generate a compressed stream that does not depend on creation time. |
| 193 | |
| 194 | """ |
| 195 | |
| 196 | # Ensure attributes exist at __del__ |
| 197 | self.mode = None |
| 198 | self.fileobj = None |
| 199 | self._buffer = None |
| 200 | |
| 201 | if mode and ('t' in mode or 'U' in mode): |
| 202 | raise ValueError("Invalid mode: {!r}".format(mode)) |
| 203 | if mode and 'b' not in mode: |
| 204 | mode += 'b' |