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