Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oZip
LOCAL cReadmeText
LOCAL cJsonText
LOCAL cXmlText
nSuccess := 0
oZip := CreateObject("Chilkat.Zip")
nSuccess := oZip:NewZip("stringEntries.zip")
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
// Add a README text file.
cReadmeText := "This ZIP archive was created using AddString."
nSuccess := oZip:AddString("docs/readme.txt", cReadmeText, "utf-8")
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
// Add a JSON configuration file.
cJsonText := '{ "server": "example.com", "port": 443 }'
nSuccess := oZip:AddString("config/settings.json", cJsonText, "utf-8")
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
// Add a small XML document.
cXmlText := "<root><status>OK</status></root>"
nSuccess := oZip:AddString("xml/status.xml", cXmlText, "utf-8")
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
// Write the ZIP archive to disk and close it.
nSuccess := oZip:WriteZipAndClose()
IF (nSuccess == 0)
? oZip:LastErrorText
oZip:destroy()
RETURN
ENDIF
? "ZIP archive created successfully."
oZip:destroy()