Hi steve,
To create a ZIP archive while keeping a directory tree structure in LuaRT, you need to use the second parameter of the Zip:write() method.
By default, passing just a sys.File or a file path to zip:write(file) will place it at the root level of the ZIP archive. To place a file inside a subdirectory (like a "lib" folder), you pass a second string argument to zip:write() specifying the internal path within the ZIP archive.
Here is a complete, clean example showing how to structure this:
local compression = require("compression")
local sys = require("sys")
-- Create or overwrite a new ZIP archive
local z = compression.Zip("my_project.zip", "write")
if not z then
error("Failed to create ZIP archive")
end
-- 1. Writing files at the root level
-- This will be placed at the root as "main.lua"
if not z:write("main.lua") then
print("Error writing main.lua: " .. z.error)
end
-- This will be placed at the root as "README.md"
if not z:write("README.md") then
print("Error writing README.md: " .. z.error)
end
-- 2. Writing files below a "lib" folder
-- Pass the internal ZIP path as the second argument
if not z:write("utils.lua", "lib/utils.lua") then
print("Error writing utils.lua: " .. z.error)
end
if not z:write("network.lua", "lib/network.lua") then
print("Error writing network.lua: " .. z.error)
end
-- Explicitly close the archive to finalize it
z:close()
print("ZIP archive created successfully with the requested tree structure!")