Sample code for 30+ languages & platforms
PureBasic 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 PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkZip.pb"
IncludeFile "CkFileAccess.pb"
IncludeFile "CkCrypt2.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

    crypt.i = CkCrypt2::ckCreate()
    If crypt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    zip.i = CkZip::ckCreate()
    If zip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckNewZip(zip,"test.zip")
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkCrypt2::ckDispose(crypt)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ;  Add the bytes 0x00 0x01 0x02 0x03 ... 0x0F as a file in the .zip
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkBinData::ckAppendEncoded(bd,"000102030405060708090A0B0C0D0E0F","hex")
    CkZip::ckAddBd(zip,"binaryData.dat",bd)

    ;  Add the string "Hello World!" to the .zip
    CkZip::ckAddString(zip,"helloWorld.txt","Hello World!","utf-8")

ERROR - PureBasic is an array language
    zipFileInMemory = CkZip::ckWriteToMemory(zip)

    ;  We could save these files to a file, and it is a valid .zip
    fac.i = CkFileAccess::ckCreate()
    If fac.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkFileAccess::ckWriteEntireFile(fac,"test.zip",zipFileInMemory)
    If success = 0
        Debug CkFileAccess::ckLastErrorText(fac)
        CkCrypt2::ckDispose(crypt)
        CkZip::ckDispose(zip)
        CkBinData::ckDispose(bd)
        CkFileAccess::ckDispose(fac)
        ProcedureReturn
    EndIf

    Debug "Zip Created!"


    CkCrypt2::ckDispose(crypt)
    CkZip::ckDispose(zip)
    CkBinData::ckDispose(bd)
    CkFileAccess::ckDispose(fac)


    ProcedureReturn
EndProcedure