Skip to content

Commit 03bce18

Browse files
miss-islington5ec1cffblurb-it[bot]picnixz
authored
[3.12] GH-128131: Completely support random read access of uncompressed unencrypted files in ZipFile (GH-128143) (#129092)
GH-128131: Completely support random read access of uncompressed unencrypted files in ZipFile (GH-128143) (cherry picked from commit dda02eb) Co-authored-by: 5ec1cff <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 728c6b8 commit 03bce18

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

Lib/test/test_zipfile/test_core.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _pyio
12
import array
23
import contextlib
34
import importlib.util
@@ -3440,5 +3441,87 @@ def test_too_short(self):
34403441
b"zzz", zipfile._strip_extra(b"zzz", (self.ZIP64_EXTRA,)))
34413442

34423443

3444+
class StatIO(_pyio.BytesIO):
3445+
"""Buffer which remembers the number of bytes that were read."""
3446+
3447+
def __init__(self):
3448+
super().__init__()
3449+
self.bytes_read = 0
3450+
3451+
def read(self, size=-1):
3452+
bs = super().read(size)
3453+
self.bytes_read += len(bs)
3454+
return bs
3455+
3456+
3457+
class StoredZipExtFileRandomReadTest(unittest.TestCase):
3458+
"""Tests whether an uncompressed, unencrypted zip entry can be randomly
3459+
seek and read without reading redundant bytes."""
3460+
def test_stored_seek_and_read(self):
3461+
3462+
sio = StatIO()
3463+
# 20000 bytes
3464+
txt = b'0123456789' * 2000
3465+
3466+
# The seek length must be greater than ZipExtFile.MIN_READ_SIZE
3467+
# as `ZipExtFile._read2()` reads in blocks of this size and we
3468+
# need to seek out of the buffered data
3469+
read_buffer_size = zipfile.ZipExtFile.MIN_READ_SIZE
3470+
self.assertGreaterEqual(10002, read_buffer_size) # for forward seek test
3471+
self.assertGreaterEqual(5003, read_buffer_size) # for backward seek test
3472+
# The read length must be less than MIN_READ_SIZE, since we assume that
3473+
# only 1 block is read in the test.
3474+
read_length = 100
3475+
self.assertGreaterEqual(read_buffer_size, read_length) # for read() calls
3476+
3477+
with zipfile.ZipFile(sio, "w", compression=zipfile.ZIP_STORED) as zipf:
3478+
zipf.writestr("foo.txt", txt)
3479+
3480+
# check random seek and read on a file
3481+
with zipfile.ZipFile(sio, "r") as zipf:
3482+
with zipf.open("foo.txt", "r") as fp:
3483+
# Test this optimized read hasn't rewound and read from the
3484+
# start of the file (as in the case of the unoptimized path)
3485+
3486+
# forward seek
3487+
old_count = sio.bytes_read
3488+
forward_seek_len = 10002
3489+
current_pos = 0
3490+
fp.seek(forward_seek_len, os.SEEK_CUR)
3491+
current_pos += forward_seek_len
3492+
self.assertEqual(fp.tell(), current_pos)
3493+
self.assertEqual(fp._left, fp._compress_left)
3494+
arr = fp.read(read_length)
3495+
current_pos += read_length
3496+
self.assertEqual(fp.tell(), current_pos)
3497+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3498+
self.assertEqual(fp._left, fp._compress_left)
3499+
read_count = sio.bytes_read - old_count
3500+
self.assertLessEqual(read_count, read_buffer_size)
3501+
3502+
# backward seek
3503+
old_count = sio.bytes_read
3504+
backward_seek_len = 5003
3505+
fp.seek(-backward_seek_len, os.SEEK_CUR)
3506+
current_pos -= backward_seek_len
3507+
self.assertEqual(fp.tell(), current_pos)
3508+
self.assertEqual(fp._left, fp._compress_left)
3509+
arr = fp.read(read_length)
3510+
current_pos += read_length
3511+
self.assertEqual(fp.tell(), current_pos)
3512+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3513+
self.assertEqual(fp._left, fp._compress_left)
3514+
read_count = sio.bytes_read - old_count
3515+
self.assertLessEqual(read_count, read_buffer_size)
3516+
3517+
# eof flags test
3518+
fp.seek(0, os.SEEK_END)
3519+
fp.seek(12345, os.SEEK_SET)
3520+
current_pos = 12345
3521+
arr = fp.read(read_length)
3522+
current_pos += read_length
3523+
self.assertEqual(arr, txt[current_pos - read_length:current_pos])
3524+
3525+
34433526
if __name__ == "__main__":
34443527
unittest.main()

Lib/zipfile/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,15 @@ def seek(self, offset, whence=os.SEEK_SET):
11401140
self._offset = buff_offset
11411141
read_offset = 0
11421142
# Fast seek uncompressed unencrypted file
1143-
elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset > 0:
1143+
elif self._compress_type == ZIP_STORED and self._decrypter is None and read_offset != 0:
11441144
# disable CRC checking after first seeking - it would be invalid
11451145
self._expected_crc = None
11461146
# seek actual file taking already buffered data into account
11471147
read_offset -= len(self._readbuffer) - self._offset
11481148
self._fileobj.seek(read_offset, os.SEEK_CUR)
11491149
self._left -= read_offset
1150+
self._compress_left -= read_offset
1151+
self._eof = self._left <= 0
11501152
read_offset = 0
11511153
# flush read buffer
11521154
self._readbuffer = b''
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Completely support random access of uncompressed unencrypted read-only
2+
zip files obtained by :meth:`ZipFile.open <zipfile.ZipFile.open>`.

0 commit comments

Comments
 (0)