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