C++ String Compression: PPMD, Deflate, BZip2, LZW

This C++ example demonstrates string compression using four different compression algorithms: PPMD, Deflate, BZip2, and LZW.

void TestCompression(const char *algorithm)
    {
    CkCompression comp;

    comp.put_Algorithm(algorithm);
    printf ("algorithm: %s\n",algorithm);

    CkByteData compressedData;
    comp.CompressString("abc abc abc abc abc abc abc abc abc abc abc 123 123 abc 123",compressedData);

    // Get the compressed data:
    const unsigned char *pCompressedData = compressedData.getBytes();
    unsigned long numBytes = compressedData.getSize();
    printf("numBytes = %d\n",numBytes);

    // Imagine you've sent the compressed data over a socket connection to some receiver...


    // Decompress:
    CkByteData compressedData2;
    compressedData2.append(pCompressedData,numBytes);
    printf("%s\n",comp.decompressString(compressedData2));
    printf("----\n");
    }

void CompressionExample(void)
    {
    CkCompression comp;
    comp.UnlockComponent("30-day trial");

    TestCompression("deflate");
    TestCompression("ppmd");
    TestCompression("bzip2");
    TestCompression("lzw");

    }

The output is as follows:

algorithm: deflate
numBytes = 15
abc abc abc abc abc abc abc abc abc abc abc 123 123 abc 123
----
algorithm: ppmd
numBytes = 18
abc abc abc abc abc abc abc abc abc abc abc 123 123 abc 123
----
algorithm: bzip2
numBytes = 54
abc abc abc abc abc abc abc abc abc abc abc 123 123 abc 123
----
algorithm: lzw
numBytes = 30
abc abc abc abc abc abc abc abc abc abc abc 123 123 abc 123
----