Sample code for 30+ languages & platforms
PureBasic

Add Text from a StringBuilder to a ZIP Using AddSb

See more Zip Examples

This example demonstrates how to use the AddSb method to add text from a StringBuilder object as a file entry within a ZIP archive.

The text is converted to bytes using the specified character encoding before being stored in the ZIP archive.

This method is useful for dynamically generating text files entirely in memory without creating temporary files on disk.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

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

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

    ; Create a StringBuilder containing text data.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppendLine(sb,"Line 1: Hello World!",1)
    CkStringBuilder::ckAppendLine(sb,"Line 2: This text came from a StringBuilder.",1)

    ; Add the StringBuilder contents as a UTF-8 text file
    ; stored in the ZIP archive as "docs/readme.txt".
    success = CkZip::ckAddSb(zip,"docs/readme.txt",sb,"utf-8")
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    ; Write the ZIP archive to disk and close it.
    success = CkZip::ckWriteZipAndClose(zip)
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    Debug "ZIP archive created successfully."


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


    ProcedureReturn
EndProcedure