Compress - Maple Help

StringTools

 Compress
 compress a string or byte sequence

 Calling Sequence Compress(S) Compress(S, n) Compress(S, output=f)

Parameters

 S - string, Array(datatype=integer[1]), or list(integer) n - (optional) positive integer number of bytes f - (optional) output format, either rtable or list

Description

 • The Compress(S) command takes the given string, byte list, or integer[1] array and compresses it into a lossless, more compact format.
 • By default the return value is an Array with datatype=integer[1].  The values in the array range from -128 to 127 as integer[1] is a signed integer format.
 • When output=list is specified the return value is a list of integers in the range 0 to 256.
 • Both the array output format and the list output format are suitable for use with readbytes and writebytes.
 • The optional value n indicates that only the first n bytes should be compressed.  If n is unspecified, or if n <= 0 or if n exceeds the actual number of bytes, then all of S is compressed.
 • The compression algorithm is taken from the zlib library written by Jean-loup Gailly and Mark Adler.  See http://zlib.net

Examples

Compress a string and retrieve it.

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $r≔\mathrm{Compress}\left("hello world"\right)$
 ${r}{≔}\left[\begin{array}{cccccccccccccccccccc}{120}& {-100}& {-53}& {72}& {-51}& {-55}& {-55}& {87}& {40}& {-49}& {47}& {-54}& {73}& {97}& {0}& {0}& {30}& {104}& {4}& {93}\end{array}\right]$ (1)
 > $\mathrm{Uncompress}\left(r,'\mathrm{output}'='\mathrm{string}'\right)$
 ${"hello world"}$ (2)
 > $r≔\mathrm{Compress}\left("hello world"\right)$
 ${r}{≔}\left[\begin{array}{cccccccccccccccccccc}{120}& {-100}& {-53}& {72}& {-51}& {-55}& {-55}& {87}& {40}& {-49}& {47}& {-54}& {73}& {97}& {0}& {0}& {30}& {104}& {4}& {93}\end{array}\right]$ (3)
 > $\mathrm{Uncompress}\left(r,'\mathrm{output}'='\mathrm{string}'\right)$
 ${"hello world"}$ (4)

Compress the Thue-Morse word on 10^7 (ten million) letters by 99%.

 > $\mathrm{tm}≔\mathrm{ThueMorse}\left({10}^{7}\right):$
 > $\mathrm{compressed}≔\mathrm{Compress}\left(\mathrm{tm}\right)$
 ${\mathrm{compressed}}{≔}\left[{120}{,}{-100}{,}{-20}{,}{-111}{,}{75}{,}{106}{,}{-61}{,}{80}{,}{0}{,}{-60}{,}{-82}{,}{-12}{,}{124}{,}{-1}{,}{-53}{,}{-107}{,}{-46}{,}{44}{,}{-102}{,}{69}{,}{3}{,}{41}{,}{36}{,}{66}{,}{-47}{,}{44}{,}{12}{,}{-2}{,}{36}{,}{-106}{,}{-28}{,}{57}{,}{-41}{,}{117}{,}{-82}{,}{115}{,}{-82}{,}{-17}{,}{-29}{,}{-36}{,}{-50}{,}{127}{,}{-82}{,}{-18}{,}{-17}{,}{-1}{,}{126}{,}{-10}{,}{-24}{,}{-9}{,}{127}{,}{-3}{,}{-17}{,}{-65}{,}{-17}{,}{127}{,}{-106}{,}{-9}{,}{110}{,}{-1}{,}{87}{,}{-9}{,}{125}{,}{-6}{,}{-9}{,}{-89}{,}{-5}{,}{105}{,}{-66}{,}{-35}{,}{127}{,}{-5}{,}{-69}{,}{-9}{,}{-81}{,}{-9}{,}{-45}{,}{124}{,}{-38}{,}{-65}{,}{-34}{,}{79}{,}{-13}{,}{105}{,}{-1}{,}{122}{,}{63}{,}{-51}{,}{-73}{,}{-5}{,}{111}{,}{127}{,}{-9}{,}{-2}{,}{-11}{,}{126}{,}{-102}{,}{111}{,}{-9}{,}{-33}{,}{\dots }{,}{\text{⋯ 92487 Array entries not shown}}\right]$ (5)
 > $1.-\frac{\mathrm{numelems}\left(\mathrm{compressed}\right)}{\mathrm{length}\left(\mathrm{tm}\right)}$
 ${0.9907413000}$ (6)