![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PHP Extension) Compress and Decompress Base64
This example demonstrates how to compress and decompress binary data using the The input data is provided as a Base64-encoded string. It is first decoded into raw bytes and loaded into a To verify correctness, the example performs the reverse operation: it decodes the compressed Base64 back to bytes, decompresses the data, and encodes the result to Base64 again. The final Base64 output is compared with the original input to confirm that the compression and decompression process preserved the data exactly. This example highlights:
<?php include("chilkat.php"); $success = false; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for sample code. // This is the original data, encoded as base64. // It decodes to plain text containing several repeated lines. $originalBase64 = 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NClRoZSBxdWljayBicm93biBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgZG9nLg0KVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NCg0K'; $compress = new CkCompression(); $compress->put_Algorithm('deflate'); // Load the original base64 data into a BinData object. // AppendEncoded decodes the base64 and stores the decoded bytes in binData. $binData = new CkBinData(); $success = $binData->AppendEncoded($originalBase64,'base64'); if ($success == false) { print 'Failed to decode the original base64 data.' . "\n"; exit; } // Compress the bytes contained in binData. // CompressBd modifies binData in-place, replacing the original bytes // with the compressed bytes. $success = $compress->CompressBd($binData); if ($success == false) { print $compress->lastErrorText() . "\n"; exit; } // Show the compressed result as base64 so it can be printed as text. $compressedBase64 = $binData->getEncoded('base64'); print 'Compressed data, base64 encoded:' . "\n"; print $compressedBase64 . "\n"; // Expected compressed base64: // C8lIVSgszUzOVkgqyi/PU0jLr1DIKs0tSE1RyC9LLVIoAcrnJFZVKqTkp+vxcoUMYeW8XAA= // -------------------------------------------------------------------- // Decompress the data to verify that we get back the original bytes. // -------------------------------------------------------------------- // Start with a fresh BinData object containing the compressed bytes. // The compressed data is currently represented as base64 text, so decode it // back to bytes before calling DecompressBd. $success = $binData->Clear(); $success = $binData->AppendEncoded($compressedBase64,'base64'); if ($success == false) { print 'Failed to decode the compressed base64 data.' . "\n"; exit; } // DecompressBd also modifies binData in-place, replacing the compressed bytes // with the decompressed bytes. $success = $compress->DecompressBd($binData); if ($success == false) { print $compress->lastErrorText() . "\n"; exit; } // Encode the decompressed bytes as base64 so we can compare them with the // original base64 input. $decompressedBase64 = $binData->getEncoded('base64'); print 'Decompressed data, base64 encoded:' . "\n"; print $decompressedBase64 . "\n"; // The decompressed base64 should match the original base64 input. if ($decompressedBase64 == $originalBase64) { print 'Success. The decompressed data matches the original data.' . "\n"; } else { print 'Failure. The decompressed data does not match the original data.' . "\n"; } ?> |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.