Skip to main content

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 CircleCI Test Coverage

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.

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

stream-unzip-0.0.29.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stream_unzip-0.0.29-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file stream-unzip-0.0.29.tar.gz.

File metadata

  • Download URL: stream-unzip-0.0.29.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for stream-unzip-0.0.29.tar.gz
Algorithm Hash digest
SHA256 8fe985713fe250aa72a5e5e296bc72cd2cceb71cd0285390f690d81016d7d2d3
MD5 e69a52de90bb180020db9a3c990906e9
BLAKE2b-256 2673bb7ad84377d3d79bb722df27b518a9196a6b530f4c039494ad089be3b030

See more details on using hashes here.

File details

Details for the file stream_unzip-0.0.29-py3-none-any.whl.

File metadata

  • Download URL: stream_unzip-0.0.29-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for stream_unzip-0.0.29-py3-none-any.whl
Algorithm Hash digest
SHA256 fe55c1d891f8bc7caf833ee5c13163ab9a8d1b14b9e1ba62746db1cdf9880010
MD5 572011e20bcd64c074ade750e50a7dc1
BLAKE2b-256 30e1307e8cf45671294dd4dabf920f53cf637b1688f0140df9df9d3fa2ff6942

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page