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) Chunked Compression Using CompressBd2 with FirstChunk and LastChunk

See more Compression Examples
This example demonstrates how to compress data in multiple segments using the CompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.

The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.

This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCompress
    Variant vBdOut
    Handle hoBdOut
    String sPart1
    String sPart2
    String sPart3
    Variant vBdIn
    Handle hoBdIn
    String sCompressedBase64
    Variant vBdDecompressed
    Handle hoBdDecompressed
    String sResultText
    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"

    // This will accumulate the compressed output.
    Get Create (RefClass(cComChilkatBinData)) To hoBdOut
    If (Not(IsComObjectCreated(hoBdOut))) Begin
        Send CreateComObject of hoBdOut
    End

    // ------------------------------------------------------------------
    // Simulate input arriving in chunks.
    // ------------------------------------------------------------------

    Move "The quick brown fox " To sPart1
    Move "jumps over the lazy dog. " To sPart2
    Move "This text is split into chunks." To sPart3

    // ------------------------------------------------------------------
    // Compress the first chunk
    // ------------------------------------------------------------------

    Set ComFirstChunk Of hoCompress To True
    Set ComLastChunk Of hoCompress To False

    Get Create (RefClass(cComChilkatBinData)) To hoBdIn
    If (Not(IsComObjectCreated(hoBdIn))) Begin
        Send CreateComObject of hoBdIn
    End
    Get ComAppendString Of hoBdIn sPart1 "utf-8" To iSuccess

    Get pvComObject of hoBdIn to vBdIn
    Get pvComObject of hoBdOut to vBdOut
    Get ComCompressBd2 Of hoCompress vBdIn vBdOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // ------------------------------------------------------------------
    // Compress a middle chunk
    // ------------------------------------------------------------------

    Set ComFirstChunk Of hoCompress To False
    Set ComLastChunk Of hoCompress To False

    Get ComClear Of hoBdIn To iSuccess
    Get ComAppendString Of hoBdIn sPart2 "utf-8" To iSuccess

    Get pvComObject of hoBdIn to vBdIn
    Get pvComObject of hoBdOut to vBdOut
    Get ComCompressBd2 Of hoCompress vBdIn vBdOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // ------------------------------------------------------------------
    // Compress the final chunk
    // ------------------------------------------------------------------

    Set ComFirstChunk Of hoCompress To False
    Set ComLastChunk Of hoCompress To True

    Get ComClear Of hoBdIn To iSuccess
    Get ComAppendString Of hoBdIn sPart3 "utf-8" To iSuccess

    Get pvComObject of hoBdIn to vBdIn
    Get pvComObject of hoBdOut to vBdOut
    Get ComCompressBd2 Of hoCompress vBdIn vBdOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the final compressed result as base64 for display
    Get ComGetEncoded Of hoBdOut "base64" To sCompressedBase64

    Showln "Compressed data (base64):"
    Showln sCompressedBase64

    // ------------------------------------------------------------------
    // Decompress to verify correctness
    // ------------------------------------------------------------------

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

    Set ComFirstChunk Of hoCompress To True
    Set ComLastChunk Of hoCompress To True

    Get pvComObject of hoBdOut to vBdOut
    Get pvComObject of hoBdDecompressed to vBdDecompressed
    Get ComDecompressBd2 Of hoCompress vBdOut vBdDecompressed To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCompress To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Convert decompressed bytes back to a string
    Get ComGetString Of hoBdDecompressed "utf-8" To sResultText

    Showln "Decompressed text:"
    Showln sResultText


End_Procedure

 

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