Skip to content

random access uncompressed unencrypted ZipExtFile  #128131

Closed

Description

@vvb2060

Feature or enhancement

Proposal:

# Fast seek uncompressed unencrypted file
elif 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 read_offset < 0:
# Position is before the current position. Reset the ZipExtFile

if read_offset < 0, ZipExtFile is reset and read from the beginning. I think read_offset > 0 is unnecessary.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Dec 20, 2024
  2. danifus commented on Dec 22, 2024

    @danifus
    Contributor

    There is a bug in zipfile._SharedFile.seek() that affects concurrent reads of uncompressed, unencrypted files which should be merged along with this PR: #127856

  3. jaraco commented on Dec 26, 2024

    @jaraco
    Member

    Can you revise the original post to clarify what’s going on here? I read it, but I don’t understand: what is wrong with the current behavior? Under what conditions do the problems occur and thus who is affected? What do you expect instead? Just articulate as much as you can so it’s clear from the problem description what the proposed improvement is.

  4. vvb2060 commented on Dec 31, 2024

    @vvb2060
    Author

    #27737 does not really support random access, currently zipfile can only read uncompressed unencrypted ZipExtFile sequentially (only forward seek).

  5. jaraco commented on Jan 20, 2025

    @jaraco
    Member

    Thanks vvb2060. Let me re-articulate what I think you're saying.

    In #27737, the zipfile module introduced the possibility of seeking within a zip file, but only for compressed or encrypted payloads. Today, if someone attempts to seek with an offset that's 0 or negative, the wrong logic is reached, and an error occurs. Instead, it should be straightforward to enable seeking in any direction (any value of read_offset) consistently (for unencrypted, uncompressed files).

  6. added a commit that references this issue on Jan 20, 2025
  7. added 2 commits that reference this issue on Jan 20, 2025
  8. added a commit that references this issue on Jan 20, 2025
  9. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-featureA feature request or enhancement
    on Jan 20, 2025
  10. picnixz commented on Jan 20, 2025

    @picnixz
    Member

    See #128143 (comment) for the rationale of backports and the type-bug label.

  11. added a commit that references this issue on Jan 20, 2025
  12. picnixz commented on Jan 20, 2025

    @picnixz
    Member

    Closing since completed and backported. Thank you everyone.

  13. added a commit that references this issue on Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions