Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
test compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Halbronn committed Mar 6, 2020
1 parent 51e7c04 commit d4e823a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions idarling/test/test_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# https://docs.python.org/3/library/bz2.html#examples-of-usage
# https://www.programcreek.com/python/example/6089/bz2.BZ2Decompressor

import bz2, hexdump

original_data = b'abc'*1000

def gen_data(data, chunk_size=1000):
"""Yield incremental blocks of chunksize bytes."""
len_data = len(data)
total_count_sent = 0
for i in range(int(len_data/chunk_size)):
yield data[chunk_size*i: chunk_size*(i+1)]
if len_data % chunk_size != 0:
yield data[-(len_data%chunk_size):]
else:
yield b''

comp = bz2.BZ2Compressor()
out = b""
for chunk in gen_data(original_data):
out = out + comp.compress(chunk)

# Finish the compression process. Call this once you have
# finished providing data to the compressor.
out = out + comp.flush()

hexdump.hexdump(out)
print("-"*30)


decomp = bz2.BZ2Decompressor()
data_decompressed = b''
for chunk in gen_data(out):
data_decompressed += decomp.decompress(chunk)

print(len(data_decompressed))
hexdump.hexdump(data_decompressed[:100])

0 comments on commit d4e823a

Please sign in to comment.