Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Unzip Some Files by Iterating over Entries

See more Zip Examples

Demonstrates how to unzip specific files by iterating over entries in a .zip.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local zip = chilkat.Zip.new()

-- Open a .zip containing:
--
-- a1.xml
-- b1.xml
-- c1.xml
-- dir1/a2.xml
-- dir1/c2.xml
-- dir2/dir3/c3.xml

-- We wish to unzip only a1.xml, b1.xml, and c1.xml
local success = zip:open_zip("qa_data/zips/xml_files.zip")
if not success then
    print(zip.last_error_text)
    return
end

local sbFilename = chilkat.StringBuilder.new()

local entry = chilkat.ZipEntry.new()
local numEntries = zip.num_entries
local i = 0
while i < numEntries do
    zip:entry_at(i, entry)

    local entryFilePath = entry.file_name
    print(entryFilePath)

    if not entry.is_directory then
        sbFilename:set_string(entryFilePath)
        if not sbFilename:contains("/", false) then
            -- Does not contain "/"
            -- Unzip to the qa_output directory.
            success = entry:extract("qa_output")
            if not success then
                print("Failed to unzip " .. entryFilePath)
            else
                print("Unzipped " .. entryFilePath)
            end
        end
    end

    i = i + 1
end