Chilkat2-Python
Chilkat2-Python
Compress Text Feed to Binary
See more Compression Examples
This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed binary data.Chilkat Chilkat2-Python Downloads
import sys
import chilkat2
success = False
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
bdCompressed = chilkat2.BinData()
compress = chilkat2.Compression()
compress.Algorithm = "deflate"
compress.Charset = "utf-8"
sbUncompressedChunk = chilkat2.StringBuilder()
compress.FirstChunk = True
compress.LastChunk = False
for i in range(0,25):
if (i == 24):
compress.LastChunk = True
sbUncompressedChunk.Clear()
sbUncompressedChunk.AppendInt(i)
sbUncompressedChunk.Append(": This is a line of data to be compressed...\r\n")
compress.CompressSb(sbUncompressedChunk,bdCompressed)
compress.FirstChunk = False
# Show the compressed data in hex format:
print("The hex encoded compressed text:")
print(bdCompressed.GetEncoded("hex"))
# Now decompress in one call. It is important to set both FirstChunk and LastChunk = True
bdDecompressed = chilkat2.BinData()
compress.FirstChunk = True
compress.LastChunk = True
success = compress.DecompressBd2(bdCompressed,bdDecompressed)
if (success == False):
print(compress.LastErrorText)
sys.exit()
originalText = bdDecompressed.GetString("utf-8")
print(originalText)