Unicode C++
Unicode C++
Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)
See more Compression Examples
This example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
- Single-call compression, where the entire input is compressed in one operation.
- Chunked compression, where the input is split into multiple parts and processed sequentially using the
FirstChunkandLastChunkproperties.
The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).
Key Points
CompressSbconverts text to bytes using theCharsetproperty (UTF-8 recommended).- Compressed data is appended to a
BinDataobject. - When both
FirstChunkandLastChunkaretrue, compression is done in a single call. - For chunked processing:
- First chunk →
FirstChunk = true,LastChunk = false - Middle chunks → both
false - Final chunk →
LastChunk = true
- First chunk →
- Chunked compression is useful when processing data incrementally.
Chilkat Unicode C++ Downloads
#include <CkCompressionW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
CkCompressionW compress;
compress.put_Algorithm(L"zlib");
// ================================================================
// 1) Single-call compression (entire data in one call)
// ================================================================
CkStringBuilderW sb;
sb.Append(L"The quick brown fox jumps over the lazy dog. ");
sb.Append(L"This is a simple example using CompressSb.");
CkBinDataW bdCompressed;
// When both FirstChunk and LastChunk are true (the defaults),
// the entire compression happens in a single call.
compress.put_FirstChunk(true);
compress.put_LastChunk(true);
success = compress.CompressSb(sb,bdCompressed);
if (success == false) {
wprintf(L"%s\n",compress.lastErrorText());
return;
}
const wchar_t *compressedBase64 = bdCompressed.getEncoded(L"base64");
wprintf(L"Single-call compressed (base64):\n");
wprintf(L"%s\n",compressedBase64);
// ================================================================
// 2) Chunked compression using FirstChunk / LastChunk
// ================================================================
CkBinDataW bdChunkedOut;
// First chunk
compress.put_FirstChunk(true);
compress.put_LastChunk(false);
CkStringBuilderW sbPart;
sbPart.Append(L"The quick brown fox ");
success = compress.CompressSb(sbPart,bdChunkedOut);
if (success == false) {
wprintf(L"%s\n",compress.lastErrorText());
return;
}
// Middle chunk
compress.put_FirstChunk(false);
compress.put_LastChunk(false);
sbPart.Clear();
sbPart.Append(L"jumps over the lazy dog. ");
success = compress.CompressSb(sbPart,bdChunkedOut);
if (success == false) {
wprintf(L"%s\n",compress.lastErrorText());
return;
}
// Final chunk
compress.put_FirstChunk(false);
compress.put_LastChunk(true);
sbPart.Clear();
sbPart.Append(L"This is a chunked CompressSb example.");
success = compress.CompressSb(sbPart,bdChunkedOut);
if (success == false) {
wprintf(L"%s\n",compress.lastErrorText());
return;
}
const wchar_t *chunkedBase64 = bdChunkedOut.getEncoded(L"base64");
wprintf(L"Chunked compressed (base64):\n");
wprintf(L"%s\n",chunkedBase64);
// ================================================================
// 3) Decompress to verify correctness
// ================================================================
CkStringBuilderW sbDecompressed;
// Decompress in a single call (entire data already assembled)
compress.put_FirstChunk(true);
compress.put_LastChunk(true);
success = compress.DecompressSb(bdChunkedOut,sbDecompressed);
if (success == false) {
wprintf(L"%s\n",compress.lastErrorText());
return;
}
wprintf(L"Decompressed text:\n");
wprintf(L"%s\n",sbDecompressed.getAsString());
}