1 """Functions that read and write gzipped files.
3 The user of the file doesn't have to worry about the compression,
4 but random access is not allowed."""
6 # based on Andrew Kuchling's minigzip.py distributed with the zlib module
8 import struct, sys, time, os
13 __all__ = ["GzipFile","open"]
15 FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
19 def write32u(output, value):
20 # The L format writes the bit pattern correctly whether signed
22 output.write(struct.pack("<L", value))
25 return struct.unpack("<I", input.read(4))[0]
27 def open(filename, mode="rb", compresslevel=9):
28 """Shorthand for GzipFile(filename, mode, compresslevel).
30 The filename argument is required; mode defaults to 'rb'
31 and compresslevel defaults to 9.
34 return GzipFile(filename, mode, compresslevel)
36 class GzipFile(io.BufferedIOBase):
37 """The GzipFile class simulates most of the methods of a file object with
38 the exception of the readinto() and truncate() methods.
43 max_read_chunk = 10 * 1024 * 1024 # 10Mb
45 def __init__(self, filename=None, mode=None,
46 compresslevel=9, fileobj=None, mtime=None):
47 """Constructor for the GzipFile class.
49 At least one of fileobj and filename must be given a
52 The new class instance is based on fileobj, which can be a regular
53 file, a StringIO object, or any other object which simulates a file.
54 It defaults to None, in which case filename is opened to provide
57 When fileobj is not None, the filename argument is only used to be
58 included in the gzip file header, which may includes the original
59 filename of the uncompressed file. It defaults to the filename of
60 fileobj, if discernible; otherwise, it defaults to the empty string,
61 and in this case the original filename is not included in the header.
63 The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb',
64 depending on whether the file will be read or written. The default
65 is the mode of fileobj if discernible; otherwise, the default is 'rb'.
66 Be aware that only the 'rb', 'ab', and 'wb' values should be used
67 for cross-platform portability.
69 The compresslevel argument is an integer from 1 to 9 controlling the
70 level of compression; 1 is fastest and produces the least compression,
71 and 9 is slowest and produces the most compression. The default is 9.
73 The mtime argument is an optional numeric timestamp to be written
74 to the stream when compressing. All gzip compressed streams
75 are required to contain a timestamp. If omitted or None, the
76 current time is used. This module ignores the timestamp when
77 decompressing; however, some programs, such as gunzip, make use
78 of it. The format of the timestamp is the same as that of the
79 return value of time.time() and of the st_mtime member of the
80 object returned by os.stat().
84 # guarantee the file is opened in binary mode on platforms
85 # that care about that sort of thing
86 if mode and 'b' not in mode:
89 fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
91 # Issue #13781: os.fdopen() creates a fileobj with a bogus name
92 # attribute. Avoid saving this in the gzip header's filename field.
93 if hasattr(fileobj, 'name') and fileobj.name != '<fdopen>':
94 filename = fileobj.name
98 if hasattr(fileobj, 'mode'): mode = fileobj.mode
103 # Set flag indicating start of a new member
104 self._new_member = True
105 # Buffer data read from gzip file. extrastart is offset in
106 # stream where buffer starts. extrasize is number of
107 # bytes remaining in buffer from current stream position.
112 # Starts small, scales exponentially
113 self.min_readsize = 100
115 elif mode[0:1] == 'w' or mode[0:1] == 'a':
117 self._init_write(filename)
118 self.compress = zlib.compressobj(compresslevel,
124 raise IOError, "Mode " + mode + " not supported"
126 self.fileobj = fileobj
130 if self.mode == WRITE:
131 self._write_gzip_header()
136 warnings.warn("use the name attribute", DeprecationWarning, 2)
137 if self.mode == WRITE and self.name[-3:] != ".gz":
138 return self.name + ".gz"
142 s = repr(self.fileobj)
143 return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
145 def _check_closed(self):
146 """Raises a ValueError if the underlying file object has been closed.
150 raise ValueError('I/O operation on closed file.')
152 def _init_write(self, filename):
154 self.crc = zlib.crc32("") & 0xffffffffL
159 def _write_gzip_header(self):
160 self.fileobj.write('\037\213') # magic header
161 self.fileobj.write('\010') # compression method
162 fname = os.path.basename(self.name)
163 if fname.endswith(".gz"):
168 self.fileobj.write(chr(flags))
172 write32u(self.fileobj, long(mtime))
173 self.fileobj.write('\002')
174 self.fileobj.write('\377')
176 self.fileobj.write(fname + '\000')
178 def _init_read(self):
179 self.crc = zlib.crc32("") & 0xffffffffL
182 def _read_gzip_header(self):
183 magic = self.fileobj.read(2)
184 if magic != '\037\213':
185 raise IOError, 'Not a gzipped file'
186 method = ord( self.fileobj.read(1) )
188 raise IOError, 'Unknown compression method'
189 flag = ord( self.fileobj.read(1) )
190 self.mtime = read32(self.fileobj)
191 # extraflag = self.fileobj.read(1)
192 # os = self.fileobj.read(1)
196 # Read & discard the extra field, if present
197 xlen = ord(self.fileobj.read(1))
198 xlen = xlen + 256*ord(self.fileobj.read(1))
199 self.fileobj.read(xlen)
201 # Read and discard a null-terminated string containing the filename
203 s = self.fileobj.read(1)
204 if not s or s=='\000':
207 # Read and discard a null-terminated string containing a comment
209 s = self.fileobj.read(1)
210 if not s or s=='\000':
213 self.fileobj.read(2) # Read & discard the 16-bit header CRC
215 def write(self,data):
217 if self.mode != WRITE:
219 raise IOError(errno.EBADF, "write() on read-only GzipFile object")
221 if self.fileobj is None:
222 raise ValueError, "write() on closed GzipFile object"
224 # Convert data type if called by io.BufferedWriter.
225 if isinstance(data, memoryview):
226 data = data.tobytes()
229 self.size = self.size + len(data)
230 self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
231 self.fileobj.write( self.compress.compress(data) )
232 self.offset += len(data)
236 def read(self, size=-1):
238 if self.mode != READ:
240 raise IOError(errno.EBADF, "read() on write-only GzipFile object")
242 if self.extrasize <= 0 and self.fileobj is None:
246 if size < 0: # get the whole thing
250 readsize = min(self.max_read_chunk, readsize * 2)
252 size = self.extrasize
253 else: # just get some more of it
255 while size > self.extrasize:
257 readsize = min(self.max_read_chunk, readsize * 2)
259 if size > self.extrasize:
260 size = self.extrasize
262 offset = self.offset - self.extrastart
263 chunk = self.extrabuf[offset: offset + size]
264 self.extrasize = self.extrasize - size
269 def _unread(self, buf):
270 self.extrasize = len(buf) + self.extrasize
271 self.offset -= len(buf)
273 def _read(self, size=1024):
274 if self.fileobj is None:
275 raise EOFError, "Reached EOF"
278 # If the _new_member flag is set, we have to
279 # jump to the next member, if there is one.
281 # First, check if we're at the end of the file;
282 # if so, it's time to stop; no more members to read.
283 pos = self.fileobj.tell() # Save current position
284 self.fileobj.seek(0, 2) # Seek to end of file
285 if pos == self.fileobj.tell():
286 raise EOFError, "Reached EOF"
288 self.fileobj.seek( pos ) # Return to original position
291 self._read_gzip_header()
292 self.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
293 self._new_member = False
295 # Read a chunk of data from the file
296 buf = self.fileobj.read(size)
298 # If the EOF has been reached, flush the decompression object
299 # and mark this object as finished.
302 uncompress = self.decompress.flush()
304 self._add_read_data( uncompress )
305 raise EOFError, 'Reached EOF'
307 uncompress = self.decompress.decompress(buf)
308 self._add_read_data( uncompress )
310 if self.decompress.unused_data != "":
311 # Ending case: we've come to the end of a member in the file,
312 # so seek back to the start of the unused data, finish up
313 # this member, and read a new gzip header.
314 # (The number of bytes to seek back is the length of the unused
315 # data, minus 8 because _read_eof() will rewind a further 8 bytes)
316 self.fileobj.seek( -len(self.decompress.unused_data)+8, 1)
318 # Check the CRC and file size, and set the flag so we read
319 # a new member on the next call
321 self._new_member = True
323 def _add_read_data(self, data):
324 self.crc = zlib.crc32(data, self.crc) & 0xffffffffL
325 offset = self.offset - self.extrastart
326 self.extrabuf = self.extrabuf[offset:] + data
327 self.extrasize = self.extrasize + len(data)
328 self.extrastart = self.offset
329 self.size = self.size + len(data)
332 # We've read to the end of the file, so we have to rewind in order
333 # to reread the 8 bytes containing the CRC and the file size.
334 # We check the that the computed CRC and size of the
335 # uncompressed data matches the stored values. Note that the size
336 # stored is the true file size mod 2**32.
337 self.fileobj.seek(-8, 1)
338 crc32 = read32(self.fileobj)
339 isize = read32(self.fileobj) # may exceed 2GB
340 if crc32 != self.crc:
341 raise IOError("CRC check failed %s != %s" % (hex(crc32),
343 elif isize != (self.size & 0xffffffffL):
344 raise IOError, "Incorrect length of data produced"
346 # Gzip files can be padded with zeroes and still have archives.
347 # Consume all zero bytes and set the file position to the first
348 # non-zero byte. See http://www.gzip.org/#faq8
351 c = self.fileobj.read(1)
353 self.fileobj.seek(-1, 1)
357 return self.fileobj is None
360 if self.fileobj is None:
362 if self.mode == WRITE:
363 self.fileobj.write(self.compress.flush())
364 write32u(self.fileobj, self.crc)
365 # self.size may exceed 2GB, or even 4GB
366 write32u(self.fileobj, self.size & 0xffffffffL)
368 elif self.mode == READ:
371 self.myfileobj.close()
372 self.myfileobj = None
374 def flush(self,zlib_mode=zlib.Z_SYNC_FLUSH):
376 if self.mode == WRITE:
377 # Ensure the compressor's buffer is flushed
378 self.fileobj.write(self.compress.flush(zlib_mode))
382 """Invoke the underlying file object's fileno() method.
384 This will raise AttributeError if the underlying file object
385 doesn't support fileno().
387 return self.fileobj.fileno()
390 '''Return the uncompressed stream file position indicator to the
391 beginning of the file'''
392 if self.mode != READ:
393 raise IOError("Can't rewind in write mode")
395 self._new_member = True
402 return self.mode == READ
405 return self.mode == WRITE
410 def seek(self, offset, whence=0):
413 offset = self.offset + offset
415 raise ValueError('Seek from end not supported')
416 if self.mode == WRITE:
417 if offset < self.offset:
418 raise IOError('Negative seek in write mode')
419 count = offset - self.offset
420 for i in range(count // 1024):
421 self.write(1024 * '\0')
422 self.write((count % 1024) * '\0')
423 elif self.mode == READ:
424 if offset < self.offset:
425 # for negative seek, rewind and do positive seek
427 count = offset - self.offset
428 for i in range(count // 1024):
430 self.read(count % 1024)
434 def readline(self, size=-1):
436 # Shortcut common case - newline found in buffer.
437 offset = self.offset - self.extrastart
438 i = self.extrabuf.find('\n', offset) + 1
440 self.extrasize -= i - offset
441 self.offset += i - offset
442 return self.extrabuf[offset: i]
445 readsize = self.min_readsize
450 c = self.read(readsize)
453 # We set i=size to break out of the loop under two
454 # conditions: 1) there's no newline, and the chunk is
455 # larger than size, or 2) there is a newline, but the
456 # resulting line would be longer than 'size'.
457 if (size <= i) or (i == -1 and len(c) > size):
460 if i >= 0 or c == '':
461 bufs.append(c[:i + 1]) # Add portion of last chunk
462 self._unread(c[i + 1:]) # Push back rest of chunk
465 # Append chunk to list, decrease 'size',
468 readsize = min(size, readsize * 2)
469 if readsize > self.min_readsize:
470 self.min_readsize = min(readsize, self.min_readsize * 2, 512)
471 return ''.join(bufs) # Return resulting line
475 # Act like gzip; with -d, act like gunzip.
476 # The input file is not deleted, however, nor are any other gzip
477 # options or features supported.
479 decompress = args and args[0] == "-d"
487 f = GzipFile(filename="", mode="rb", fileobj=sys.stdin)
490 if arg[-3:] != ".gz":
491 print "filename doesn't end in .gz:", repr(arg)
494 g = __builtin__.open(arg[:-3], "wb")
498 g = GzipFile(filename="", mode="wb", fileobj=sys.stdout)
500 f = __builtin__.open(arg, "rb")
501 g = open(arg + ".gz", "wb")
507 if g is not sys.stdout:
509 if f is not sys.stdin:
512 if __name__ == '__main__':