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.
Compress a sequence of bytes.
strc = deflate(str)
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.
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
Uncompress the result of deflate.
str = inflate(strc)
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.
Read deflated data and uncompress them.
(data, nin) = zread(fd, n) (data, nin) = zread(fd)
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.
Compress a sequence of bytes and write the result.
nout = zwrite(fd, data)
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.