Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PHP Extension Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PHP Extension) Chunked Compression Using CompressBd2 with FirstChunk and LastChunk

See more Compression Examples
This example demonstrates how to compress data in multiple segments using the CompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.

The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate BinData object without modifying the input chunks. Finally, the example decompresses the combined result to verify that the original data is restored correctly.

This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.

Chilkat PHP Downloads

PHP Extension for Windows, Linux, MacOS, Alpine Linux

<?php

include("chilkat.php");

$success = false;

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

$compress = new CkCompression();
$compress->put_Algorithm('zlib');

// This will accumulate the compressed output.
$bdOut = new CkBinData();

// ------------------------------------------------------------------
// Simulate input arriving in chunks.
// ------------------------------------------------------------------

$part1 = 'The quick brown fox ';
$part2 = 'jumps over the lazy dog. ';
$part3 = 'This text is split into chunks.';

// ------------------------------------------------------------------
// Compress the first chunk
// ------------------------------------------------------------------

$compress->put_FirstChunk(true);
$compress->put_LastChunk(false);

$bdIn = new CkBinData();
$bdIn->AppendString($part1,'utf-8');

$success = $compress->CompressBd2($bdIn,$bdOut);
if ($success == false) {
    print $compress->lastErrorText() . "\n";
    exit;
}

// ------------------------------------------------------------------
// Compress a middle chunk
// ------------------------------------------------------------------

$compress->put_FirstChunk(false);
$compress->put_LastChunk(false);

$bdIn->Clear();
$bdIn->AppendString($part2,'utf-8');

$success = $compress->CompressBd2($bdIn,$bdOut);
if ($success == false) {
    print $compress->lastErrorText() . "\n";
    exit;
}

// ------------------------------------------------------------------
// Compress the final chunk
// ------------------------------------------------------------------

$compress->put_FirstChunk(false);
$compress->put_LastChunk(true);

$bdIn->Clear();
$bdIn->AppendString($part3,'utf-8');

$success = $compress->CompressBd2($bdIn,$bdOut);
if ($success == false) {
    print $compress->lastErrorText() . "\n";
    exit;
}

// Get the final compressed result as base64 for display
$compressedBase64 = $bdOut->getEncoded('base64');

print 'Compressed data (base64):' . "\n";
print $compressedBase64 . "\n";

// ------------------------------------------------------------------
// Decompress to verify correctness
// ------------------------------------------------------------------

$bdDecompressed = new CkBinData();

$compress->put_FirstChunk(true);
$compress->put_LastChunk(true);

$success = $compress->DecompressBd2($bdOut,$bdDecompressed);
if ($success == false) {
    print $compress->lastErrorText() . "\n";
    exit;
}

// Convert decompressed bytes back to a string
$resultText = $bdDecompressed->getString('utf-8');

print 'Decompressed text:' . "\n";
print $resultText . "\n";

?>

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.