Lua Module Lzo

lua-users home
wiki

Lzo is a Lua module for simple compression and decompression of data via the [miniLZO] library.

Description

Main goals of the module are:

Quick Tips

The module is very easy to use, there are only two functions:

lzo.compress(str)

lzo.decompress(str)

Both take a Lua string as single argument and return another Lua string. Strings passed to lzo.decompress must have a special format, as generated by lzo.compress, otherwise lzo.decompress will produce an error.

Example

Here's a quick example that opens a file, compressing it to another:

input = io.open("inputfile", "rb")

output = io.open("outputfile", "wb")



in_data = input:read("*a")

input:close()



out_data= lzo.compress(in_data)



output:write(out_data)

output:close()



print(string.len(in_data) .. " bytes compressed to " ..

      string.len(out_data).. " bytes")



-- Let the GC thrash away buffers

in_data = nil

out_data= nil

Extras

miniLZO includes a function to calculate checksums using the adler32 algorithm. In latests versions of the module, I interfaced this function too, so you can call it from Lua as:

lzo.adler(num)

The number (optional) is the initial value of the sum; if not given a default value of 0 is used.

Author

AdrianPerez

Downloads

At this moment it's only available as source code. Latest version is 0.4.

You will only need Lua 5.0 beta or above, as the miniLZO source is included in the package.

Current version was tested with MacOS X 10.2 (Jaguar) and Linux. As miniLZO is very portable, it should compile and work in nearly any operating system where Lua is capable of running.

Bug Patch

This code has bug when compiling in lua 5.0. This is modified version of luaLZO_open()... replace to this.


int luaLZO_open(lua_State* L)

{

	ASSERT(L);

	

	if (lzo_init() != LZO_E_OK) {

		luaL_error(L, "failed to initialize the mini-LZO (de)compressor");

	}



	static const luaL_reg lzolib[] = {

	  {"comp", luaLZO_compress},

	  {"decomp", luaLZO_decompress},

	  {"adler", luaLZO_adler},

	  {NULL, NULL}

	};



	luaL_openlib(L, "lzo", lzolib, 0);



	return 0;

}

- redpixel (redpixel_at_korea.com)

Thanks, I'll take care of this in the next release :) -- AdrianPerez


RecentChanges · preferences
edit · history
Last edited October 31, 2009 6:32 pm GMT (diff)