 Compress - Maple Help

ArrayTools

 Compress
 compress a byte sequence Calling Sequence Compress(data, opts) Parameters

 data - ByteArray or list of integers opts - (optional) equation of the form format = value Options

 • format = one of bzip2, gzip, zip, or zlib
 This option allows a compression format to be specified explicitly. Options are bzip2, gzip, zip, or zlib. The default is zlib. Description

 • The Compress(A) command takes the given byte array or list and compresses it into a lossless, more compact format. The return value is a ByteArray.
 • If A is a list, its contents must be integers between -128 and 127. Examples

Generate a ByteArray of number-theoretic data and compress it.

 > $\mathrm{OmegaArray}≔\mathrm{Array}\left(1..{10}^{5},\mathrm{NumberTheory}:-\mathrm{NumberOfPrimeFactors},\mathrm{datatype}=\mathrm{integer}\left[1\right]\right)$
 > $\mathrm{CompressedOmegaArray}≔\mathrm{ArrayTools}:-\mathrm{Compress}\left(\mathrm{OmegaArray}\right)$

Compare the size of the original with the compressed result.

 > $\mathrm{numelems}\left(\mathrm{OmegaArray}\right),\mathrm{numelems}\left(\mathrm{CompressedOmegaArray}\right)$
 ${100000}{,}{37013}$ (1)

Generate random data from a discrete uniform statistical distribution between -10 and 10 and compress it with zip format.

 > $\mathrm{data}≔\mathrm{Statistics}:-\mathrm{Sample}\left(\mathrm{DiscreteUniform}\left(-10,10\right),1000000\right)$
 > $\mathrm{original}≔\mathrm{convert}\left(\mathrm{data},\mathrm{ByteArray}\right)$
 > $\mathrm{compressed}≔\mathrm{ArrayTools}:-\mathrm{Compress}\left(\mathrm{original}\right)$

Compute the compression ratio for this example.

 > $1.-\frac{\mathrm{numelems}\left(\mathrm{compressed}\right)}{\mathrm{numelems}\left(\mathrm{original}\right)}$
 ${0.8718235000}$ (2)

Convert a string to a ByteArray and compress it using gzip.

 > $\mathrm{PaulCliffordText}≔"It was a dark and stormy night; the rain fell in torrents - except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets \left(for it is in London that our scene lies\right), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.":$
 > $\mathrm{original}≔\mathrm{StringTools}:-\mathrm{ToByteArray}\left(\mathrm{PaulCliffordText}\right)$
 > $\mathrm{compressed}≔\mathrm{ArrayTools}:-\mathrm{Compress}\left(\mathrm{original},\mathrm{format}=\mathrm{gzip}\right)$

Compute the compression ratio for this example.

 > $1.-\frac{\mathrm{numelems}\left(\mathrm{compressed}\right)}{\mathrm{numelems}\left(\mathrm{original}\right)}$
 ${0.2996941896}$ (3) Compatibility

 • The ArrayTools[Compress] command was introduced in Maple 2015.