z3
TOC »
Description
A gzip (RFC1951 + RFC1952) compression and decompression library
Author
Oskar Schirmer and felix winkelmann
Repository
This egg is hosted on the CHICKEN Subversion repository:
https://anonymous@code.call-cc.org/svn/chicken-eggs/release/5/z3
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
Requirements
Documentation
Provides functions to read and write compressed data using the RFC 1951 (DEFLATE) and RFC 1952 (gzip) compression standards. This extension includes code from Oskar Schirmers excellent z3lib library.
RFC1952 is popularly used in the gzip file format and in compression of replies from web servers. RFC1951 (DEFLATE) is the underlying compression technique which is also used by PKZIP. NOTE: RFC1952 is essentially a file wrapper around a RFC1951 stream. Attempting to use the RFC1952 interfaces on RFC1951 data or vice versa will result in errors. Errors occurring in inflation/deflation procedures will result in a composite condition of the kinds exn and z3.
Raw data interface - RFC1951
- z3:encode-init #!key buffer buffer-size tellwhen thrmin thrmax initialgrant preferlongerprocedure
Initializes RFC1951 compression of data into a memory buffer. Various keyword arguments can be supplied to control compression:
- buffer
- The target buffer for the compressed data.
- buffer-size
- maximal size of the compressed data.
- tellwhen
- Frequency for code size estimation (0 for none, i.e. full block usage).
- thrmin, thrmax
- Threshold for block close decision.
- initialgrant
- For threshold comparison, all but the first slice are asumed to be incremented by this value to roughly compensate RFC1951 block dynamic table size.
- preferlonger
- When non-zero, the compressor will try to find a longer match at n+1 and prefer it over a previous match at n.
Returns a z3 handle.
- z3:encodeprocedure
Encode data (a string) into a memory buffer using RFC1951 compression and returns the number of bytes written. If less data has been commpressed than given in the call, invoke z3:encode repeatedly with the remaining data. Returns #f when finished. Each time some compressed data is" available, the one-argument procedure RECEIVER is called with a string containing a chunk of compressed data.
- z3:decode-initprocedure
Initialize an in-memory RFC1951 decompression and return a z3 handle for it.
- z3:decode Z3HANDLE RECEIVER BUFFER #!optional LENGTHprocedure
Decode the RFC1951 compressed data in BUFFER (a string) and return the number of bytes decompressed.
- z3:handle? Xprocedure
Returns #t if X is a z3 handle or #f otherwise.
Buffer interface - RFC1951
- z3:encode-buffer STRING #!optional START ENDprocedure
Encodes the data in STRING using RFC1951 encoding with optional start- and end-positions. Returns a freshly allocated string that contains the compressed data.
- z3:encode-buffer-to-blob STRING #!optional START ENDprocedure
Like z3:encode-buffer, but returns a freshly-allocated blob containing the compressed data.
- z3:decode-buffer STRING #!optional START ENDprocedure
Decodes the RFC1951 compressed data in STRING and returns the uncompressed data. Optional start- and end-position may be given. STRING may be a string or a blob.
File-system interface - RFC1952
- z3:encode-file FILENO #!key level filename comment ostype extraprocedure
Open a RFC1952 compressed "gzip" file (specified by the file-descriptor in FILENO) for encoded data. The keyword arguments have the following meaning:
- level
- Desired compression-level in the range 1 .. 9, or 0 for selecting the default compression level
- filename
- The name of the file to compress, will be included in the gzip-file header
- comment
- An arbitrary comment
- ostype
- Operating system indicator byte (defaults to -1)
- extra
- Extra data to be encoded in the header"
Returns a z3 file-handle.
- z3:write-encoded Z3FHANDLE DATA #!optional LENGTHprocedure
Writes DATA (a string) into a RFC1952 (gzip) compressed file, optionally limited in length. Before a file has been encoded completely, this procedure must be called once more with DATA being #f to indicate that the compression process is finished.
- z3:decode-file FILENOprocedure
Returns a z3 file-handle for reading the gzip file designated by the file-descriptor FILENO.
- z3:read-decoded Z3FHANDLE #!optional LENGTHprocedure
Reads a chunk of decoded data from a gzip compressed file. The length of the chunk can be given as an optional argument and defaults to 4096 bytes. Returns a string or the end-of-file object.
- z3:file-handle? Xprocedure
Returns #t if X is a z3 file handle or #f otherwise.
- z3:file-handle-fileno Z3FHANDLEprocedure
Returns the file-descriptor associated with a z3 file-handle.
Port interface - RFC1952
- z3:open-compressed-input-file FILENAMEprocedure
Opens a RFC1952 compressed "gzip" input file and returns a port that automatically decompresses the data as it is read.
- z3:open-compressed-output-file FILENAME #!key level comment ostype extraprocedure
Creates a RFC1952 compressed "gzip" file and returns an output-port. The keyword arguments have the same meaning as for the z3:encode-file procedure.
Example
(import (chicken io) (chicken port) z3) (let ((port (z3:open-compressed-output-file "hello.gz"))) (with-output-to-port port (lambda () (write-line "hello"))) (close-output-port port)) (let ((port (z3:open-compressed-input-file "hello.gz"))) (with-input-from-port port (lambda () (print (read-line)))) (close-input-port port))
Version
- 2.0
- Ported to CHICKEN 5
- 1.45
- Removed optional destination argument to z3:encode-buffer, fixed case when compression exceeds temporary destination buffer
- 1.42
- use bind instead of easyffi [Ivan Raikov]
- 1.41
- add z3:encode-buffer-to-blob, extend z3:decode-buffer to accept blob input [Jim Ursetto]
- 1.40
- fixed dependencies and converted documentation to wiki format
- 1.39
- Fix eof bug in port interface
- 1.38
- Fix minor bug in error handling and clarify documentation (trac tickets 81, 82)
- 1.37
- ported to CHICKEN 4
- 1.36
- Should compile now on Windows with mingw
- 1.35
- Fixed bug in z3:encode-buffer [reported by Mario Domenech Goulart]
- 1.34
- Added whole-buffer encoding/decoding
- 1.33
- Fixed setup script and meta file [Thanks to Peter Busser]
- 1.32
- Fixed bug in z3lib code, related to 64-bit architectures
- 1.31
- Fixed bug in z3:encode
- 0.9
- Initial release
License
Copyright (c) 2005-2007 Oskar Schirmer. All rights reserved.
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 ASIS, 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.