chickadee » zlib » zlib-compressing-output-port

zlib-compressing-output-port port #!rest optionsprocedure

Returns an output-port to which arbitrary data can be written. Its compressed form will be written to, usually with some delay, the supplied output-port port. It is important to call close-output-port on the returned port. Doing so does not close the supplied port.

The keyword arguments options are supplied to deflateInit2. They are as follows:

  • level: compression level in the range [0-9] (from fastest to best). 0 means no compression is applied and the data is simply wrapped in the associated headers. The current default is level 6.
  • method: Currently only 'deflated is supported.
  • window-bits: Specifies the history buffer size in base two logarithm. The current default is 15. Supported ranges are:
    • [8..15] for zlib RFC1950 streams.
    • [-15..-8] for raw deflate RFC1951 streams.
    • [25..31] for gzip RFC1952 streams.
  • mem-level: Specifies memory consumption for internal state, more is faster. Valid ranges are [1..9]. The current default is 8.
  • strategy: Valid symbols are
    • 'default (or #f)
    • 'filtered
    • 'huffman-only
    • 'rle
    • 'fixed
  • set-finalizer: A procedure called on the resulting output-port. The default is (lambda (x) (set-finalizer! x deflate-free!)). Supply your own if set-finalizer!'s overhead is undesirable.
  • buffer: A string, often heavily mutated, used for internal transfers. (make-string 4096) is the default.

If some of the supplied options are invalid, zlib throws (error ...). Note that flush-output-port currently has no affect. Although zlib has support to flush the stream, providing immediate available data, this degrades the compression performance. As this is usually not the desired outcome of flush-output-port, we leave it as a no-op.

Here is an example to produce zlib compressed data:

(string->blob
 (call-with-output-string
  (lambda (os)
    (let ((op (zlib-compressing-output-port os)))
      (display "hello world" op)
      (close-output-port op)))))
;; => #${789ccb48cdc9c95728cf2fca4901001a0b045d}
;; echo  789ccb48cdc9c95728cf2fca4901001a0b045d | xxd -plain -revert | file -
/dev/stdin: zlib compressed data