The Lempel-Ziv-Welch (LZW) algorithm provides loss-less data compression.

You can read a complete description of it in the   Wikipedia article   on the subject.   It was patented, but it entered the public domain in 2004.




fcn lzwCompress(uncompressed){ // text-->list of 12 bit ints
   dictionary:=(256).pump(Dictionary(),fcn(n){ return(n.toChar(),n) });
   w,compressed:="",List();
   foreach c in (uncompressed){
      wc:=w+c;
      if(dictionary.holds(wc)) w=wc;
      else{
   compressed.append(dictionary[w]);  // 12 bits
   dictionary[wc]=dictionary.len();
   w=c;
      }
   }
   if(w) compressed.append(dictionary[w]);
   compressed
} 
fcn lzwUncompress(compressed){ // compressed data-->text
   dictionary:=(256).pump(Dictionary(),fcn(n){ return(n,n.toChar()) });
   w,decommpressed:=dictionary[compressed[0]],Data(Void,w);
   foreach k in (compressed[1,*]){
      if(dictionary.holds(k))      entry:=dictionary[k];
      else if(k==dictionary.len()) entry:=w+w[0];
      else throw(Exception.ValueError("Invalid compressed data"));
      decommpressed.append(entry);
      dictionary.add(dictionary.len(),w+entry[0]);
      w=entry;
   }
   decommpressed.text
}