Skip to content

gh-88339: enable fast seeking of uncompressed unencrypted zipfile.ZipExtFile #27737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,6 +2030,7 @@ def test_seek_tell(self):
fp.seek(bloc, os.SEEK_CUR)
self.assertEqual(fp.tell(), bloc)
self.assertEqual(fp.read(5), txt[bloc:bloc+5])
self.assertEqual(fp.tell(), bloc + 5)
fp.seek(0, os.SEEK_END)
self.assertEqual(fp.tell(), len(txt))
fp.seek(0, os.SEEK_SET)
Expand All @@ -2047,6 +2048,7 @@ def test_seek_tell(self):
fp.seek(bloc, os.SEEK_CUR)
self.assertEqual(fp.tell(), bloc)
self.assertEqual(fp.read(5), txt[bloc:bloc+5])
self.assertEqual(fp.tell(), bloc + 5)
fp.seek(0, os.SEEK_END)
self.assertEqual(fp.tell(), len(txt))
fp.seek(0, os.SEEK_SET)
Expand Down
24 changes: 19 additions & 5 deletions Lib/zipfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ def __init__(self, fileobj, mode, zipinfo, pwd=None,
self._orig_compress_size = zipinfo.compress_size
self._orig_file_size = zipinfo.file_size
self._orig_start_crc = self._running_crc
self._orig_crc = self._expected_crc
self._seekable = True
except AttributeError:
pass
Expand Down Expand Up @@ -1069,17 +1070,17 @@ def seekable(self):
raise ValueError("I/O operation on closed file.")
return self._seekable

def seek(self, offset, whence=0):
def seek(self, offset, whence=os.SEEK_SET):
if self.closed:
raise ValueError("seek on closed file.")
if not self._seekable:
raise io.UnsupportedOperation("underlying stream is not seekable")
curr_pos = self.tell()
if whence == 0: # Seek from start of file
if whence == os.SEEK_SET:
new_pos = offset
elif whence == 1: # Seek from current position
elif whence == os.SEEK_CUR:
new_pos = curr_pos + offset
elif whence == 2: # Seek from EOF
elif whence == os.SEEK_END:
new_pos = self._orig_file_size + offset
else:
raise ValueError("whence must be os.SEEK_SET (0), "
Expand All @@ -1094,14 +1095,27 @@ def seek(self, offset, whence=0):
read_offset = new_pos - curr_pos
buff_offset = read_offset + self._offset

if buff_offset >= 0 and buff_offset < len(self._readbuffer):
# Fast seek uncompressed unencrypted file
if self._compress_type == ZIP_STORED and self._decrypter is None and read_offset > 0:
# disable CRC checking after first seeking - it would be invalid
self._expected_crc = None
# seek actual file taking already buffered data into account
read_offset -= len(self._readbuffer) - self._offset
self._fileobj.seek(read_offset, os.SEEK_CUR)
self._left -= read_offset
read_offset = 0
# flush read buffer
self._readbuffer = b''
self._offset = 0
elif buff_offset >= 0 and buff_offset < len(self._readbuffer):
# Just move the _offset index if the new position is in the _readbuffer
self._offset = buff_offset
read_offset = 0
elif read_offset < 0:
# Position is before the current position. Reset the ZipExtFile
self._fileobj.seek(self._orig_compress_start)
self._running_crc = self._orig_start_crc
self._expected_crc = self._orig_crc
self._compress_left = self._orig_compress_size
self._left = self._orig_file_size
self._readbuffer = b''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable fast seeking of uncompressed unencrypted :class:`zipfile.ZipExtFile`