Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Functions that read and write gzipped files. |
| 2 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 3 | The user of the file doesn't have to worry about the compression, |
| 4 | but random access is not allowed.""" |
| 5 | |
| 6 | # based on Andrew Kuchling's minigzip.py distributed with the zlib module |
| 7 | |
Tim Peters | 49667c2 | 2004-07-27 21:05:21 +0000 | [diff] [blame] | 8 | import struct, sys, time |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 9 | import zlib |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 10 | import builtins |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 11 | |
Skip Montanaro | 2dd4276 | 2001-01-23 15:35:05 +0000 | [diff] [blame] | 12 | __all__ = ["GzipFile","open"] |
| 13 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 14 | FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 |
| 15 | |
| 16 | READ, WRITE = 1, 2 |
| 17 | |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 18 | def U32(i): |
| 19 | """Return i as an unsigned integer, assuming it fits in 32 bits. |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 20 | If it's >= 2GB when viewed as a 32-bit unsigned int, return a long. |
| 21 | """ |
| 22 | if i < 0: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 23 | i += 1 << 32 |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 24 | return i |
| 25 | |
Tim Peters | 9288f95 | 2002-11-05 20:38:55 +0000 | [diff] [blame] | 26 | def LOWU32(i): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 27 | """Return the low-order 32 bits, as a non-negative int""" |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 28 | return i & 0xFFFFFFFF |
Tim Peters | 9288f95 | 2002-11-05 20:38:55 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 95bdd0b | 1999-04-12 14:34:16 +0000 | [diff] [blame] | 30 | def write32u(output, value): |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 31 | # The L format writes the bit pattern correctly whether signed |
| 32 | # or unsigned. |
Guido van Rossum | 95bdd0b | 1999-04-12 14:34:16 +0000 | [diff] [blame] | 33 | output.write(struct.pack("<L", value)) |
| 34 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 35 | def read32(input): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 36 | return struct.unpack("<I", input.read(4))[0] |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 37 | |
Fred Drake | fa1591c | 1999-04-05 18:37:59 +0000 | [diff] [blame] | 38 | def open(filename, mode="rb", compresslevel=9): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 39 | """Shorthand for GzipFile(filename, mode, compresslevel). |
| 40 | |
| 41 | The filename argument is required; mode defaults to 'rb' |
| 42 | and compresslevel defaults to 9. |
| 43 | |
| 44 | """ |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 45 | return GzipFile(filename, mode, compresslevel) |
| 46 | |
| 47 | class GzipFile: |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 48 | """The GzipFile class simulates most of the methods of a file object with |
Guido van Rossum | 97c5fcc | 2002-08-06 17:03:25 +0000 | [diff] [blame] | 49 | the exception of the readinto() and truncate() methods. |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 50 | |
| 51 | """ |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 53 | myfileobj = None |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 54 | max_read_chunk = 10 * 1024 * 1024 # 10Mb |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 55 | |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 56 | def __init__(self, filename=None, mode=None, |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 57 | compresslevel=9, fileobj=None, mtime=None): |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 58 | """Constructor for the GzipFile class. |
| 59 | |
| 60 | At least one of fileobj and filename must be given a |
| 61 | non-trivial value. |
| 62 | |
| 63 | The new class instance is based on fileobj, which can be a regular |
| 64 | file, a StringIO object, or any other object which simulates a file. |
| 65 | It defaults to None, in which case filename is opened to provide |
| 66 | a file object. |
| 67 | |
| 68 | When fileobj is not None, the filename argument is only used to be |
| 69 | included in the gzip file header, which may includes the original |
| 70 | filename of the uncompressed file. It defaults to the filename of |
| 71 | fileobj, if discernible; otherwise, it defaults to the empty string, |
| 72 | and in this case the original filename is not included in the header. |
| 73 | |
| 74 | The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb', |
| 75 | depending on whether the file will be read or written. The default |
| 76 | is the mode of fileobj if discernible; otherwise, the default is 'rb'. |
| 77 | Be aware that only the 'rb', 'ab', and 'wb' values should be used |
| 78 | for cross-platform portability. |
| 79 | |
| 80 | The compresslevel argument is an integer from 1 to 9 controlling the |
| 81 | level of compression; 1 is fastest and produces the least compression, |
| 82 | and 9 is slowest and produces the most compression. The default is 9. |
| 83 | |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 84 | The mtime argument is an optional numeric timestamp to be written |
| 85 | to the stream when compressing. All gzip compressed streams |
| 86 | are required to contain a timestamp. If omitted or None, the |
| 87 | current time is used. This module ignores the timestamp when |
| 88 | decompressing; however, some programs, such as gunzip, make use |
| 89 | of it. The format of the timestamp is the same as that of the |
| 90 | return value of time.time() and of the st_mtime member of the |
| 91 | object returned by os.stat(). |
| 92 | |
Raymond Hettinger | aef22fb | 2002-05-29 16:18:42 +0000 | [diff] [blame] | 93 | """ |
| 94 | |
Skip Montanaro | 12424bc | 2002-05-23 01:43:05 +0000 | [diff] [blame] | 95 | # guarantee the file is opened in binary mode on platforms |
| 96 | # that care about that sort of thing |
| 97 | if mode and 'b' not in mode: |
| 98 | mode += 'b' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 99 | if fileobj is None: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 100 | fileobj = self.myfileobj = builtins.open(filename, mode or 'rb') |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 101 | if filename is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 102 | if hasattr(fileobj, 'name'): filename = fileobj.name |
| 103 | else: filename = '' |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 104 | if mode is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | if hasattr(fileobj, 'mode'): mode = fileobj.mode |
Fred Drake | 9bb76d1 | 1999-04-05 18:33:40 +0000 | [diff] [blame] | 106 | else: mode = 'rb' |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 108 | if mode[0:1] == 'r': |
| 109 | self.mode = READ |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 110 | # Set flag indicating start of a new member |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 111 | self._new_member = True |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 112 | self.extrabuf = b"" |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 113 | self.extrasize = 0 |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 114 | self.name = filename |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 115 | # Starts small, scales exponentially |
| 116 | self.min_readsize = 100 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 117 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 118 | elif mode[0:1] == 'w' or mode[0:1] == 'a': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 119 | self.mode = WRITE |
| 120 | self._init_write(filename) |
| 121 | self.compress = zlib.compressobj(compresslevel, |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 122 | zlib.DEFLATED, |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 123 | -zlib.MAX_WBITS, |
| 124 | zlib.DEF_MEM_LEVEL, |
| 125 | 0) |
| 126 | else: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 127 | raise IOError("Mode " + mode + " not supported") |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | self.fileobj = fileobj |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 130 | self.offset = 0 |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 131 | self.mtime = mtime |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | if self.mode == WRITE: |
| 134 | self._write_gzip_header() |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 135 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 136 | @property |
| 137 | def filename(self): |
| 138 | import warnings |
Philip Jenvey | a394f2d | 2009-05-08 03:57:12 +0000 | [diff] [blame] | 139 | warnings.warn("use the name attribute", DeprecationWarning, 2) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 140 | if self.mode == WRITE and self.name[-3:] != ".gz": |
| 141 | return self.name + ".gz" |
| 142 | return self.name |
| 143 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 144 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 145 | s = repr(self.fileobj) |
| 146 | return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>' |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 147 | |
| 148 | def _init_write(self, filename): |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 149 | self.name = filename |
Antoine Pitrou | c8428d3 | 2009-12-14 18:23:30 +0000 | [diff] [blame] | 150 | self.crc = zlib.crc32(b"") & 0xffffffff |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 151 | self.size = 0 |
| 152 | self.writebuf = [] |
| 153 | self.bufsize = 0 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 154 | |
| 155 | def _write_gzip_header(self): |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 156 | self.fileobj.write(b'\037\213') # magic header |
| 157 | self.fileobj.write(b'\010') # compression method |
Lars Gustäbel | 5590d8c | 2007-08-10 12:02:32 +0000 | [diff] [blame] | 158 | try: |
Lars Gustäbel | ead7056 | 2007-08-13 09:05:16 +0000 | [diff] [blame] | 159 | # RFC 1952 requires the FNAME field to be Latin-1. Do not |
| 160 | # include filenames that cannot be represented that way. |
| 161 | fname = self.name.encode('latin-1') |
| 162 | if fname.endswith(b'.gz'): |
| 163 | fname = fname[:-3] |
Lars Gustäbel | 5590d8c | 2007-08-10 12:02:32 +0000 | [diff] [blame] | 164 | except UnicodeEncodeError: |
Lars Gustäbel | ead7056 | 2007-08-13 09:05:16 +0000 | [diff] [blame] | 165 | fname = b'' |
| 166 | flags = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 167 | if fname: |
| 168 | flags = FNAME |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 169 | self.fileobj.write(chr(flags).encode('latin-1')) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 170 | mtime = self.mtime |
| 171 | if mtime is None: |
| 172 | mtime = time.time() |
| 173 | write32u(self.fileobj, int(mtime)) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 174 | self.fileobj.write(b'\002') |
| 175 | self.fileobj.write(b'\377') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 176 | if fname: |
Lars Gustäbel | 5590d8c | 2007-08-10 12:02:32 +0000 | [diff] [blame] | 177 | self.fileobj.write(fname + b'\000') |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 178 | |
| 179 | def _init_read(self): |
Antoine Pitrou | c8428d3 | 2009-12-14 18:23:30 +0000 | [diff] [blame] | 180 | self.crc = zlib.crc32(b"") & 0xffffffff |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 181 | self.size = 0 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 182 | |
| 183 | def _read_gzip_header(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 184 | magic = self.fileobj.read(2) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 185 | if magic != b'\037\213': |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 186 | raise IOError('Not a gzipped file') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 187 | method = ord( self.fileobj.read(1) ) |
| 188 | if method != 8: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 189 | raise IOError('Unknown compression method') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 190 | flag = ord( self.fileobj.read(1) ) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 191 | self.mtime = read32(self.fileobj) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 192 | # extraflag = self.fileobj.read(1) |
| 193 | # os = self.fileobj.read(1) |
Antoine Pitrou | 42db3ef | 2009-01-04 21:37:59 +0000 | [diff] [blame] | 194 | self.fileobj.read(2) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 196 | if flag & FEXTRA: |
| 197 | # Read & discard the extra field, if present |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 198 | xlen = ord(self.fileobj.read(1)) |
| 199 | xlen = xlen + 256*ord(self.fileobj.read(1)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 200 | self.fileobj.read(xlen) |
| 201 | if flag & FNAME: |
| 202 | # Read and discard a null-terminated string containing the filename |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 203 | while True: |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 204 | s = self.fileobj.read(1) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 205 | if not s or s==b'\000': |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 206 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 207 | if flag & FCOMMENT: |
| 208 | # Read and discard a null-terminated string containing a comment |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 209 | while True: |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 210 | s = self.fileobj.read(1) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 211 | if not s or s==b'\000': |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 212 | break |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 213 | if flag & FHCRC: |
| 214 | self.fileobj.read(2) # Read & discard the 16-bit header CRC |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | def write(self,data): |
Martin v. Löwis | db04489 | 2002-03-11 06:46:52 +0000 | [diff] [blame] | 218 | if self.mode != WRITE: |
| 219 | import errno |
| 220 | raise IOError(errno.EBADF, "write() on read-only GzipFile object") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 222 | if self.fileobj is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 223 | raise ValueError("write() on closed GzipFile object") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 224 | if len(data) > 0: |
| 225 | self.size = self.size + len(data) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 226 | self.crc = zlib.crc32(data, self.crc) & 0xffffffff |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 227 | self.fileobj.write( self.compress.compress(data) ) |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 228 | self.offset += len(data) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 5606801 | 2000-02-02 16:51:06 +0000 | [diff] [blame] | 230 | def read(self, size=-1): |
Martin v. Löwis | db04489 | 2002-03-11 06:46:52 +0000 | [diff] [blame] | 231 | if self.mode != READ: |
| 232 | import errno |
Brett Cannon | edfb302 | 2003-12-04 19:28:06 +0000 | [diff] [blame] | 233 | raise IOError(errno.EBADF, "read() on write-only GzipFile object") |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 235 | if self.extrasize <= 0 and self.fileobj is None: |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 236 | return b'' |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 237 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 238 | readsize = 1024 |
Guido van Rossum | 5606801 | 2000-02-02 16:51:06 +0000 | [diff] [blame] | 239 | if size < 0: # get the whole thing |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 240 | try: |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 241 | while True: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 242 | self._read(readsize) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 243 | readsize = min(self.max_read_chunk, readsize * 2) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 244 | except EOFError: |
| 245 | size = self.extrasize |
| 246 | else: # just get some more of it |
| 247 | try: |
| 248 | while size > self.extrasize: |
| 249 | self._read(readsize) |
Andrew M. Kuchling | 01cb47b | 2005-06-09 14:19:32 +0000 | [diff] [blame] | 250 | readsize = min(self.max_read_chunk, readsize * 2) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 251 | except EOFError: |
Guido van Rossum | 84c6fc9 | 1998-08-03 15:41:39 +0000 | [diff] [blame] | 252 | if size > self.extrasize: |
| 253 | size = self.extrasize |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 254 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 255 | chunk = self.extrabuf[:size] |
| 256 | self.extrabuf = self.extrabuf[size:] |
| 257 | self.extrasize = self.extrasize - size |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 258 | |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 259 | self.offset += size |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 260 | return chunk |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 262 | def _unread(self, buf): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 263 | self.extrabuf = buf + self.extrabuf |
Guido van Rossum | 84c6fc9 | 1998-08-03 15:41:39 +0000 | [diff] [blame] | 264 | self.extrasize = len(buf) + self.extrasize |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 265 | self.offset -= len(buf) |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 266 | |
| 267 | def _read(self, size=1024): |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 268 | if self.fileobj is None: |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 269 | raise EOFError("Reached EOF") |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 270 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 271 | if self._new_member: |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 272 | # If the _new_member flag is set, we have to |
| 273 | # jump to the next member, if there is one. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 274 | # |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 275 | # First, check if we're at the end of the file; |
| 276 | # if so, it's time to stop; no more members to read. |
| 277 | pos = self.fileobj.tell() # Save current position |
| 278 | self.fileobj.seek(0, 2) # Seek to end of file |
| 279 | if pos == self.fileobj.tell(): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 280 | raise EOFError("Reached EOF") |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 281 | else: |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 282 | self.fileobj.seek( pos ) # Return to original position |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 283 | |
| 284 | self._init_read() |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 285 | self._read_gzip_header() |
| 286 | self.decompress = zlib.decompressobj(-zlib.MAX_WBITS) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 287 | self._new_member = False |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 288 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 289 | # Read a chunk of data from the file |
| 290 | buf = self.fileobj.read(size) |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 291 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 292 | # If the EOF has been reached, flush the decompression object |
| 293 | # and mark this object as finished. |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 294 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 295 | if buf == b"": |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 296 | uncompress = self.decompress.flush() |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 297 | self._read_eof() |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 298 | self._add_read_data( uncompress ) |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 299 | raise EOFError('Reached EOF') |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 300 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 301 | uncompress = self.decompress.decompress(buf) |
| 302 | self._add_read_data( uncompress ) |
| 303 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 304 | if self.decompress.unused_data != b"": |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 305 | # Ending case: we've come to the end of a member in the file, |
| 306 | # so seek back to the start of the unused data, finish up |
| 307 | # this member, and read a new gzip header. |
| 308 | # (The number of bytes to seek back is the length of the unused |
| 309 | # data, minus 8 because _read_eof() will rewind a further 8 bytes) |
| 310 | self.fileobj.seek( -len(self.decompress.unused_data)+8, 1) |
| 311 | |
| 312 | # Check the CRC and file size, and set the flag so we read |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 313 | # a new member on the next call |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 314 | self._read_eof() |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 315 | self._new_member = True |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 316 | |
| 317 | def _add_read_data(self, data): |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 318 | self.crc = zlib.crc32(data, self.crc) & 0xffffffff |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 319 | self.extrabuf = self.extrabuf + data |
| 320 | self.extrasize = self.extrasize + len(data) |
| 321 | self.size = self.size + len(data) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 322 | |
| 323 | def _read_eof(self): |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 324 | # We've read to the end of the file, so we have to rewind in order |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 325 | # to reread the 8 bytes containing the CRC and the file size. |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 326 | # We check the that the computed CRC and size of the |
Tim Peters | 9288f95 | 2002-11-05 20:38:55 +0000 | [diff] [blame] | 327 | # uncompressed data matches the stored values. Note that the size |
| 328 | # stored is the true file size mod 2**32. |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 329 | self.fileobj.seek(-8, 1) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 330 | crc32 = read32(self.fileobj) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 331 | isize = read32(self.fileobj) # may exceed 2GB |
| 332 | if crc32 != self.crc: |
| 333 | raise IOError("CRC check failed %s != %s" % (hex(crc32), |
| 334 | hex(self.crc))) |
Christian Heimes | 1dc5400 | 2008-03-24 02:19:29 +0000 | [diff] [blame] | 335 | elif isize != (self.size & 0xffffffff): |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 336 | raise IOError("Incorrect length of data produced") |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 337 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 338 | def close(self): |
Georg Brandl | b533e26 | 2008-05-25 18:19:30 +0000 | [diff] [blame] | 339 | if self.fileobj is None: |
| 340 | return |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 341 | if self.mode == WRITE: |
| 342 | self.fileobj.write(self.compress.flush()) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 343 | write32u(self.fileobj, self.crc) |
Tim Peters | 9288f95 | 2002-11-05 20:38:55 +0000 | [diff] [blame] | 344 | # self.size may exceed 2GB, or even 4GB |
Christian Heimes | 1dc5400 | 2008-03-24 02:19:29 +0000 | [diff] [blame] | 345 | write32u(self.fileobj, self.size & 0xffffffff) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 346 | self.fileobj = None |
| 347 | elif self.mode == READ: |
| 348 | self.fileobj = None |
| 349 | if self.myfileobj: |
| 350 | self.myfileobj.close() |
| 351 | self.myfileobj = None |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 352 | |
Andrew M. Kuchling | 916fcc3 | 1999-08-10 13:19:30 +0000 | [diff] [blame] | 353 | def __del__(self): |
Jeremy Hylton | e298c30 | 2000-05-08 16:59:59 +0000 | [diff] [blame] | 354 | try: |
| 355 | if (self.myfileobj is None and |
| 356 | self.fileobj is None): |
| 357 | return |
| 358 | except AttributeError: |
| 359 | return |
| 360 | self.close() |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 361 | |
Martin v. Löwis | f2a8d63 | 2005-03-03 08:35:22 +0000 | [diff] [blame] | 362 | def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH): |
| 363 | if self.mode == WRITE: |
Tim Peters | eba28be | 2005-03-28 01:08:02 +0000 | [diff] [blame] | 364 | # Ensure the compressor's buffer is flushed |
| 365 | self.fileobj.write(self.compress.flush(zlib_mode)) |
Mark Dickinson | 751125d | 2010-05-04 18:47:50 +0000 | [diff] [blame] | 366 | self.fileobj.flush() |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 367 | |
Tim Peters | 5cfb05e | 2004-07-27 21:02:02 +0000 | [diff] [blame] | 368 | def fileno(self): |
| 369 | """Invoke the underlying file object's fileno() method. |
| 370 | |
| 371 | This will raise AttributeError if the underlying file object |
| 372 | doesn't support fileno(). |
| 373 | """ |
| 374 | return self.fileobj.fileno() |
| 375 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 376 | def isatty(self): |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 377 | return False |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 378 | |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 379 | def tell(self): |
| 380 | return self.offset |
| 381 | |
| 382 | def rewind(self): |
| 383 | '''Return the uncompressed stream file position indicator to the |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 384 | beginning of the file''' |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 385 | if self.mode != READ: |
| 386 | raise IOError("Can't rewind in write mode") |
| 387 | self.fileobj.seek(0) |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 388 | self._new_member = True |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 389 | self.extrabuf = b"" |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 390 | self.extrasize = 0 |
| 391 | self.offset = 0 |
| 392 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 393 | def seek(self, offset, whence=0): |
| 394 | if whence: |
| 395 | if whence == 1: |
| 396 | offset = self.offset + offset |
| 397 | else: |
| 398 | raise ValueError('Seek from end not supported') |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 399 | if self.mode == WRITE: |
| 400 | if offset < self.offset: |
| 401 | raise IOError('Negative seek in write mode') |
| 402 | count = offset - self.offset |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 403 | chunk = bytes(1024) |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 404 | for i in range(count // 1024): |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 405 | self.write(chunk) |
| 406 | self.write(bytes(count % 1024)) |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 407 | elif self.mode == READ: |
| 408 | if offset < self.offset: |
| 409 | # for negative seek, rewind and do positive seek |
| 410 | self.rewind() |
| 411 | count = offset - self.offset |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 412 | for i in range(count // 1024): |
| 413 | self.read(1024) |
Martin v. Löwis | 8cc965c | 2001-08-09 07:21:56 +0000 | [diff] [blame] | 414 | self.read(count % 1024) |
| 415 | |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 416 | def readline(self, size=-1): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 417 | if size < 0: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 418 | size = sys.maxsize |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 419 | readsize = self.min_readsize |
| 420 | else: |
| 421 | readsize = size |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 422 | bufs = [] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 423 | while size != 0: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 424 | c = self.read(readsize) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 425 | i = c.find(b'\n') |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 426 | |
| 427 | # We set i=size to break out of the loop under two |
| 428 | # conditions: 1) there's no newline, and the chunk is |
| 429 | # larger than size, or 2) there is a newline, but the |
| 430 | # resulting line would be longer than 'size'. |
| 431 | if (size <= i) or (i == -1 and len(c) > size): |
| 432 | i = size - 1 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 433 | |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 434 | if i >= 0 or c == b'': |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 435 | bufs.append(c[:i + 1]) # Add portion of last chunk |
| 436 | self._unread(c[i + 1:]) # Push back rest of chunk |
| 437 | break |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 438 | |
| 439 | # Append chunk to list, decrease 'size', |
| 440 | bufs.append(c) |
| 441 | size = size - len(c) |
| 442 | readsize = min(size, readsize * 2) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 443 | if readsize > self.min_readsize: |
| 444 | self.min_readsize = min(readsize, self.min_readsize * 2, 512) |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 445 | return b''.join(bufs) # Return resulting line |
Tim Peters | 07e99cb | 2001-01-14 23:47:14 +0000 | [diff] [blame] | 446 | |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 447 | def readlines(self, sizehint=0): |
| 448 | # Negative numbers result in reading all the lines |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 449 | if sizehint <= 0: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 450 | sizehint = sys.maxsize |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 451 | L = [] |
| 452 | while sizehint > 0: |
| 453 | line = self.readline() |
Walter Dörwald | 5b1284d | 2007-06-06 16:43:59 +0000 | [diff] [blame] | 454 | if line == b"": |
Tim Peters | fb0ea52 | 2002-11-04 19:50:11 +0000 | [diff] [blame] | 455 | break |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 456 | L.append(line) |
Andrew M. Kuchling | 41616ee | 2000-07-29 20:15:26 +0000 | [diff] [blame] | 457 | sizehint = sizehint - len(line) |
| 458 | |
| 459 | return L |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 460 | |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 461 | def writelines(self, L): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 462 | for line in L: |
| 463 | self.write(line) |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 464 | |
Neil Schemenauer | cacbdf6 | 2002-03-20 18:36:00 +0000 | [diff] [blame] | 465 | def __iter__(self): |
| 466 | return self |
| 467 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 468 | def __next__(self): |
Neil Schemenauer | cacbdf6 | 2002-03-20 18:36:00 +0000 | [diff] [blame] | 469 | line = self.readline() |
| 470 | if line: |
| 471 | return line |
| 472 | else: |
| 473 | raise StopIteration |
| 474 | |
Antoine Pitrou | 308705e | 2009-01-10 16:22:51 +0000 | [diff] [blame] | 475 | def __enter__(self): |
| 476 | if self.fileobj is None: |
| 477 | raise ValueError("I/O operation on closed GzipFile object") |
| 478 | return self |
| 479 | |
| 480 | def __exit__(self, *args): |
| 481 | self.close() |
| 482 | |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 483 | |
| 484 | def _test(): |
| 485 | # Act like gzip; with -d, act like gunzip. |
| 486 | # The input file is not deleted, however, nor are any other gzip |
| 487 | # options or features supported. |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 488 | args = sys.argv[1:] |
| 489 | decompress = args and args[0] == "-d" |
| 490 | if decompress: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 491 | args = args[1:] |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 492 | if not args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 493 | args = ["-"] |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 494 | for arg in args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 495 | if decompress: |
| 496 | if arg == "-": |
Antoine Pitrou | 9d625c2 | 2009-01-04 21:11:10 +0000 | [diff] [blame] | 497 | f = GzipFile(filename="", mode="rb", fileobj=sys.stdin.buffer) |
| 498 | g = sys.stdout.buffer |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 499 | else: |
| 500 | if arg[-3:] != ".gz": |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 501 | print("filename doesn't end in .gz:", repr(arg)) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 502 | continue |
| 503 | f = open(arg, "rb") |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 504 | g = builtins.open(arg[:-3], "wb") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 505 | else: |
| 506 | if arg == "-": |
Antoine Pitrou | 9d625c2 | 2009-01-04 21:11:10 +0000 | [diff] [blame] | 507 | f = sys.stdin.buffer |
| 508 | g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 509 | else: |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 510 | f = builtins.open(arg, "rb") |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 511 | g = open(arg + ".gz", "wb") |
Guido van Rossum | 8ca162f | 2002-04-07 06:36:23 +0000 | [diff] [blame] | 512 | while True: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 513 | chunk = f.read(1024) |
| 514 | if not chunk: |
| 515 | break |
| 516 | g.write(chunk) |
| 517 | if g is not sys.stdout: |
| 518 | g.close() |
| 519 | if f is not sys.stdin: |
| 520 | f.close() |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 521 | |
| 522 | if __name__ == '__main__': |
| 523 | _test() |