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