![]() |
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) Stream a Large ZIP Entry While Uncompressing Using ZipEntry.UnzipToStreamAsyncSee more Zip Examples This example demonstrates how to use 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:
This approach is useful when:
The example demonstrates a producer/consumer pattern where:
The uncompressed bytes are written incrementally to: Note: The standard 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.
<?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.