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) Stream a Large ZIP Entry While Uncompressing Using ZipEntry.UnzipToStreamAsync

See more Zip Examples

This example demonstrates how to use ZipEntry.UnzipToStreamAsync to asynchronously unzip a ZIP entry directly to a stream while simultaneously reading the uncompressed data from the foreground thread.

The example is especially useful for large ZIP entries because the entire uncompressed file does not need to be held in memory at once. Instead, the uncompressed data is processed incrementally in chunks as it becomes available.

The example performs the following steps:

  • Opens an existing ZIP archive
  • Gets a ZIP entry
  • Starts an asynchronous unzip operation to a Stream
  • Reads the uncompressed data chunk-by-chunk from the stream
  • Writes each chunk directly to an output file

This approach is useful when:

  • Processing very large ZIP entries
  • Avoiding large memory allocations
  • Streaming uncompressed data to another destination
  • Performing incremental processing while decompression occurs

The example demonstrates a producer/consumer pattern where:

  • The background thread inflates the ZIP entry into the Stream
  • The foreground thread reads the uncompressed bytes from the Stream as they become available

The uncompressed bytes are written incrementally to:

qa_output/out.dat

Note: The standard Zip.Unzip* methods that extract directly to filesystem files already perform decompression in streaming mode internally within Chilkat. Chilkat does not inflate the entire file into memory before writing the output file.

Therefore, it is not necessary to use a more complex streaming approach such as this example merely to efficiently unzip large files to disk.

The purpose of this example is instead to demonstrate how an application can directly access and process the uncompressed data incrementally in chunks as the unzip operation is occurring.

This technique is ideal when the application needs to inspect, analyze, transform, transmit, or otherwise process the uncompressed bytes while they are being produced.

Note: This example requires Chilkat v11.0.0 or greater.

Chilkat PHP Downloads

PHP Extension for Windows, Linux, MacOS, Alpine Linux

<?php

include("chilkat.php");

$success = false;

$success = false;

$zip = new CkZip();

// Open an existing ZIP archive.
$success = $zip->OpenZip('qa_data/zips/big.zip');
if ($success == false) {
    print $zip->lastErrorText() . "\n";
    exit;
}

// Get the ZIP entry to be unzipped.
// 
// This example uses the first entry in the ZIP archive.
// The entry may contain a large file, so we will unzip it
// to a stream instead of loading the entire file into memory.
$entry = new CkZipEntry();

$success = $zip->EntryAt(0,$entry);
if ($success == false) {
    print $zip->lastErrorText() . "\n";
    exit;
}

// Create the stream that will receive the uncompressed data.
$dataStream = new CkStream();

// Start an asynchronous unzip operation.
// 
// UnzipToStreamAsync writes the uncompressed entry data to dataStream
// from a background thread. The foreground thread can read from the
// stream as data becomes available.
// unzipTask is a CkTask
$unzipTask = $entry->UnzipToStreamAsync($dataStream);
if ($entry->get_LastMethodSuccess() == false) {
    print $entry->lastErrorText() . "\n";
    exit;
}

// Start the background unzip thread.
$unzipTask->Run();

// Open the output file that will receive the uncompressed data.
$fac = new CkFileAccess();

$success = $fac->OpenForWrite('c:/temp/qa_output/out.dat');
if ($success == false) {
    print $fac->lastErrorText() . "\n";
    exit;
}

// Read the uncompressed data from the stream in chunks.
// 
// This avoids holding the entire uncompressed file in memory.
// Each chunk is written immediately to the output file.
$bd = new CkBinData();

while (($dataStream->get_EndOfStream() != true)) {

    // Read the next available chunk of uncompressed bytes.
    $dataStream->ReadBd($bd);

    // Write this chunk to the output file.
    $fac->FileWriteBd($bd,0,0);

    // Clear the BinData object so it can be reused for the next chunk.
    $bd->Clear();
}

// Close the output file.
$fac->FileClose();

// The background unzip task has completed when the stream reaches EOF.
// Delete the task object when no longer needed.

// Close the ZIP archive.
$zip->CloseZip();

print 'Success.' . "\n";

?>

 

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