Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.0.0+

Create a Zip Entirely in Memory

See more Zip Examples

Demonstrates how to create a .zip from in-memory byte data and strings, and to write the .zip to an in-memory image.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCrypt
    Handle hoZip
    Variant vBd
    Handle hoBd
    Variant hoZipFileInMemory
    Handle hoFac
    String sTemp1

    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(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Get Create (RefClass(cComChilkatZip)) To hoZip
    If (Not(IsComObjectCreated(hoZip))) Begin
        Send CreateComObject of hoZip
    End

    Get ComNewZip Of hoZip "test.zip" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoZip To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Add the bytes 0x00 0x01 0x02 0x03 ... 0x0F as a file in the .zip
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get ComAppendEncoded Of hoBd "000102030405060708090A0B0C0D0E0F" "hex" To iSuccess
    Get pvComObject of hoBd to vBd
    Get ComAddBd Of hoZip "binaryData.dat" vBd To iSuccess

    //  Add the string "Hello World!" to the .zip
    Get ComAddString Of hoZip "helloWorld.txt" "Hello World!" "utf-8" To iSuccess

    Get ComWriteToMemory Of hoZip To hoZipFileInMemory

    //  We could save these files to a file, and it is a valid .zip
    Get Create (RefClass(cComCkFileAccess)) To hoFac
    If (Not(IsComObjectCreated(hoFac))) Begin
        Send CreateComObject of hoFac
    End
    Get ComWriteEntireFile Of hoFac "test.zip" vZipFileInMemory To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFac To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Zip Created!"


End_Procedure