![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C++) Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)See more Compression ExamplesThis example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
The example shows how compressed output is appended to a Key Points
#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()); } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.