Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let compress = CkoCompression()!
    compress.algorithm = "zlib"

    // This will accumulate the compressed output.
    let bdOut = CkoBinData()!

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

    var part1: String? = "The quick brown fox "
    var part2: String? = "jumps over the lazy dog. "
    var part3: String? = "This text is split into chunks."

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

    compress.firstChunk = true
    compress.lastChunk = false

    let bdIn = CkoBinData()!
    bdIn.appendString(str: part1, charset: "utf-8")

    success = compress.compressBd2(bdIn: bdIn, bdOut: bdOut)
    if success == false {
        print("\(compress.lastErrorText!)")
        return
    }

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

    compress.firstChunk = false
    compress.lastChunk = false

    bdIn.clear()
    bdIn.appendString(str: part2, charset: "utf-8")

    success = compress.compressBd2(bdIn: bdIn, bdOut: bdOut)
    if success == false {
        print("\(compress.lastErrorText!)")
        return
    }

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

    compress.firstChunk = false
    compress.lastChunk = true

    bdIn.clear()
    bdIn.appendString(str: part3, charset: "utf-8")

    success = compress.compressBd2(bdIn: bdIn, bdOut: bdOut)
    if success == false {
        print("\(compress.lastErrorText!)")
        return
    }

    // Get the final compressed result as base64 for display
    var compressedBase64: String? = bdOut.getEncoded(encoding: "base64")

    print("Compressed data (base64):")
    print("\(compressedBase64!)")

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

    let bdDecompressed = CkoBinData()!

    compress.firstChunk = true
    compress.lastChunk = true

    success = compress.decompressBd2(bdIn: bdOut, bdOut: bdDecompressed)
    if success == false {
        print("\(compress.lastErrorText!)")
        return
    }

    // Convert decompressed bytes back to a string
    var resultText: String? = bdDecompressed.getString(charset: "utf-8")

    print("Decompressed text:")
    print("\(resultText!)")

}