Sample code for 30+ languages & platforms
Swift

Transition from BeginCompressStringENC to FirstChunk/LastChunk

Provides instructions for replacing deprecated BeginCompressStringENC method calls with using FirstChunk/LastChunk properties.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let sbCompressedBase64 = CkoStringBuilder()!

    let compress = CkoCompression()!
    compress.algorithm = "deflate"
    compress.charset = "utf-8"
    compress.encodingMode = "base64"

    let sbIndex = CkoStringBuilder()!

    // ----------------------------------------------------------------------------------
    // This is the deprecated way of compressing in chunks using Begin/More/End methods..
    // ----------------------------------------------------------------------------------
    var i: Int
    for i = 0; i <= 24; i++ {
        // Note: It is possible (and normal) for a BeginCompress* or MoreCompress* method to return
        // an empty string (or 0 bytes).  When this happens, the input data is not lost.  It will be flushed
        // in a subsequent call.
        sbIndex.clear()
        sbIndex.appendInt(value: i)
        if i == 0 {
            sbCompressedBase64.append(value: compress.beginCompressStringENC(str: sbIndex.getAsString()))
        }
        else {
            sbCompressedBase64.append(value: compress.moreCompressStringENC(str: sbIndex.getAsString()))
        }

        sbCompressedBase64.append(value: compress.moreCompressStringENC(str: ": This is a line of data to be compressed...\r\n"))
    }

    // Flush any remaining output.
    sbCompressedBase64.append(value: compress.endCompressStringENC())

    print("The base64 encoded compressed text:")
    print("\(sbCompressedBase64.getAsString()!)")

    // Decompress in one call:
    var originalText: String? = compress.decompressStringENC(str: sbCompressedBase64.getAsString())
    print("\(originalText!)")

    // ----------------------------------------------------------------------------------
    // This is new way of compressing in chunks using FirstChunk and LastChunk properties
    // ----------------------------------------------------------------------------------

    sbCompressedBase64.clear()

    compress.firstChunk = true
    compress.lastChunk = false

    let bdCompressed = CkoBinData()!
    let sbUncompressedChunk = CkoStringBuilder()!

    for i = 0; i <= 24; i++ {
        if i == 24 {
            compress.lastChunk = true
        }

        sbUncompressedChunk.clear()
        sbUncompressedChunk.appendInt(value: i)
        sbUncompressedChunk.append(value: ": This is a line of data to be compressed...\r\n")

        compress.compressSb(sb: sbUncompressedChunk, binData: bdCompressed)

        compress.firstChunk = false
    }

    print("The base64 encoded compressed text:")
    print("\(bdCompressed.getEncoded(encoding: "base64")!)")

    // Decompress in one call:
    originalText = compress.decompressStringENC(str: bdCompressed.getEncoded(encoding: "base64"))
    print("\(originalText!)")

}