Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(DataFlex) Unzip One File to a StreamDemonstrates how to unzip a particular file contained within a .zip archive to a Chilkat stream.
Use ChilkatAx-win32.pkg Procedure Test Handle hoZip Boolean iSuccess Variant vEntry Handle hoEntry Variant vStreamA Handle hoStreamA Variant vStreamB Handle hoStreamB Variant vUnzipTask Handle hoUnzipTask Variant vSb Handle hoSb Variant vStreamC Handle hoStreamC String s String sTemp1 Boolean bTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatZip)) To hoZip If (Not(IsComObjectCreated(hoZip))) Begin Send CreateComObject of hoZip End // First open a .zip. Get ComOpenZip Of hoZip "qa_data/hamlet.zip" To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // Find the entry to be unzipped to a stream.. Get ComFirstMatchingEntry Of hoZip "*hamlet.xml" To vEntry If (IsComObject(vEntry)) Begin Get Create (RefClass(cComChilkatZipEntry)) To hoEntry Set pvComObject Of hoEntry To vEntry End Get ComLastMethodSuccess Of hoZip To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoZip To sTemp1 Showln sTemp1 Procedure_Return End // There are three ways we can unzip (inflate) to a stream. // // 1) Set a sink file on the stream object and call UnzipToStream synchronously. // This unzips to the stream's sink (which is to simply unzip to a file in the filesystem). // // 2) Create a stream object and call UnzipToStreamAsync. This starts the unzip in a background // thread (after Task.Run is called). Your application can then read the unzipped (inflated) file // directly from the stream. // // 3) (not shown in this example) Unzip to a stream object that is the source of something else. // // ---------------------------------------------------------------- // Case 1: Unzip to a file by setting the sink of streamA to a file. Get Create (RefClass(cComChilkatStream)) To hoStreamA If (Not(IsComObjectCreated(hoStreamA))) Begin Send CreateComObject of hoStreamA End Set ComSinkFile Of hoStreamA To "qa_output/hamletA.xml" Get pvComObject of hoStreamA to vStreamA Get ComUnzipToStream Of hoEntry vStreamA To iSuccess If (iSuccess <> True) Begin Get ComLastErrorText Of hoEntry To sTemp1 Showln sTemp1 Send Destroy of hoEntry Procedure_Return End // Close the stream to close the output file. Get ComWriteClose Of hoStreamA To iSuccess // ---------------------------------------------------------------- // Case 2a: Uzip to a stream asynchronously. Get Create (RefClass(cComChilkatStream)) To hoStreamB If (Not(IsComObjectCreated(hoStreamB))) Begin Send CreateComObject of hoStreamB End Get pvComObject of hoStreamB to vStreamB Get ComUnzipToStreamAsync Of hoEntry vStreamB 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 <> True) Begin Get ComLastErrorText Of hoEntry To sTemp1 Showln sTemp1 Send Destroy of hoEntry Procedure_Return End // Start the background unzip thread. Get ComRun Of hoUnzipTask To iSuccess // Read the stream. (this is reading the unzipped file data) Get Create (RefClass(cComChilkatStringBuilder)) To hoSb If (Not(IsComObjectCreated(hoSb))) Begin Send CreateComObject of hoSb End While ((ComEndOfStream(hoStreamB)) <> True) // Each call to ReadSb appends to the contents of sb. Get pvComObject of hoSb to vSb Get ComReadSb Of hoStreamB vSb To iSuccess Loop Get ComWriteFile Of hoSb "qa_output/hamletB.xml" "utf-8" False To iSuccess Send Destroy of hoUnzipTask Showln "Success." // ---------------------------------------------------------------- // Case 2b: Uzip to a stream asynchronously and emit output while reading. Get Create (RefClass(cComChilkatStream)) To hoStreamC If (Not(IsComObjectCreated(hoStreamC))) Begin Send CreateComObject of hoStreamC End Get pvComObject of hoStreamC to vStreamC Get ComUnzipToStreamAsync Of hoEntry vStreamC 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 <> True) Begin Get ComLastErrorText Of hoEntry To sTemp1 Showln sTemp1 Send Destroy of hoEntry Procedure_Return End Get ComRun Of hoUnzipTask To iSuccess While ((ComEndOfStream(hoStreamC)) <> True) Get ComReadString Of hoStreamC To s Showln s Loop Send Destroy of hoEntry Send Destroy of hoUnzipTask Showln "Success." End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.