Zlib is a widespread library implementing the RFC1950 (zlib wrapper), RFC1951 (deflate stream) and RFC1952 (gzip wrapper) compression standards. The SWI-Prolog binding is a foreign library that creates a compressed stream as a wrapper around a normal stream. Implemented this way, it can perform a wide variety of tasks:
- Read/write gzip compatible files
- Setup standard compressed stream communication
- Realise in-memory compression or decompression
- Deal with streams holding embedded compressed objects
The core predicate of the library is zopen/3.
The remainder of the functionality of
defined in Prolog and can be used as a starting point for other
high-level primitives. See also
ztest.pl providing test and
demo code. This file is part of the source distribution.
Part of the functionality of this library can also be realised using the pipe interface and the gzip program. For example, a gziped file can also be opened in Prolog using the code below.
... open(pipe('gunzip < file.gz'), read, In), ...
The advantage of this library over using an external program for such tasks is enhanced platform independence and reduced time to open a file. Platform independence is improved as we do not have to worry about availability of the gunzip utility and we do not have to worry about shell and filename quoting issues. While the above replacement code works well on most modern Unix systems, it only works with special precautions on Windows.1Install gunzip, deal with Windows path-names, the windows shell and quoting.
The library becomes unavoidable if we consider compressed network communication. Here we get the stream from tcp_open_socket/3. The library provides efficient creation of a compressed stream, as well as support for flushing output through the standard Prolog flush_output/1 call.