Sample code for 30+ languages & platforms
Perl Requires Chilkat v11.0.0+

Compress String Feed to Base64

See more Compression Examples

This example receives incoming text data in chunks, compresses as a stream, and accumulates the compressed data in base64.

Chilkat Perl Downloads

Perl
use strict;
use warnings;
use Chilkat2;

my $success = 0;

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

my $sbCompressedBase64 = Chilkat2::StringBuilder->new;

my $compress = Chilkat2::Compression->new;
$compress->Algorithm('deflate');
$compress->Charset('utf-8');
$compress->EncodingMode('base64');

$compress->FirstChunk(1);
$compress->LastChunk(0);

my $bdCompressed = Chilkat2::BinData->new;
my $sbUncompressedChunk = Chilkat2::StringBuilder->new;

for my $i (0 .. 24) {
    if ($i == 24) {
        $compress->LastChunk(1);
    }

    $sbUncompressedChunk->Clear();
    $sbUncompressedChunk->AppendInt($i);
    $sbUncompressedChunk->Append(": This is a line of data to be compressed...\r\n");

    $compress->CompressSb($sbUncompressedChunk, $bdCompressed);

    $compress->FirstChunk(0);
}

print "The base64 encoded compressed text:\n";
print $bdCompressed->GetEncoded('base64'), "\n";

# Decompress in one call:
my $originalText = $compress->DecompressStringENC($bdCompressed->GetEncoded('base64'));
print $originalText, "\n";