Low-level interface to the zlib library that enables capturing the decoding state
Project description
zlib-state
Low-level interface to the zlib library that enables capturing the decoding state.
Install
From PyPi:
pip install zlib-state
From source:
pip install .
Tested on Ubuntu/macOs/Windows with Python 3.7-3.12.
GzipStateFile
Wraps Decompressor as a buffered reader.
Based on my benchmarking, this is somewhat slower than python's gzip.
A typical usage pattern looks like:
import zlib_state
TARGET_LINE = 5000 # pick back up after around the 5,000th line
# Specify keep_last_state=True to tell object to grab and keep the state and pos after each block
with zlib_state.GzipStateFile('testdata/frankenstein.txt.gz', keep_last_state=True) as f:
for i, line in enumerate(f):
if i == TARGET_LINE:
state, pos = f.last_state, f.last_state_pos
with zlib_state.GzipStateFile('testdata/frankenstein.txt.gz') as f:
f.zseek(pos, state)
remainder = f.read()
Decompressor
Very basic decompression object that's picky and unforgiving.
Based on my benchmarking, this can iterate over gzip files faster than python's gzip.
A typical usage pattern looks like:
import zlib_state
decomp = zlib_state.Decompressor(32 + 15) # from zlib; 32 indicates gzip header, 15 window size
block_count = 0
with open('testdata/frankenstein.txt.gz', 'rb') as f:
while not decomp.eof():
needed_input = decomp.needs_input()
if needed_input > 0:
# decomp needs more input, and it tells you how much.
decomp.feed_input(f.read(needed_input))
# next_chunk may be empty (e.g., if finished with gzip headers) or may contain data.
# It sends as much as it has left in its output buffer, or asks zlib to continue.
next_chunk = decomp.read() # you can also pass a maximum size to take and/or a buffer to write to
if decomp.block_boundary():
block_count += 1
# When it reaches the end of a deflate block, it always stops. At these times, you can grab the state
# if you wish.
if block_count == 4: # resume after the 4th block
state = decomp.get_state() # includes zdict, bits, byte -- everything it needs to resume from pos
pos = decomp.total_in() # the current position in the binary file to resume from
print(f'{block_count} blocks processed')
# resume from somewhere in the file. Only possible spots are the block boundaries, given the state
f.seek(pos)
decomp = zlib_state.Decompressor(-15) # from zlib; 15 window size, negative means no headers
decomp.set_state(*state)
while not decomp.eof():
needed_input = decomp.needs_input()
if needed_input > 0:
# decomp needs more input, and it tells you how much.
decomp.feed_input(f.read(needed_input))
next_chunk = decomp.read()
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
zlib_state-0.1.7.tar.gz
(9.5 kB
view hashes)
Built Distributions
Close
Hashes for zlib_state-0.1.7-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e033569981d36cda15d3aa1bc68f421a87b7a8bd2d357ee849baa0b58564d416 |
|
MD5 | 092dc363a3d3a8f11307c9d7166360d4 |
|
BLAKE2b-256 | 628cbb53ccec930022b62d9cc29853598f3bc82ad319ff261262d13a9bb3329f |
Close
Hashes for zlib_state-0.1.7-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9af76794076e75ccd07ca844963b4478230536ecf0ae412946fc030bed498c80 |
|
MD5 | 634d55f460d31983e45f8b9780c55d18 |
|
BLAKE2b-256 | 3140e23c4ddc4b3bc13316231afc852bb08f6f76ea72ba5447b7663d05b66de8 |
Close
Hashes for zlib_state-0.1.7-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a413087419ed0bfeea5a62baf35d9a68008b1020222a0052a422ee1e6d55a8d1 |
|
MD5 | 45d1fe469d8332ad89811083e0e4a29f |
|
BLAKE2b-256 | 7b0ae43ee576700cc923bd963813f74b76064b081a569020b4eb6d4eaea61795 |
Close
Hashes for zlib_state-0.1.7-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6feb65a79b5c273a6ca6bec5c7c334db68108d9ad5c45ed4d6c1158ab8cc484 |
|
MD5 | 1393ed8360e753296f09c54070165019 |
|
BLAKE2b-256 | bf7456a575e4cb91cc18d5e970f5beb88615208c471fcc1126e0bc10a83c4f0c |
Close
Hashes for zlib_state-0.1.7-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3dc435fc1144e28d20c766c8be11c0e66e8aefc6f82e2313adcb3e7207827b7 |
|
MD5 | 48408a65181a69495c07e5392262d9bf |
|
BLAKE2b-256 | 73db1c8d132f29197c8ed2983df5432a25fe5e266ee2ccb8196bd6100504c49a |