Sample code for 30+ languages & platforms
PureBasic

Transition from BeginCompressStringENC to FirstChunk/LastChunk

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkCompression.pb"

Procedure ChilkatExample()

    success.i = 0

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

    sbCompressedBase64.i = CkStringBuilder::ckCreate()
    If sbCompressedBase64.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    compress.i = CkCompression::ckCreate()
    If compress.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCompression::setCkAlgorithm(compress, "deflate")
    CkCompression::setCkCharset(compress, "utf-8")
    CkCompression::setCkEncodingMode(compress, "base64")

    sbIndex.i = CkStringBuilder::ckCreate()
    If sbIndex.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; ----------------------------------------------------------------------------------
    ; This is the deprecated way of compressing in chunks using Begin/More/End methods..
    ; ----------------------------------------------------------------------------------
    i.i
    For i = 0 To 24
        ; 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.
        CkStringBuilder::ckClear(sbIndex)
        CkStringBuilder::ckAppendInt(sbIndex,i)
        If i = 0
            CkStringBuilder::ckAppend(sbCompressedBase64,CkCompression::ckBeginCompressStringENC(compress,CkStringBuilder::ckGetAsString(sbIndex)))
        Else
            CkStringBuilder::ckAppend(sbCompressedBase64,CkCompression::ckMoreCompressStringENC(compress,CkStringBuilder::ckGetAsString(sbIndex)))
        EndIf

        CkStringBuilder::ckAppend(sbCompressedBase64,CkCompression::ckMoreCompressStringENC(compress,": This is a line of data to be compressed..." + Chr(13) + Chr(10)))
    Next

    ; Flush any remaining output.
    CkStringBuilder::ckAppend(sbCompressedBase64,CkCompression::ckEndCompressStringENC(compress))

    Debug "The base64 encoded compressed text:"
    Debug CkStringBuilder::ckGetAsString(sbCompressedBase64)

    ; Decompress in one call:
    originalText.s = CkCompression::ckDecompressStringENC(compress,CkStringBuilder::ckGetAsString(sbCompressedBase64))
    Debug originalText

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

    CkStringBuilder::ckClear(sbCompressedBase64)

    CkCompression::setCkFirstChunk(compress, 1)
    CkCompression::setCkLastChunk(compress, 0)

    bdCompressed.i = CkBinData::ckCreate()
    If bdCompressed.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    sbUncompressedChunk.i = CkStringBuilder::ckCreate()
    If sbUncompressedChunk.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    For i = 0 To 24
        If i = 24
            CkCompression::setCkLastChunk(compress, 1)
        EndIf

        CkStringBuilder::ckClear(sbUncompressedChunk)
        CkStringBuilder::ckAppendInt(sbUncompressedChunk,i)
        CkStringBuilder::ckAppend(sbUncompressedChunk,": This is a line of data to be compressed..." + Chr(13) + Chr(10))

        CkCompression::ckCompressSb(compress,sbUncompressedChunk,bdCompressed)

        CkCompression::setCkFirstChunk(compress, 0)
    Next

    Debug "The base64 encoded compressed text:"
    Debug CkBinData::ckGetEncoded(bdCompressed,"base64")

    ; Decompress in one call:
    originalText = CkCompression::ckDecompressStringENC(compress,CkBinData::ckGetEncoded(bdCompressed,"base64"))
    Debug originalText


    CkStringBuilder::ckDispose(sbCompressedBase64)
    CkCompression::ckDispose(compress)
    CkStringBuilder::ckDispose(sbIndex)
    CkBinData::ckDispose(bdCompressed)
    CkStringBuilder::ckDispose(sbUncompressedChunk)


    ProcedureReturn
EndProcedure