Skip to content

PlugwiseBV/lzlib

 
 

Repository files navigation

*************************************************************************
* Author    : Tiago Dionizio <tiago.dionizio@gmail.com>                 *
* Library   : lzlib - Lua 5.1 interface to access zlib library functions*
*                                                                       *
* 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.                *
*************************************************************************

To use this library you need zlib library.
You can get it from http://www.gzip.org/zlib/


Loading the library:

    If you built the library as a loadable package
        [local] zlib = require 'zlib'

    If you compiled the package statically into your application, call
    the function "luaopen_zlib(L)". It will create a table with the zlib
    functions and leave it on the stack.

-- zlib functions --

zlib.version()
	returns zlib version

zlib.adler32([int adler32, string buffer])
	Without any parameters, returns the inicial adler32 value.

	Call to update the adler32 value, adler is the current value, buffer is passed
	to adler32 zlib function and the updated value is returned.

zlib.crc32([int crc32, string buffer])
	Same as zlib.adler32.

zlib.compress(string buffer [, int level] [, int method] [, int windowBits] [, int memLevel] [, int strategy])
	Return a string containing the compressed buffer according to the given parameters.

    Documentation for the parameters (taken from zlib.h). The integers the macro defines represent
    are noted in square brackets.

         The method parameter is the compression method. It must be Z_DEFLATED [8] in
       this version of the library.

         The windowBits parameter is the base two logarithm of the window size
       (the size of the history buffer). It should be in the range 8..15 for this
       version of the library. Larger values of this parameter result in better
       compression at the expense of memory usage. The default value is 15 if
       deflateInit is used instead.

         windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
       determines the window size. deflate() will then generate raw deflate data
       with no zlib header or trailer, and will not compute an adler32 check value.

         windowBits can also be greater than 15 for optional gzip encoding. Add
       16 to windowBits to write a simple gzip header and trailer around the
       compressed data instead of a zlib wrapper. The gzip header will have no
       file name, no extra data, no comment, no modification time (set to zero),
       no header crc, and the operating system will be set to 255 (unknown).  If a
       gzip stream is being written, strm->adler is a crc32 instead of an adler32.

         The memLevel parameter specifies how much memory should be allocated
       for the internal compression state. memLevel=1 uses minimum memory but
       is slow and reduces compression ratio; memLevel=9 uses maximum memory
       for optimal speed. The default value is 8. See zconf.h for total memory
       usage as a function of windowBits and memLevel.

       [Here is the relevant section from zconf.h:]
             The memory requirements for deflate are (in bytes):
                      (1 << (windowBits+2)) +  (1 << (memLevel+9))
           that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
           plus a few kilobytes for small objects. For example, if you want to reduce
           the default memory requirements from 256K to 128K, compile with
               make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
           Of course this will generally degrade compression (there's no free lunch).
           
             The memory requirements for inflate are (in bytes) 1 << windowBits
           that is, 32K for windowBits=15 (default value) plus a few kilobytes
           for small objects.

         The strategy parameter is used to tune the compression algorithm. Use the
       value Z_DEFAULT_STRATEGY [0] for normal data, Z_FILTERED [1] for data produced by a
       filter (or predictor), Z_HUFFMAN_ONLY [2] to force Huffman encoding only (no
       string match), or Z_RLE [3] to limit match distances to one (run-length
       encoding). Filtered data consists mostly of small values with a somewhat
       random distribution. In this case, the compression algorithm is tuned to
       compress them better. The effect of Z_FILTERED is to force more Huffman
       coding and less string matching; it is somewhat intermediate between
       Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as
       fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The
       strategy parameter only affects the compression ratio but not the
       correctness of the compressed output even if it is not set appropriately.
       Z_FIXED [4] prevents the use of dynamic Huffman codes, allowing for a simpler
       decoder for special applications.

zlib.decompress(string buffer [, int windowBits])
	Return the decompressed stream after processing the given buffer.


zlib.deflate{
	sink: function | { write: function [, close: function, flush: function ] },
	compression level, [Z_DEFAULT_COMPRESSION]
	method, [Z_DEFLATED]
	windowBits, [15]
	memLevel, [8]
	strategy, [Z_DEFAULT_STRATEGY]
    dictionary, [""]
}
	Return a deflate stream.

	stream:write(...)
		Write each parameter into the stream.

	stream:flush(['sync' | 'full' | 'finish'])
		Flush output for deflate streams.

	stream:close()
		Close the stream.

zlib.inflate{
	source: string | function | { read: function, close: function },
	windowBits: number, [15]
    dictionary, [""]
}
	Return an inflate stream.

deflate and inflate streams can be used almost like a normal file:

	stream:read((number | '*l' | '*a')*)
		Return a value for each given parameter. Returns a line when
		no format is specified.

	stream:lines()
		Return iterator that returns a line each time it is called, or nil
		on end of file.
	stream:close()
		Close the stream.

About

Lua bindings to the ZLib compression library

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages