Index: Lib/zipfile.py =================================================================== --- Lib/zipfile.py (revision 76872) +++ Lib/zipfile.py (working copy) @@ -3,6 +3,7 @@ """ import struct, os, time, sys, shutil import binascii, cStringIO, stat +import io try: import zlib # We may need its compression method @@ -451,7 +452,7 @@ self._UpdateKeys(c) return c -class ZipExtFile: +class ZipExtFile(io.BufferedIOBase): """File-like object for reading an archive member. Is returned by ZipFile.open(). """ @@ -462,117 +463,35 @@ self.bytes_read = 0L self.rawbuffer = '' self.readbuffer = '' - self.linebuffer = '' + self.offset = 0 self.eof = False - self.univ_newlines = False - self.nlSeps = ("\n", ) - self.lastdiscard = '' self.compress_type = zipinfo.compress_type self.compress_size = zipinfo.compress_size - self.closed = False self.mode = "r" self.name = zipinfo.filename - # read from compressed files in 64k blocks - self.compreadsize = 64*1024 + # read from compressed files in 4k blocks + self.compreadsize = 4*1024 if self.compress_type == ZIP_DEFLATED: self.dc = zlib.decompressobj(-15) - def set_univ_newlines(self, univ_newlines): - self.univ_newlines = univ_newlines + def peek(self, n): + """Returns buffered bytes without advancing the position.""" + if n > len(self.readbuffer) - self.offset: + chunk = self.read(n) + self.offset -= len(chunk) + + return self.readbuffer[self.offset:] - # pick line separator char(s) based on universal newlines flag - self.nlSeps = ("\n", ) - if self.univ_newlines: - self.nlSeps = ("\r\n", "\r", "\n") + def read1(self, n): + """Read up to n bytes with at most one read() system call.""" + return self.read(n) - def __iter__(self): - return self + def readable(self): + return True - def next(self): - nextline = self.readline() - if not nextline: - raise StopIteration() - - return nextline - - def close(self): - self.closed = True - - def _checkfornewline(self): - nl, nllen = -1, -1 - if self.linebuffer: - # ugly check for cases where half of an \r\n pair was - # read on the last pass, and the \r was discarded. In this - # case we just throw away the \n at the start of the buffer. - if (self.lastdiscard, self.linebuffer[0]) == ('\r','\n'): - self.linebuffer = self.linebuffer[1:] - - for sep in self.nlSeps: - nl = self.linebuffer.find(sep) - if nl >= 0: - nllen = len(sep) - return nl, nllen - - return nl, nllen - - def readline(self, size = -1): - """Read a line with approx. size. If size is negative, - read a whole line. - """ - if size < 0: - size = sys.maxint - elif size == 0: - return '' - - # check for a newline already in buffer - nl, nllen = self._checkfornewline() - - if nl >= 0: - # the next line was already in the buffer - nl = min(nl, size) - else: - # no line break in buffer - try to read more - size -= len(self.linebuffer) - while nl < 0 and size > 0: - buf = self.read(min(size, 100)) - if not buf: - break - self.linebuffer += buf - size -= len(buf) - - # check for a newline in buffer - nl, nllen = self._checkfornewline() - - # we either ran out of bytes in the file, or - # met the specified size limit without finding a newline, - # so return current buffer - if nl < 0: - s = self.linebuffer - self.linebuffer = '' - return s - - buf = self.linebuffer[:nl] - self.lastdiscard = self.linebuffer[nl:nl + nllen] - self.linebuffer = self.linebuffer[nl + nllen:] - - # line is always returned with \n as newline char (except possibly - # for a final incomplete line in the file, which is handled above). - return buf + "\n" - - def readlines(self, sizehint = -1): - """Return a list with all (following) lines. The sizehint parameter - is ignored in this implementation. - """ - result = [] - while True: - line = self.readline() - if not line: break - result.append(line) - return result - def read(self, size = None): # act like file() obj and return empty string if size is 0 if size == 0: @@ -588,10 +507,10 @@ if size is not None and size >= 0: if self.compress_type == ZIP_STORED: - lr = len(self.readbuffer) + lr = len(self.readbuffer) - self.offset bytesToRead = min(bytesToRead, size - lr) elif self.compress_type == ZIP_DEFLATED: - if len(self.readbuffer) > size: + if len(self.readbuffer) - self.offset > size: # the user has requested fewer bytes than we've already # pulled through the decompressor; don't read any more bytesToRead = 0 @@ -631,16 +550,17 @@ # prevent decompressor from being used again self.dc = None - self.readbuffer += newdata + self.readbuffer = self.readbuffer[self.offset:] + newdata + self.offset = 0 # return what the user asked for - if size is None or len(self.readbuffer) <= size: - bytes = self.readbuffer - self.readbuffer = '' + if size is None or len(self.readbuffer) - self.offset <= size: + bytes = self.readbuffer[self.offset:] + self.offset = len(self.readbuffer) else: - bytes = self.readbuffer[:size] - self.readbuffer = self.readbuffer[size:] + bytes = self.readbuffer[self.offset: self.offset + size] + self.offset += size return bytes @@ -918,9 +838,6 @@ else: zef = ZipExtFile(zef_file, zinfo, zd) - # set universal newlines on ZipExtFile if necessary - if "U" in mode: - zef.set_univ_newlines(True) return zef def extract(self, member, path=None, pwd=None): Index: Lib/test/test_zipfile.py =================================================================== --- Lib/test/test_zipfile.py (revision 76872) +++ Lib/test/test_zipfile.py (working copy) @@ -1176,7 +1176,7 @@ def test_main(): run_unittest(TestsWithSourceFile, TestZip64InSmallFiles, OtherTests, PyZipFileTests, DecryptionTests, TestsWithMultipleOpens, - TestWithDirectory, UniversalNewlineTests, + TestWithDirectory, TestsWithRandomBinaryFiles) if __name__ == "__main__":