=head1 NAME zip - read data in zip files (LuaZIP) =head1 OVERVIEW LuaZip is a lightweight Lua (L) 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 (L) 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 (L). In fact, the API is so similar that parts of this manual are extracted from the Lua manual, copyrighted by Tecgraf, PUC-Rio. =head1 EXAMPLE Suppose we have the following file hierarchy: /a /b c.zip /a2 b2.ext2 /a3.ext3 /luazip.zip =over 4 =item c.zip contains the file 'd.txt' =item b2.ext2 is a zip file containing the file 'c2/d2.txt' =item a3.ext3 is a zip file containing the file 'b3/c3/d3.txt' =item luazip.zip contains the files 'luazip.h', 'luazip.c', 'Makefile', 'README' =back 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() =head1 DOWNLOAD LuaZip source can be downloaded from its Lua Forge (L) page. If you are using LuaBinaries (L) Release 2 a Windows binary version of LuaZip can be found at the same LuaForge page. =head1 INSTALLATION LuaZip follows the package model (L) for Lua 5.1, therefore it should be "installed". Refer to Compat-5.1 configuration (L) section about how to install the compiled binary properly. If you are using Unix you may need to download zziplib 0.13.36 (L) If you are using Windows, the binary version of LuaZip includes zziplib (C). =head1 REFERENCE =head2 C 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 C, there is no C parameter, as the only supported mode is "read". =head2 C 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 C, there is no C 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 I 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. =head2 C zfile:close () This function closes a zfile opened by C =head2 C zfile:files () Returns an iterator function that returns a new table containing the following information each time it is called: =over 4 =item C the full path of a file =item C the compressed size of the file in bytes =item C the uncompressed size of the file in bytes =back =head2 C zfile:open (filename) This function opens a file that is stored inside the zip file opened by C. The filename may contain the full path of the file contained inside the zip. The directory separator must be '/'. Unlike C, there is no C parameter, as the only supported mode is "read". =head2 C file:read (format1, ...) Reads a C 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: =over 4 =item C<"*a"> reads the whole file, starting at the current position. On end of file, it returns the empty string. =item C<"*l"> reads the next line (skipping the end of line), returns nil on end of file. This is the default format. =item I reads a string with up to that number of characters, returning nil on end of file. =back Unlike the standard I/O read, the format C<"*n"> is not supported. =head2 C file:seek ([whence] [, offset]) Sets and gets the file position, measured from the beginning of the file, to the position given by C plus a base specified by the string C, as follows: =over 4 =item C base is position 0 (beginning of the file); =item C base is current position; =item C base is end of file; =back In case of success, function C 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 C is C<"cur">, and for C is 0. Therefore, the call C returns the current file position, without changing it; the call C sets the position to the beginning of the file (and returns 0); and the call C sets the position to the end of the file, and returns its size. =head2 C file:close () This function closes a file opened by C. =head2 C 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. =head1 VERSION Current version is 1.2.2. It was developed for Lua 5.0. Version 1.2.2 follows the package model (L) for Lua 5.1 (see section Installation for more details). =head1 HISTORY =over 4 =item Version 1.2.2 [24/Mar/2006] Minor fixes on makefile and config =item Version 1.2.1 [08/Jun/2005] Minor bug fixes =item Version 1.2.0 [01/Dec/2004] =item Version 1.1.3 [25/Jun/2004] (L) First Public Release =back =head1 CREDITS LuaZip was designed and coded by Danilo Tuler as part of the Kepler Project (L). =head1 CONTACT 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 (L). =head1 LICENSE 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 (L) software. Its licenses are compatible with GPL (L). LuaZip is not in the public domain and the Kepler Project (L) 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 E 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. ~~~~~