Lua Requires Chilkat v11.0.0+
Lua
Zip and Keep Open
See more Zip Examples
Zip a directory tree by calling WriteZip instead of WriteZipAndClose. When WriteZip is called, the created zip is automatically opened and the zip object contains references to the compressed zip entries (mapped entries) contained within the .zip.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local zip = chilkat.Zip.new()
local zipPath = "c:/temp/myFiles.zip"
-- Initialize the zip object, which also sets the FileName property to the path of the zip to be created.
zip:new_zip(zipPath)
-- Append references to files to be zipped.
local recurse = true
local success = zip:append_files("c:/temp/files_to_zip/*", recurse)
if not success then
print(zip.last_error_text)
return
end
-- Write the .zip, but don't close the zip file.
-- The zip file we just created is automatically opened and the zip object
-- now contains entries that are contained within the zip. (They are memory-mapped entries)
success = zip:write_zip()
if not success then
print(zip.last_error_text)
return
end
print("Successfully created " .. zipPath)
-- Let's look at what's in the .zip we just created..
local numEntries = zip.num_entries
local entry = chilkat.ZipEntry.new()
local i = 0
while i < numEntries do
zip:entry_at(i, entry)
if entry.is_directory then
print(i .. ": " .. entry.file_name .. " (directory)")
else
print(i .. ": " .. entry.file_name)
end
i = i + 1
end
-- Close the zip file when finished.
zip:close_zip()