zip - read data in zip files (LuaZIP)
LuaZip is a lightweight Lua (http://www.lua.org) 5.0 extension library used to read files stored inside zip files. The API is very similar to the standard Lua I/O library API. It uses zziplib (http://zziplib.sourceforge.net) to do all the hard work.
The API exposed to Lua is very simple and very similiar to the usual file handling functions provided by the I/O Lua standard library (http://www.lua.org/manual/5.0/manual.html#5.6). In fact, the API is so similar that parts of this manual are extracted from the Lua manual, copyrighted by Tecgraf, PUC-Rio.
Suppose we have the following file hierarchy:
/a /b c.zip /a2 b2.ext2 /a3.ext3 /luazip.zip
Below is a small sample code displaying the basic use of the library.
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()
LuaZip source can be downloaded from its Lua Forge (http://luaforge.net/projects/luazip/files) page. If you are using LuaBinaries (http://luabinaries.luaforge.net) Release 2 a Windows binary version of LuaZip can be found at the same LuaForge page.
LuaZip follows the package model (http://www.keplerproject.org/compat/) for Lua 5.1, therefore it should be ``installed''. Refer to Compat-5.1 configuration (http://www.keplerproject.org/compat/manual.html#configuration) section about how to install the compiled binary properly.
If you are using Unix you may need to download zziplib 0.13.36 (http://zziplib.sourceforge.net)
If you are using Windows, the binary version of LuaZip includes
zziplib (zlib1.dll
).
zip.open
zip.open (filename)
This function opens a zip file and returns a new zip file handle. In
case of error it returns nil and an error message. Unlike io.open
,
there is no mode
parameter, as the only supported mode is
``read''.</dd>
zip.openfile
zip.openfile (filename [, extensions]])
This function opens a file and returns a file handle. In case of error
it returns nil and an error message. Unlike io.open
, there is no
mode
parameter, as the only supported mode is ``read''.
This function implements a virtual file system based on optionally compressed files. Instead of simply looking for a file at a given path, this function goes recursively up through all path separators (``/'') looking for zip files there. If it finds a zip file, this function use the remaining path to open the requested file.
The optional parameter extensions allows the use of file extensions other than .zip during the lookup. It can be a string corresponding to the extension or an indexed table with the lookup extensions sequence.
zfile:close
zfile:close ()
This function closes a zfile opened by zip.open
zfile:files
zfile:files ()
Returns an iterator function that returns a new table containing the following information each time it is called:
filename
compressed_size
uncompressed_size
zfile:open
zfile:open (filename)
This function opens a file that is stored inside the zip file opened
by zip.open
. The filename may contain the full path of the file
contained inside the zip. The directory separator must be '/'.
Unlike f:open
, there is no mode
parameter, as the only supported
mode is ``read''.
file:read
file:read (format1, ...)
Reads a file
according to the given formats, which specify what to
read.
For each format, the function returns a string with the characters read, or nil if it cannot read data with the specified format. When called without formats, it uses a default format that reads the entire next line (see below).
The available formats are:
"*a"
"*l"
Unlike the standard I/O read, the format "*n"
is not supported.
file:seek
file:seek ([whence] [, offset])
Sets and gets the file position, measured from the beginning of the
file, to the position given by offset
plus a base specified by the
string whence
, as follows:
set
cur
end
In case of success, function seek
returns the final file position,
measured in bytes from the beginning of the file. If this function
fails, it returns nil, plus an error string. The default value for
whence
is "cur"
, and for offset
is 0. Therefore, the call
file:seek()
returns the current file position, without changing it;
the call file:seek("set")
sets the position to the beginning of the
file (and returns 0); and the call file:seek("end")
sets the
position to the end of the file, and returns its size.
file:close
file:close ()
This function closes a file opened by zfile:open
.
file:lines
file:lines ()
Returns an iterator function that returns a new line from the file each time it is called. Therefore, the construction
for line in file:lines() do ... end
will iterate over all lines of the file.
Current version is 1.2.2. It was developed for Lua 5.0.
Version 1.2.2 follows the package model (http://www.keplerproject.org/compat) for Lua 5.1 (see section Installation for more details).
LuaZip was designed and coded by Danilo Tuler as part of the Kepler Project (http://www.keplerproject.org).
For more information please contact (mailto:info-NO-SPAM-THANKS@keplerproject.org) us. Comments are welcome!
You can also reach other Kepler developers and users on the Kepler Project mailing list (http://luaforge.net/mail/).
LuaZip is free software and uses the same license as Lua 5.0.
LuaZip is free software: it can be used for both academic and commercial purposes at absolutely no cost. There are no royalties or GNU-like ``copyleft'' restrictions. LuaZip qualifies as Open Source (http://www.opensource.org/docs/definition.html) software. Its licenses are compatible with GPL (http://www.gnu.org/licenses/gpl.html). LuaZip is not in the public domain and the Kepler Project (http://www.keplerproject.org) keep its copyright. The legal details are below.
The spirit of the license is that you are free to use LuaZip for any purpose at no cost without having to ask us. The only requirement is that if you do use LuaZip, then you should give us credit by including the appropriate copyright notice somewhere in your product or its documentation.
The LuaZip library was designed and implemented by Danilo Tuler. Part of the code is a derived work from the Lua standard I/O library, copyrighted by Tecgraf, PUC-Rio.
~~~~~
Copyright © 2003-2006 The Kepler Project.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ``Software''), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
~~~~~