LuaZip
Reading files inside zip files

Example

Suppose we have the following file hierarchy:

/a
    /b
        c.zip
/a2
    b2.ext2
/a3.ext3
/luazip.zip
  • c.zip contains the file 'd.txt'
  • b2.ext2 is a zip file containing the file 'c2/d2.txt'
  • a3.ext3 is a zip file containing the file 'b3/c3/d3.txt'
  • luazip.zip contains the files 'luazip.h', 'luazip.c', 'Makefile', 'README'
Below is a small sample code displaying the basic use of the library.
local zip = require "zip"

local zfile, err = zip.open('luazip.zip')

-- print the filenames of the files inside the zip
for file in zfile:files() do
    print(file.filename)
end

-- open README and print it
local f1, err = zfile:open('README')
local s1 = f1:read("*a")
print(s1)

f1:close()
zfile:close()

-- open d.txt inside c.zip
local d, err = zip.openfile('a/b/c/d.txt')
assert(d, err)
d:close()

-- open d2.txt inside b2.ext2
local d2, err = zip.openfile('a2/b2/c2/d2.txt', "ext2")
assert(d2, err)
d2:close()

-- open d3.txt inside a3.ext3
local d3, err = zip.openfile('a3/b3/c3/d3.txt', {"ext2", "ext3"})
assert(d3, err)
d3:close()