LUALoriotPro WEB site

LoriotPro scripting documentation
Extensions for LUA language

Table of contents Search function

Print the current page Mail this  link UTUBE Channel version française

LUA_ZIP

This example explains how to add the compression function of the luazip 1.2.1 package to LoriotPro. The library is available on the Kepler project web site http://www.keplerproject.org/ .

  1. Download the file luazip-1.2.1-win32.zip.
  2. The file luazip-1.2.1-win32.zip contains two DLL, zip.dll and zip1.dll. We rename the the zip.dll in lua_zip.dll to keep the original file.
  3. Copy both dll in the ./bin directory of LoriotPro
  4. Check with the dependy utilityr thatno other dll are missing.


You can note the name of the initialization function : luaopen_zip

Extract of the luazip documentation.


Copyright © 2003-2005 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.

LuaZip
Reading files inside zip files

LuaZip is a lightweight Lua 5.0 extension library that can be used to read files stored inside zip files. It uses zziplib 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. In fact, the API is so similar that parts of this manual are extractions from the Lua manual, copyrighted by Tecgraf, PUC-Rio.
LuaZip source is distributed as a pair of C source and header files. The distribution provides a Makefile prepared to compile the library and install it. The file config should be edited to suit the needs of the aimed platform.
Installation
LuaZip follows the package model for Lua 5.1, therefore it should be "installed". Refer to Compat-5.1 configuration section about how to install the compiled binary properly.
Reference
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".
zip.openfile (filename [, extensions]])
This functions opens a file and returns a file handle. In case of errors it returns nil and an error message. Unlike io.open, there is no mode parameter, as the only supported mode is "read".
This functions implements a virtual file system based on optionally compressed files. Instead of simply looking for a file at a given path, this function goes recursivly up through all path separators ("/") looking for zip file there. If it finds a zip file, this functions use the remaining path to open the asked 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 sequence extensions.
zfile:close ()
This function closes a zfile opened by zip.open
zfile:files ()
Returns an iterator function that, each time it is called, returns a new table containing the following information:
• filename: the full path of a file
• compressed_size: the compressed size of the file in bytes
• uncompressed_size: the uncompressed size of the file in bytes
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 (format1, ...)
Reads the file 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": reads the whole file, starting at the current position. On end of file, it returns the empty string.
• "*l": reads the next line (skipping the end of line), return nil on end of file. This is the default format.
• number: reads a string with up to that number of characters, returning nil on end of file.

Unlike the standard read, the format "*n" is not supported.
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: base is position 0 (beginning of the file);
• cur: base is current position;
• end: base is end of file;
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 a string error. 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 ()
This function closes a file opened by zfile:open.
file:lines ()
Returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction
for line in file:lines() do ... end
will iterate over all lines of the file.

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 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()

To use this library, you should first call the lp.LoadLibrary function.

Example :

lp_value = 0;
lp_buffer ="error";

lib,init=lp.LoadLibrary(lp.GetPath().."/lua_zip.dll","luaopen_zip");
if (lib) then
init();
zfile,err=zip.open (lp.GetPath().."/luazip-1.2.1-win32.zip");
if zfile then
for file in zfile:files() do
lp.Print(file.filename,"\n")
end
zfile:close();
end
end
lp_buffer ="ok";


www.loriotpro.com