![]() |
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
(DataFlex) 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.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoZip Variant vEntry Handle hoEntry Variant vDataStream Handle hoDataStream Variant vUnzipTask Handle hoUnzipTask Handle hoFac Variant vBd Handle hoBd String sTemp1 Boolean bTemp1 Move False To iSuccess Move False To iSuccess Get Create (RefClass(cComChilkatZip)) To hoZip If (Not(IsComObjectCreated(hoZip))) Begin Send CreateComObject of hoZip End // Open an existing ZIP archive. Get ComOpenZip Of hoZip "qa_data/zips/big.zip" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // 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. Get Create (RefClass(cComChilkatZipEntry)) To hoEntry If (Not(IsComObjectCreated(hoEntry))) Begin Send CreateComObject of hoEntry End Get pvComObject of hoEntry to vEntry Get ComEntryAt Of hoZip 0 vEntry To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // Create the stream that will receive the uncompressed data. Get Create (RefClass(cComChilkatStream)) To hoDataStream If (Not(IsComObjectCreated(hoDataStream))) Begin Send CreateComObject of hoDataStream End // 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. Get pvComObject of hoDataStream to vDataStream Get ComUnzipToStreamAsync Of hoEntry vDataStream To vUnzipTask If (IsComObject(vUnzipTask)) Begin Get Create (RefClass(cComChilkatTask)) To hoUnzipTask Set pvComObject Of hoUnzipTask To vUnzipTask End Get ComLastMethodSuccess Of hoEntry To bTemp1 If (bTemp1 = False) Begin Get ComLastErrorText Of hoEntry To sTemp1 Showln sTemp1 Procedure_Return End // Start the background unzip thread. Get ComRun Of hoUnzipTask To iSuccess // Open the output file that will receive the uncompressed data. Get Create (RefClass(cComCkFileAccess)) To hoFac If (Not(IsComObjectCreated(hoFac))) Begin Send CreateComObject of hoFac End Get ComOpenForWrite Of hoFac "c:/temp/qa_output/out.dat" To iSuccess If (iSuccess = False) Begin Get ComLastErrorText Of hoFac To sTemp1 Showln sTemp1 Procedure_Return End // 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. Get Create (RefClass(cComChilkatBinData)) To hoBd If (Not(IsComObjectCreated(hoBd))) Begin Send CreateComObject of hoBd End While ((ComEndOfStream(hoDataStream)) <> True) // Read the next available chunk of uncompressed bytes. Get pvComObject of hoBd to vBd Get ComReadBd Of hoDataStream vBd To iSuccess // Write this chunk to the output file. Get pvComObject of hoBd to vBd Get ComFileWriteBd Of hoFac vBd 0 0 To iSuccess // Clear the BinData object so it can be reused for the next chunk. Get ComClear Of hoBd To iSuccess Loop // Close the output file. Send ComFileClose To hoFac // The background unzip task has completed when the stream reaches EOF. // Delete the task object when no longer needed. Send Destroy of hoUnzipTask // Close the ZIP archive. Send ComCloseZip To hoZip Showln "Success." End_Procedure |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.