Chilkat Examples

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

DataFlex 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
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

 

 

 

(DataFlex) Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples
This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

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

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCompress
    Variant vSb
    Handle hoSb
    Variant vBdCompressed
    Handle hoBdCompressed
    String sCompressedBase64
    Variant vBdChunkedOut
    Handle hoBdChunkedOut
    Variant vSbPart
    Handle hoSbPart
    String sChunkedBase64
    Variant vSbDecompressed
    Handle hoSbDecompressed
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatCompression)) To hoCompress
    If (Not(IsComObjectCreated(hoCompress))) Begin
        Send CreateComObject of hoCompress
    End
    Set ComAlgorithm Of hoCompress To "zlib"

    // ================================================================
    // 1) Single-call compression (entire data in one call)
    // ================================================================

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get ComAppend Of hoSb "The quick brown fox jumps over the lazy dog. " To iSuccess
    Get ComAppend Of hoSb "This is a simple example using CompressSb." To iSuccess

    Get Create (RefClass(cComChilkatBinData)) To hoBdCompressed
    If (Not(IsComObjectCreated(hoBdCompressed))) Begin
        Send CreateComObject of hoBdCompressed
    End

    // When both FirstChunk and LastChunk are true (the defaults),
    // the entire compression happens in a single call.
    Set ComFirstChunk Of hoCompress To True
    Set ComLastChunk Of hoCompress To True

    Get pvComObject of hoSb to vSb
    Get pvComObject of hoBdCompressed to vBdCompressed
    Get ComCompressSb Of hoCompress vSb vBdCompressed To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetEncoded Of hoBdCompressed "base64" To sCompressedBase64

    Showln "Single-call compressed (base64):"
    Showln sCompressedBase64

    // ================================================================
    // 2) Chunked compression using FirstChunk / LastChunk
    // ================================================================

    Get Create (RefClass(cComChilkatBinData)) To hoBdChunkedOut
    If (Not(IsComObjectCreated(hoBdChunkedOut))) Begin
        Send CreateComObject of hoBdChunkedOut
    End

    // First chunk
    Set ComFirstChunk Of hoCompress To True
    Set ComLastChunk Of hoCompress To False

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbPart
    If (Not(IsComObjectCreated(hoSbPart))) Begin
        Send CreateComObject of hoSbPart
    End
    Get ComAppend Of hoSbPart "The quick brown fox " To iSuccess

    Get pvComObject of hoSbPart to vSbPart
    Get pvComObject of hoBdChunkedOut to vBdChunkedOut
    Get ComCompressSb Of hoCompress vSbPart vBdChunkedOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Middle chunk
    Set ComFirstChunk Of hoCompress To False
    Set ComLastChunk Of hoCompress To False

    Send ComClear To hoSbPart
    Get ComAppend Of hoSbPart "jumps over the lazy dog. " To iSuccess

    Get pvComObject of hoSbPart to vSbPart
    Get pvComObject of hoBdChunkedOut to vBdChunkedOut
    Get ComCompressSb Of hoCompress vSbPart vBdChunkedOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Final chunk
    Set ComFirstChunk Of hoCompress To False
    Set ComLastChunk Of hoCompress To True

    Send ComClear To hoSbPart
    Get ComAppend Of hoSbPart "This is a chunked CompressSb example." To iSuccess

    Get pvComObject of hoSbPart to vSbPart
    Get pvComObject of hoBdChunkedOut to vBdChunkedOut
    Get ComCompressSb Of hoCompress vSbPart vBdChunkedOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComGetEncoded Of hoBdChunkedOut "base64" To sChunkedBase64

    Showln "Chunked compressed (base64):"
    Showln sChunkedBase64

    // ================================================================
    // 3) Decompress to verify correctness
    // ================================================================

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbDecompressed
    If (Not(IsComObjectCreated(hoSbDecompressed))) Begin
        Send CreateComObject of hoSbDecompressed
    End

    // Decompress in a single call (entire data already assembled)
    Set ComFirstChunk Of hoCompress To True
    Set ComLastChunk Of hoCompress To True

    Get pvComObject of hoBdChunkedOut to vBdChunkedOut
    Get pvComObject of hoSbDecompressed to vSbDecompressed
    Get ComDecompressSb Of hoCompress vBdChunkedOut vSbDecompressed To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Decompressed text:"
    Get ComGetAsString Of hoSbDecompressed To sTemp1
    Showln sTemp1


End_Procedure

 

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