(rfc zlib) - zlib compression library

Library (rfc zlib)

This library provides the binding for zlib compression library. For now, it only provides the highest level APIs.

Condition Type &zlib-error
Function zlib-error? obj
Function condition-zlib-stream zlib-error

Subcondition of &error.

This condition is raised when zlib process is finished unsuccessfully. You can obtain the cause z-stream with the condition-zlib-stream procedure and get the detail message with the zlib-error-message procedure. When error occurred, however, it is raised with message-condition and it has the error message with. So you can simply get it with condition-messageprocedure.

Function zlib-error-message z-stream

Z-stream must be z-stream object.

Retrieve error message from z-stream. If z-stream does not have any error message and this procedure is called, the behaviour is unspecified.

Function open-deflating-output-port sink :key compression-level buffer-size window-bits memory-level _

_ strategy dictionary owner?

Sink must be binary output port. Creates a custom binary output port. The port deflates its input and put the result to sink. With the optional keys, you can specify the following conditions:

compression-level

You can specify an exact integer between 1 and 9 (inclusive) to compression-level. When it is omitted, a default compression level is used, which is usually 6. The following constants are defined to specify _compression-level_conveniently:

buffer-size

It specifies the buffer size of the port in bytes. The default is 4096.

window-bits

It specifies the size of the window in exact integer. Typically the value should be between 8 and 15, inclusive, and it specifies the base two logarithm of the window size used in compression. Larger number yields better compression ratio, but more memory usage. The default value is 15.

memory-level

It specifies how much memory should be allocated to keep the internal state during compression. 1 means smallest memory which causes slow and less compression. 9 means fastest and best compression with the largest amount of memory. The default value is 8.

strategy

To fine tune compression algorithm, you can use the strategy argument. The following constants are defined as the valid value as strategy.

The default strategy, suitable for most ordinary data.

Constant Z_FILTERED

Suitable for data generated by filters (or predictors). Filtered data consists mostly of small values with a somewhat compress them better. The effect of this is to force more huffman coding and less string matching.

Force to use huffman encoding only.

Constant Z_RLE

This is designed to be almost as fast as Z_HUFFMAN_ONLY, but gives better compression for PNG image data.

Constant Z_FIXED

This prevents the use of dynamic huffman codes, allowing for a simpler decoder for special applications. The choice of strategy only affects compression ratio and speed. Any choice produces correct and decompressable data.

dictionary

You can give an initial dictionary to the dictionary argument to be used in compression. The compressor and decompressor must use exactly the same dictionary.

owner?

If this argument is specified true value, the created port automatically closes the given output port sink when it is closed.

Function open-inflating-input-port source :key buffer-size window-bits dictionary owner?

Source must be a binary input port.

The open-inflating-input-port creates a custom binary input port, which reads compressed binary data from the given port source and decompresses the read data. When source port supports both port-position and set-port-position! then the procedure will set source position to offset of used bytes length when the created custom port is being closed. Thus if the source port contains mixture of deflated data and non deflated data then the custom port will only read deflated data and won't forward the original port position beyond it no matter how big the buffer-size is specified.

The meaning of buffer-size is the same as open-deflating-output-port.

The meaning of window-bits is almost the same, except if a value increased by 32 is given, the inflating port automatically detecs whether the source stream is zlib or gzip by its header.

If the input data is compressed with specified dictionary, the same dictionary must be given to dictionary argument. Otherwise &zlib-error condition is raised.

Function inflate-bytevector bv opts ...
Function deflate-bytevector bv opts ...

Inflate/deflate given bytevector bv respectively.

These are convenient procedures. It uses open-inflating-input-port or open-deflating-output-port to inflate/deflate.

The opts will be passed to underlying procedures.

Function crc32 bv :optional (checksum 0)
Function adler32 bv :optional (checksum 0)

Returns CSC32/Adler32 checksum of bytevector bv, respectively.

If optional checksum is given, then returned checksum is an update of checksum by bv.