Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) Compress and Decompress Base64

This example demonstrates how to compress and decompress binary data using the Chilkat.Compression class with the BinData object.

The input data is provided as a Base64-encoded string. It is first decoded into raw bytes and loaded into a BinData object. The example then compresses the data in-place using the deflate algorithm. Because compressed data is binary, it is re-encoded to Base64 for easy display and transport.

To verify correctness, the example performs the reverse operation: it decodes the compressed Base64 back to bytes, decompresses the data, and encodes the result to Base64 again. The final Base64 output is compared with the original input to confirm that the compression and decompression process preserved the data exactly.

This example highlights:

  • Using BinData to hold and transform binary data
  • In-place compression and decompression with CompressBd and DecompressBd
  • Converting between binary data and Base64 for display or transmission
  • Verifying round-trip integrity after compression and decompression

Chilkat C/C++ Library Downloads

MS Visual C/C++

C++ Builder

Linux C/C++

Alpine Linux C/C++

MacOS C/C++

iOS C/C++

Android C/C++

MinGW C/C++

#include <C_CkCompressionW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    const wchar_t *originalBase64;
    HCkCompressionW compress;
    HCkBinDataW binData;
    const wchar_t *compressedBase64;
    const wchar_t *decompressedBase64;

    success = FALSE;

    // This example assumes the Chilkat API has already been unlocked.
    // See Global Unlock Sample for sample code.

    // This is the original data, encoded as base64.
    // It decodes to plain text containing several repeated lines.
    originalBase64 = L"VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NClRoZSBxdWljayBicm93biBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgZG9nLg0KVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NCg0K";

    compress = CkCompressionW_Create();
    CkCompressionW_putAlgorithm(compress,L"deflate");

    // Load the original base64 data into a BinData object.
    // AppendEncoded decodes the base64 and stores the decoded bytes in binData.
    binData = CkBinDataW_Create();
    success = CkBinDataW_AppendEncoded(binData,originalBase64,L"base64");
    if (success == FALSE) {
        wprintf(L"Failed to decode the original base64 data.\n");
        CkCompressionW_Dispose(compress);
        CkBinDataW_Dispose(binData);
        return;
    }

    // Compress the bytes contained in binData.
    // CompressBd modifies binData in-place, replacing the original bytes
    // with the compressed bytes.
    success = CkCompressionW_CompressBd(compress,binData);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCompressionW_lastErrorText(compress));
        CkCompressionW_Dispose(compress);
        CkBinDataW_Dispose(binData);
        return;
    }

    // Show the compressed result as base64 so it can be printed as text.
    compressedBase64 = CkBinDataW_getEncoded(binData,L"base64");

    wprintf(L"Compressed data, base64 encoded:\n");
    wprintf(L"%s\n",compressedBase64);

    // Expected compressed base64:
    // C8lIVSgszUzOVkgqyi/PU0jLr1DIKs0tSE1RyC9LLVIoAcrnJFZVKqTkp+vxcoUMYeW8XAA=

    // --------------------------------------------------------------------
    // Decompress the data to verify that we get back the original bytes.
    // --------------------------------------------------------------------

    // Start with a fresh BinData object containing the compressed bytes.
    // The compressed data is currently represented as base64 text, so decode it
    // back to bytes before calling DecompressBd.
    success = CkBinDataW_Clear(binData);
    success = CkBinDataW_AppendEncoded(binData,compressedBase64,L"base64");
    if (success == FALSE) {
        wprintf(L"Failed to decode the compressed base64 data.\n");
        CkCompressionW_Dispose(compress);
        CkBinDataW_Dispose(binData);
        return;
    }

    // DecompressBd also modifies binData in-place, replacing the compressed bytes
    // with the decompressed bytes.
    success = CkCompressionW_DecompressBd(compress,binData);
    if (success == FALSE) {
        wprintf(L"%s\n",CkCompressionW_lastErrorText(compress));
        CkCompressionW_Dispose(compress);
        CkBinDataW_Dispose(binData);
        return;
    }

    // Encode the decompressed bytes as base64 so we can compare them with the
    // original base64 input.
    decompressedBase64 = CkBinDataW_getEncoded(binData,L"base64");

    wprintf(L"Decompressed data, base64 encoded:\n");
    wprintf(L"%s\n",decompressedBase64);

    // The decompressed base64 should match the original base64 input.
    if (decompressedBase64 == originalBase64) {
        wprintf(L"Success. The decompressed data matches the original data.\n");
    }
    else {
        wprintf(L"Failure. The decompressed data does not match the original data.\n");
    }



    CkCompressionW_Dispose(compress);
    CkBinDataW_Dispose(binData);

    }

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.