Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local zip = chilkat.Zip.new()
local success = zip:new_zip("stringEntries.zip")
if not success then
print(zip.last_error_text)
return
end
-- Add a README text file.
local readmeText = "This ZIP archive was created using AddString."
success = zip:add_string("docs/readme.txt", readmeText, "utf-8")
if not success then
print(zip.last_error_text)
return
end
-- Add a JSON configuration file.
local jsonText = "{ \"server\": \"example.com\", \"port\": 443 }"
success = zip:add_string("config/settings.json", jsonText, "utf-8")
if not success then
print(zip.last_error_text)
return
end
-- Add a small XML document.
local xmlText = "<root><status>OK</status></root>"
success = zip:add_string("xml/status.xml", xmlText, "utf-8")
if not success then
print(zip.last_error_text)
return
end
-- Write the ZIP archive to disk and close it.
success = zip:write_zip_and_close()
if not success then
print(zip.last_error_text)
return
end
print("ZIP archive created successfully.")