Sample code for 30+ languages & platforms
PureBasic

Zip Append StringBuilder

See more Zip Examples

Append the contents of a Chilkat StringBuilder object to a .zip.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkZip.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

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

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

    zipPath.s = "c:/temp/qa_output/out.zip"

    CkZip::ckNewZip(zip,zipPath)

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

    i.i = 0
    While i < 100
        CkStringBuilder::ckAppendLine(sb,"This is a test",1)
        i = i + 1
    Wend

    CkZip::ckAddSb(zip,"this_is_a_test.txt",sb,"utf-8")

    success = CkZip::ckWriteZipAndClose(zip)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    Debug "Success 1."

    ; Perhaps you want to add a file to an existing .zip
    zip2.i = CkZip::ckCreate()
    If zip2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Open the .zip we just wrote..
    success = CkZip::ckOpenZip(zip2,zipPath)

    CkStringBuilder::ckClear(sb)
    i = 0
    While i < 100
        CkStringBuilder::ckAppendLine(sb,"This is a test 2",1)
        i = i + 1
    Wend

    CkZip::ckAddSb(zip2,"this_is_a_test_2.txt",sb,"utf-8")

    zipPath2.s = "c:/temp/qa_output/out2.zip"
    CkZip::setCkFileName(zip2, zipPath2)
    success = CkZip::ckWriteZipAndClose(zip2)
    If success = 0
        Debug CkZip::ckLastErrorText(zip2)
        CkZip::ckDispose(zip)
        CkStringBuilder::ckDispose(sb)
        CkZip::ckDispose(zip2)
        ProcedureReturn
    EndIf

    Debug "Success 2."


    CkZip::ckDispose(zip)
    CkStringBuilder::ckDispose(sb)
    CkZip::ckDispose(zip2)


    ProcedureReturn
EndProcedure