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) Compressing and Decompressing Files Using Streaming (CompressFile / DecompressFile)

See more Compression Examples

This example demonstrates how to compress a file to a binary format and then restore it using the Chilkat.Compression class. The CompressFile method reads the source file, compresses it using the specified algorithm, and writes the result to a destination file. The DecompressFile method performs the reverse operation, restoring the original file from the compressed data.

Both operations are performed internally in streaming mode, allowing files of any size to be processed efficiently without loading the entire file into memory. The example also includes a simple verification step by comparing file sizes to confirm that the decompressed output matches the original input.

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();

// Use the zlib algorithm (recommended for general use)
$compress->put_Algorithm('zlib');

// ------------------------------------------------------------------
// Compress a file
// ------------------------------------------------------------------

$inputFile = 'c:/temp/example.txt';
$compressedFile = 'c:/temp/example.txt.zlib';

$success = $compress->CompressFile($inputFile,$compressedFile);
if ($success == false) {
    print 'Compression failed:' . "\n";
    print $compress->lastErrorText() . "\n";
    exit;
}

print 'File compressed successfully:' . "\n";
print '  Input:      ' . $inputFile . "\n";
print '  Compressed: ' . $compressedFile . "\n";

// ------------------------------------------------------------------
// Decompress the file back to its original form
// ------------------------------------------------------------------

$decompressedFile = 'c:/temp/example_restored.txt';

$success = $compress->DecompressFile($compressedFile,$decompressedFile);
if ($success == false) {
    print 'Decompression failed:' . "\n";
    print $compress->lastErrorText() . "\n";
    exit;
}

print 'File decompressed successfully:' . "\n";
print '  Output: ' . $decompressedFile . "\n";

// ------------------------------------------------------------------
// Optional: Verify file sizes (basic sanity check)
// ------------------------------------------------------------------

$fac = new CkFileAccess();

$originalSize = $fac->FileSize($inputFile);
$restoredSize = $fac->FileSize($decompressedFile);

print 'Original file size:   ' . $originalSize . "\n";
print 'Restored file size:   ' . $restoredSize . "\n";

if ($originalSize == $restoredSize) {
    print 'Sizes match (basic verification successful).' . "\n";
}
else {
    print 'Warning: File sizes differ.' . "\n";
}


?>

 

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