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. 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():
    # Any iterable that yields a zip file
    with httpx.stream('GET', 'https://www.example.com/my.zip') as r:
        yield from r.iter_bytes()

for file_name, file_size, unzipped_chunks in stream_unzip(zipped_chunks()):
    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.17.tar.gz (4.3 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.17-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: stream-unzip-0.0.17.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.5

File hashes

Hashes for stream-unzip-0.0.17.tar.gz
Algorithm Hash digest
SHA256 964206469fad2f3c2d0d8651af0ce3dcc5bdc27edd1c8acd4b5e759a9d2f4f6f
MD5 d0b78ada3b3220b2792e7a15055cfdcf
BLAKE2b-256 839197de02c5cb357483dee3f62765fe1a966dc5e4d1ba3a7e83080764bbfba5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: stream_unzip-0.0.17-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.5

File hashes

Hashes for stream_unzip-0.0.17-py3-none-any.whl
Algorithm Hash digest
SHA256 d738837c02012ac77fffcb29a3f55601073cf4eeb3baba66029ff72c09d0d503
MD5 506b79733ad7be7e6791f01c55c07621
BLAKE2b-256 ea172792b7d5203a17f670eb9816b00c650e462fc12a36a23dacf54e3c2f063b

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