Sample code for 30+ languages & platforms
DataFlex

Unzip One File to a Stream

See more Zip Examples

Demonstrates how to unzip a particular file contained within a .zip archive to a Chilkat stream.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoZip
    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

    Move False To iSuccess

    // 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 = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Find the entry to be unzipped to a stream..
    Get Create (RefClass(cComChilkatZipEntry)) To hoEntry
    If (Not(IsComObjectCreated(hoEntry))) Begin
        Send CreateComObject of hoEntry
    End
    Get pvComObject of hoEntry to vEntry
    Get ComEntryMatching Of hoZip "*hamlet.xml" vEntry To iSuccess
    If (iSuccess = False) 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
        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
        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
        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 hoUnzipTask
    Showln "Success."


End_Procedure