zdict
parameter and keyword argument support added in v3.3. Usepython3
.
Last active
March 21, 2024 17:24
-
-
Save esafwan/eca524aa042772f3dd8f691f6c6e5df1 to your computer and use it in GitHub Desktop.
Python zlib use custom dictionary(pre-defined zdict) example with and without unicode.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zlib | |
#Data for compression | |
hello = b'hello' | |
#Compression with dictionary | |
co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=hello) | |
compress_data = co.compress(hello) + co.flush() | |
#Compression without dictionary | |
co_nodict = zlib.compressobj(wbits=-zlib.MAX_WBITS, ) | |
compress_data_nodict = co_nodict.compress(hello) + co_nodict.flush() | |
#De-compression with dictionary | |
do = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=hello) | |
data = do.decompress(compress_data) | |
#print compressed output when dict used | |
print(compress_data) | |
print('\n') | |
#print compressed output when dict not used | |
print(compress_data_nodict) | |
print('\n') | |
#print decompressed output when dict used | |
print(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#UNICODE EXAMPLE | |
import zlib | |
#Data for compression | |
unicode_data = 'റെക്കോർഡ്' | |
hello = unicode_data.encode('utf-16be') | |
#Compression with dictionary | |
co = zlib.compressobj(wbits=-zlib.MAX_WBITS, zdict=hello) | |
compress_data = co.compress(hello) + co.flush() | |
#Compression without dictionary | |
co_nodict = zlib.compressobj(wbits=-zlib.MAX_WBITS, ) | |
compress_data_nodict = co_nodict.compress(hello) + co_nodict.flush() | |
#De-compression with dictionary | |
do = zlib.decompressobj(wbits=-zlib.MAX_WBITS, zdict=hello) | |
data = do.decompress(compress_data) | |
#print compressed output when dict used | |
print(compress_data) | |
print('\n') | |
#print compressed output when dict not used | |
print(compress_data_nodict) | |
print('\n') | |
#print decompressed output when dict used | |
print(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you. It's super useful.
Thanks to your example, we at SerpApi found out that
zlib
with a custom dictionary compressed data even more thanzstd
with a custom dictionary. (Checked with the small JSON for Sidekiq scheduled jobs.)