PureBasic
PureBasic
Add Text Files to a ZIP Using AddString
See more Zip Examples
This example demonstrates how to use the AddString method to add multiple text files directly from string variables into a ZIP archive.
Each string is converted to bytes using the specified character encoding and stored as a separate file entry within the ZIP archive.
This method is useful for dynamically generating small text files such as configuration files, reports, JSON documents, XML, or log files entirely in memory.
Chilkat PureBasic Downloads
IncludeFile "CkZip.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,"stringEntries.zip")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
ProcedureReturn
EndIf
; Add a README text file.
readmeText.s = "This ZIP archive was created using AddString."
success = CkZip::ckAddString(zip,"docs/readme.txt",readmeText,"utf-8")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
ProcedureReturn
EndIf
; Add a JSON configuration file.
jsonText.s = "{ " + Chr(34) + "server" + Chr(34) + ": " + Chr(34) + "example.com" + Chr(34) + ", " + Chr(34) + "port" + Chr(34) + ": 443 }"
success = CkZip::ckAddString(zip,"config/settings.json",jsonText,"utf-8")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
ProcedureReturn
EndIf
; Add a small XML document.
xmlText.s = "<root><status>OK</status></root>"
success = CkZip::ckAddString(zip,"xml/status.xml",xmlText,"utf-8")
If success = 0
Debug CkZip::ckLastErrorText(zip)
CkZip::ckDispose(zip)
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)
ProcedureReturn
EndIf
Debug "ZIP archive created successfully."
CkZip::ckDispose(zip)
ProcedureReturn
EndProcedure