ZLib

This section describes functions which compress and uncompress sequences of bytes, such as text. Often, these sequences present redundancy which can be removed to produce a shorter sequence, while still being able to revert to the initial one.

The ZLib extension is based on zlib by J.L. Gailly and M. Adler, whose work is gratefully acknowledged. To preserve their terminology, compression is performed with function deflate, and uncompression with inflate.

deflate

Compress a sequence of bytes.

Syntax

strc = deflate(str)

Description

deflate(str) produces a string strc which is usually shorter than it argument str. String str can be reconstructed with inflate using only strc. deflate and inflate process any sequence of bytes (8-bit words); their input argument can be any array. However, their shape and their type are lost (the result of inflate is always a row vector of characters) and their elements are restored modulo 256.

Depending on the data, compression rates of 2 or more are typical. Sequences without redundancy (such as random numbers or the result of deflate) can produce a result slightly larger than the initial sequence.

Example

str = repmat('abcd ef ', 1, 1000);
length(str)
  8000
strc = deflate(str);
length(strc)
  43
str = repmat('abcd ef ', 1, 1000);
strc = deflate(str);
str2 = inflate(strc);
str === str2
  true

To compress objects which are not sequence of bytes, you can use dumpvar and str2obj to convert them to and from a textual representation:

A = repmat(600, 2, 2)
  A =
    600  600
    600  600
inflate(deflate(A))
  1x4 uint8 array
    88  88  88  88
str = dumpvar(A);
str2obj(deflate(inflate(str)))
  600  600
  600  600

See also

inflate

inflate

Uncompress the result of deflate.

Syntax

str = inflate(strc)

Description

inflate(strc) uncompresses strc to undo the effect of compress. The output is always a character string with one row, whose characters are coded on one byte.

See also

deflate

zread

Read deflated data and uncompress them.

Syntax

(data, nin) = zread(fd, n)
(data, nin) = zread(fd)

Description

zread(fd, n) reads up to n bytes from file descriptor fd, uncompresses them using the inflate algorithm, and returns the result as a row vector of type uint8. An optional second output argument is set to the number of bytes which have actually been read; it is less than n if the end-of-file is reached.

With a single input argument, zread(fd) reads data until the end of the file.

Note that you must read a whole segment of deflated data with one call. Inflation is restarted every time zread is called.

See also

zwrite, inflate

zwrite

Compress a sequence of bytes and write the result.

Syntax

nout = zwrite(fd, data)

Description

zwrite(fd, data) compresses the array data, of type int8 or uint8, and writes the result to the file descriptor fd.

Note that you must write a whole segment of data with one call. Deflation is restarted every time zwrite is called.

See also

zread, deflate


Copyright 2001-2008, Calerga.
All rights reserved.