Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loZip
LOCAL lcReadmeText
LOCAL lcJsonText
LOCAL lcXmlText
lnSuccess = 0
loZip = CreateObject('Chilkat.Zip')
lnSuccess = loZip.NewZip("stringEntries.zip")
IF (lnSuccess = 0) THEN
? loZip.LastErrorText
RELEASE loZip
CANCEL
ENDIF
* Add a README text file.
lcReadmeText = "This ZIP archive was created using AddString."
lnSuccess = loZip.AddString("docs/readme.txt",lcReadmeText,"utf-8")
IF (lnSuccess = 0) THEN
? loZip.LastErrorText
RELEASE loZip
CANCEL
ENDIF
* Add a JSON configuration file.
lcJsonText = '{ "server": "example.com", "port": 443 }'
lnSuccess = loZip.AddString("config/settings.json",lcJsonText,"utf-8")
IF (lnSuccess = 0) THEN
? loZip.LastErrorText
RELEASE loZip
CANCEL
ENDIF
* Add a small XML document.
lcXmlText = "<root><status>OK</status></root>"
lnSuccess = loZip.AddString("xml/status.xml",lcXmlText,"utf-8")
IF (lnSuccess = 0) THEN
? loZip.LastErrorText
RELEASE loZip
CANCEL
ENDIF
* Write the ZIP archive to disk and close it.
lnSuccess = loZip.WriteZipAndClose()
IF (lnSuccess = 0) THEN
? loZip.LastErrorText
RELEASE loZip
CANCEL
ENDIF
? "ZIP archive created successfully."
RELEASE loZip