Sample code for 30+ languages & platforms
Chilkat2-Python

Compress Bytes

See more Compression Examples

Demonstrates how to compress binary data.

Chilkat Chilkat2-Python Downloads

Chilkat2-Python
import sys
import chilkat2

success = False

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

fac = chilkat2.FileAccess()

# fileBytes is a memoryview
fileBytes = fac.ReadEntireFile("qa_data/bmp/big.bmp")
if (fac.LastMethodSuccess != True):
    print(fac.LastErrorText)
    sys.exit()

compress = chilkat2.Compression()
compress.Algorithm = "deflate"

# compressedBytes is a memoryview
compressedBytes = compress.CompressBytes(fileBytes)
if (compress.LastMethodSuccess != True):
    print(compress.LastErrorText)
    sys.exit()

success = fac.WriteEntireFile("qa_output/compressedBmp.dat",compressedBytes)
if (fac.LastMethodSuccess != True):
    print(fac.LastErrorText)
    sys.exit()