API Reference¶
Core¶
- class zipwire.SyncRemoteZip(reader)[source]¶
Read files from a remote ZIP archive using synchronous HTTP range requests.
Usage:
with SyncRemoteZip(reader) as rz: for info in rz.infolist(): print(info.filename) data = rz.read("path/to/file.txt")
- Parameters:
reader (SyncReader)
- infolist()[source]¶
Return a list of RemoteZipInfo objects for all files in the archive.
- Return type:
- getinfo(name)[source]¶
Return the RemoteZipInfo for the given filename.
- Raises:
FileNotFoundInZip – If the name is not in the archive.
- Parameters:
name (str)
- Return type:
- read(name, *, max_file_size=None)[source]¶
Read and decompress a file from the archive.
- Parameters:
name (str | RemoteZipInfo) – Filename string or RemoteZipInfo object.
max_file_size (int | None) – Optional limit on uncompressed size in bytes. Raises
FileTooLargeif the entry’sfile_sizeexceeds this limit.
- Returns:
The decompressed file contents.
- Return type:
Warning
This method loads the entire decompressed file into memory. It checks the entry’s declared
file_sizeagainst max_file_size, but a crafted archive may lie about its uncompressed size. Useread_into()for defence in depth: it enforces the size limit during decompression.
- read_into(name, dest, *, max_file_size=None)[source]¶
Decompress a file from the archive into a writable destination.
Unlike
read(), this truly streams: the compressed payload is fetched in chunks viastream_range()and each chunk is decompressed and written immediately, keeping peak memory low.The streaming decompressor also enforces max_file_size during decompression, aborting if the actual output exceeds the limit regardless of what the entry metadata claims.
- Parameters:
name (str | RemoteZipInfo) – Filename string or RemoteZipInfo object.
dest (Writable) – A writable file-like object (e.g.
io.BytesIO, open file).max_file_size (int | None) – Optional limit on uncompressed size in bytes. Raises
FileTooLargeif the entry’sfile_sizeexceeds this limit, or if the actual decompressed output exceeds it.
- Return type:
None
- class zipwire.AsyncRemoteZip(reader)[source]¶
Read files from a remote ZIP archive using async HTTP range requests.
Usage:
async with AsyncRemoteZip(reader) as rz: for info in await rz.infolist(): print(info.filename) data = await rz.read("path/to/file.txt")
- Parameters:
reader (AsyncReader)
- async infolist()[source]¶
Return a list of RemoteZipInfo objects for all files in the archive.
- Return type:
- async getinfo(name)[source]¶
Return the RemoteZipInfo for the given filename.
- Raises:
FileNotFoundInZip – If the name is not in the archive.
- Parameters:
name (str)
- Return type:
- async read(name, *, max_file_size=None)[source]¶
Read and decompress a file from the archive.
- Parameters:
name (str | RemoteZipInfo) – Filename string or RemoteZipInfo object.
max_file_size (int | None) – Optional limit on uncompressed size in bytes. Raises
FileTooLargeif the entry’sfile_sizeexceeds this limit.
- Returns:
The decompressed file contents.
- Return type:
Warning
This method loads the entire decompressed file into memory. It checks the entry’s declared
file_sizeagainst max_file_size, but a crafted archive may lie about its uncompressed size. Useread_into()for defence in depth: it enforces the size limit during decompression.
- async read_into(name, dest, *, max_file_size=None)[source]¶
Decompress a file from the archive into a writable destination.
Unlike
read(), this truly streams: the compressed payload is fetched in chunks viastream_range()and each chunk is decompressed and written immediately, keeping peak memory low.The streaming decompressor also enforces max_file_size during decompression, aborting if the actual output exceeds the limit regardless of what the entry metadata claims.
- Parameters:
name (str | RemoteZipInfo) – Filename string or RemoteZipInfo object.
dest (Writable) – A writable file-like object (e.g.
io.BytesIO, open file).max_file_size (int | None) – Optional limit on uncompressed size in bytes. Raises
FileTooLargeif the entry’sfile_sizeexceeds this limit, or if the actual decompressed output exceeds it.
- Return type:
None
Protocols and Types¶
- class zipwire.SyncReader(*args, **kwargs)[source]¶
Protocol for synchronous HTTP range-request readers.
- read_range(offset, length)[source]¶
Read length bytes starting at absolute byte position offset.
Sends
Range: bytes=<offset>-<offset+length-1>.Returns the response body and the HTTP response headers.
- head()[source]¶
Send a HEAD request and return the response headers.
- Raises:
RangeRequestUnsupported – If the server does not advertise
Accept-Ranges: bytes.- Return type:
- class zipwire.AsyncReader(*args, **kwargs)[source]¶
Protocol for asynchronous HTTP range-request readers.
- async read_range(offset, length)[source]¶
Read length bytes starting at absolute byte position offset.
Sends
Range: bytes=<offset>-<offset+length-1>.Returns the response body and the HTTP response headers.
- async head()[source]¶
Send a HEAD request and return the response headers.
- Raises:
RangeRequestUnsupported – If the server does not advertise
Accept-Ranges: bytes.- Return type:
- class zipwire.Headers(*args, **kwargs)[source]¶
HTTP response headers returned by
head()andread_range().Implementations must support case-insensitive lookup or use lower-case keys so that
headers["content-length"]always works.All standard HTTP header mapping types satisfy this protocol, including
urllib3.HTTPHeaderDict,requests.structures.CaseInsensitiveDict,httpx2.Headers,aiohttp.CIMultiDictProxy, and plaindictwith lower-case keys.
Backends¶
- class zipwire.backends._urllib3.Urllib3Reader(url, *, pool=None, allow_redirects=True)[source]¶
SyncReader implementation using urllib3.PoolManager.
- Parameters:
url (str)
pool (urllib3.PoolManager | None)
allow_redirects (bool)
- class zipwire.backends._httpx2.Httpx2SyncReader(url, *, client=None, http2=None, allow_redirects=True)[source]¶
SyncReader implementation using httpx2.Client.
- class zipwire.backends._httpx2.Httpx2AsyncReader(url, *, client=None, http2=None, allow_redirects=True)[source]¶
AsyncReader implementation using httpx2.AsyncClient.
- class zipwire.backends._requests.RequestsReader(url, *, session=None, allow_redirects=True)[source]¶
SyncReader implementation using requests.Session.
- Parameters:
url (str)
session (requests.Session | None)
allow_redirects (bool)
- class zipwire.backends._aiohttp.AiohttpReader(url, *, session=None, allow_redirects=True)[source]¶
AsyncReader implementation using aiohttp.ClientSession.
- Parameters:
url (str)
session (aiohttp.ClientSession | None)
allow_redirects (bool)
Exceptions¶
Exception
└── ZipwireError
├── BadZipFile
├── UnsupportedCompression
├── CRCMismatch
├── FileTooLarge
├── RangeRequestUnsupported
└── FileNotFoundInZip (also inherits KeyError)
- exception zipwire.BadZipFile[source]¶
Bases:
ZipwireErrorThe data does not appear to be a valid ZIP archive.
- exception zipwire.UnsupportedCompression(method)[source]¶
Bases:
ZipwireErrorThe file uses an unsupported compression method.
- Parameters:
method (int)
- Return type:
None
- exception zipwire.CRCMismatch(expected, actual)[source]¶
Bases:
ZipwireErrorCRC-32 check failed after decompression.
- exception zipwire.FileTooLarge(filename, file_size, max_size)[source]¶
Bases:
ZipwireErrorThe entry’s uncompressed size exceeds the configured limit.
- exception zipwire.RangeRequestUnsupported[source]¶
Bases:
ZipwireErrorThe HTTP server does not support range requests.
- exception zipwire.FileNotFoundInZip(filename)[source]¶
Bases:
ZipwireError,KeyErrorThe requested file was not found in the ZIP archive.
- Parameters:
filename (str)
- Return type:
None