Sample code for 30+ languages & platforms
PowerBuilder

Transition from BeginCompressStringENC to FirstChunk/LastChunk

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

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_SbCompressedBase64
oleobject loo_Compress
oleobject loo_SbIndex
integer i
string ls_OriginalText
oleobject loo_BdCompressed
oleobject loo_SbUncompressedChunk

li_Success = 0

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

loo_SbCompressedBase64 = create oleobject
li_rc = loo_SbCompressedBase64.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_SbCompressedBase64
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Compress = create oleobject
li_rc = loo_Compress.ConnectToNewObject("Chilkat.Compression")

loo_Compress.Algorithm = "deflate"
loo_Compress.Charset = "utf-8"
loo_Compress.EncodingMode = "base64"

loo_SbIndex = create oleobject
li_rc = loo_SbIndex.ConnectToNewObject("Chilkat.StringBuilder")

// ----------------------------------------------------------------------------------
// This is the deprecated way of compressing in chunks using Begin/More/End methods..
// ----------------------------------------------------------------------------------

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.
    loo_SbIndex.Clear()
    loo_SbIndex.AppendInt(i)
    if i = 0 then
        loo_SbCompressedBase64.Append(loo_Compress.BeginCompressStringENC(loo_SbIndex.GetAsString()))
    else
        loo_SbCompressedBase64.Append(loo_Compress.MoreCompressStringENC(loo_SbIndex.GetAsString()))
    end if

    loo_SbCompressedBase64.Append(loo_Compress.MoreCompressStringENC(": This is a line of data to be compressed...~r~n"))
next

// Flush any remaining output.
loo_SbCompressedBase64.Append(loo_Compress.EndCompressStringENC())

Write-Debug "The base64 encoded compressed text:"
Write-Debug loo_SbCompressedBase64.GetAsString()

// Decompress in one call:
ls_OriginalText = loo_Compress.DecompressStringENC(loo_SbCompressedBase64.GetAsString())
Write-Debug ls_OriginalText

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

loo_SbCompressedBase64.Clear()

loo_Compress.FirstChunk = 1
loo_Compress.LastChunk = 0

loo_BdCompressed = create oleobject
li_rc = loo_BdCompressed.ConnectToNewObject("Chilkat.BinData")

loo_SbUncompressedChunk = create oleobject
li_rc = loo_SbUncompressedChunk.ConnectToNewObject("Chilkat.StringBuilder")

for i = 0 to 24
    if i = 24 then
        loo_Compress.LastChunk = 1
    end if

    loo_SbUncompressedChunk.Clear()
    loo_SbUncompressedChunk.AppendInt(i)
    loo_SbUncompressedChunk.Append(": This is a line of data to be compressed...~r~n")

    loo_Compress.CompressSb(loo_SbUncompressedChunk,loo_BdCompressed)

    loo_Compress.FirstChunk = 0
next

Write-Debug "The base64 encoded compressed text:"
Write-Debug loo_BdCompressed.GetEncoded("base64")

// Decompress in one call:
ls_OriginalText = loo_Compress.DecompressStringENC(loo_BdCompressed.GetEncoded("base64"))
Write-Debug ls_OriginalText


destroy loo_SbCompressedBase64
destroy loo_Compress
destroy loo_SbIndex
destroy loo_BdCompressed
destroy loo_SbUncompressedChunk