Python function to stream unzip all the files in a ZIP archive, without loading the entire ZIP file into memory or any of its uncompressed files
Project description
stream-unzip
Python function to stream unzip all the files in a ZIP archive, without loading the entire ZIP file into memory or any of its uncompressed files. Both AES and legacy (ZipCrypto/Zip 2.0) encrypted/password-protected ZIPs are supported.
While the ZIP format does have its main directory at the end, each compressed file in the archive is prefixed with a header that contains its name. Also, the Deflate algorithm that most ZIP files use indicates when it has reached the end of the stream of a member file. These facts make the streaming decompression of ZIP archives possible.
Installation
pip install stream-unzip
Usage
A single function is exposed, stream_unzip
, that takes a single argument: an iterable that should yield the bytes of a ZIP file [with no zero-length chunks]. It returns an iterable, where each yielded item is a tuple of the file name, file size [None
if this is not known], and another iterable itself yielding the unzipped bytes of that file.
from stream_unzip import stream_unzip
import httpx
def zipped_chunks():
# Iterable that yields the bytes of a zip file
with httpx.stream('GET', 'https://www.example.com/my.zip') as r:
yield from r.iter_bytes(chunk_size=65536)
for file_name, file_size, unzipped_chunks in stream_unzip(zipped_chunks(), password=b'my-password'):
for chunk in unzipped_chunks:
print(chunk)
The file name and file size are extracted as reported from the file. If you don't trust the creator of the ZIP file, these should be treated as untrusted input.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for stream_unzip-0.0.34-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41447daff2cc5ef51991f6a1662efc521cd3243a645b0b38ed197054edea7d60 |
|
MD5 | cb6e2f67cbe5c5aeea66c59ab2156ffc |
|
BLAKE2b-256 | bef85c301fdb259c31aba677cd876dc8360bf347ccc3c34ce44a4471dc1f1801 |