Sample code for 30+ languages & platforms
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 DataFlex Downloads

DataFlex
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