Sample code for 30+ languages & platforms
Perl

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).


Key Points

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat Perl Downloads

Perl
use chilkat();

$success = 0;

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

$compress = chilkat::CkCompression->new();
$compress->put_Algorithm("zlib");

# ================================================================
# 1) Single-call compression (entire data in one call)
# ================================================================

$sb = chilkat::CkStringBuilder->new();
$sb->Append("The quick brown fox jumps over the lazy dog. ");
$sb->Append("This is a simple example using CompressSb.");

$bdCompressed = chilkat::CkBinData->new();

# When both FirstChunk and LastChunk are true (the defaults),
# the entire compression happens in a single call.
$compress->put_FirstChunk(1);
$compress->put_LastChunk(1);

$success = $compress->CompressSb($sb,$bdCompressed);
if ($success == 0) {
    print $compress->lastErrorText() . "\r\n";
    exit;
}

$compressedBase64 = $bdCompressed->getEncoded("base64");

print "Single-call compressed (base64):" . "\r\n";
print $compressedBase64 . "\r\n";

# ================================================================
# 2) Chunked compression using FirstChunk / LastChunk
# ================================================================

$bdChunkedOut = chilkat::CkBinData->new();

# First chunk
$compress->put_FirstChunk(1);
$compress->put_LastChunk(0);

$sbPart = chilkat::CkStringBuilder->new();
$sbPart->Append("The quick brown fox ");

$success = $compress->CompressSb($sbPart,$bdChunkedOut);
if ($success == 0) {
    print $compress->lastErrorText() . "\r\n";
    exit;
}

# Middle chunk
$compress->put_FirstChunk(0);
$compress->put_LastChunk(0);

$sbPart->Clear();
$sbPart->Append("jumps over the lazy dog. ");

$success = $compress->CompressSb($sbPart,$bdChunkedOut);
if ($success == 0) {
    print $compress->lastErrorText() . "\r\n";
    exit;
}

# Final chunk
$compress->put_FirstChunk(0);
$compress->put_LastChunk(1);

$sbPart->Clear();
$sbPart->Append("This is a chunked CompressSb example.");

$success = $compress->CompressSb($sbPart,$bdChunkedOut);
if ($success == 0) {
    print $compress->lastErrorText() . "\r\n";
    exit;
}

$chunkedBase64 = $bdChunkedOut->getEncoded("base64");

print "Chunked compressed (base64):" . "\r\n";
print $chunkedBase64 . "\r\n";

# ================================================================
# 3) Decompress to verify correctness
# ================================================================

$sbDecompressed = chilkat::CkStringBuilder->new();

# Decompress in a single call (entire data already assembled)
$compress->put_FirstChunk(1);
$compress->put_LastChunk(1);

$success = $compress->DecompressSb($bdChunkedOut,$sbDecompressed);
if ($success == 0) {
    print $compress->lastErrorText() . "\r\n";
    exit;
}

print "Decompressed text:" . "\r\n";
print $sbDecompressed->getAsString() . "\r\n";