XML can be compress (LZW) to reduce file size

Lempel–Ziv–Welch (LZW) Compression Method

LZW is a type of lossless compression. Lossless compression is a compression method, compressing data without eliminating some of original data. Basically data will be compressed using a mathematical approach that prevents loss of data. This is a good compression method to compress sensitive type data where programmers need all data to be presented for further manipulation. E.g. data represented in XML.

Wiki definiton;

“Lossless data compression is a class of data compression algorithms that allows the exact original data to be reconstructed from the compressed data. The term lossless is in contrast to lossy data compression, which only allows an approximation of the original data to be reconstructed in exchange for better compression rates .”

 The algo for LZW was translated into AS2.0

// Use compress_fp6() instead of compress() if exporting to flash player 6

class LZW
{
 // Change this variable to output an xml safe string
 private static var xmlsafe:Boolean = false;
 private function LZW()
 {
 }
 public static function compress(str:String):String
 {
  var dico:Array = new Array();
  var skipnum:Number = xmlsafe?5:0;
  for (var i = 0; i < 256; i++)
  {
   dico[String.fromCharCode(i)] = i;
  }
  if (xmlsafe)
  {
   dico["<"] = 256;
   dico[">"] = 257;
   dico["&"] = 258;
   dico["\""] = 259;
   dico["'"] = 260;
  }
  var res:String = “”;
  var txt2encode:String = str;
  var splitStr:Array = txt2encode.split(“”);
  var len:Number = splitStr.length;
  var nbChar:Number = 256+skipnum;
  var buffer:String = “”;
  for (var i = 0; i <= len; i++)
  {
   var current = splitStr[i];
   if (dico[buffer + current] !== undefined)
   {
    buffer += current;
   }
   else
   {
    res += String.fromCharCode(dico[buffer]);
    dico[buffer + current] = nbChar;
    nbChar++;
    buffer = current;
   }
  }
  return res;
 }
 public static function decompress(str:String):String
 {
  var dico:Array = new Array();
  var skipnum:Number = xmlsafe?5:0;
  for (var i = 0; i < 256; i++)
  {
   var c:String = String.fromCharCode(i);
   dico[i] = c;
  }
  if (xmlsafe)
  {
   dico[256] = “<”;
   dico[257] = “>”;
   dico[258] = “&”;
   dico[259] = “\”";
   dico[260] = “‘”;
  }
  var txt2encode:String = str;
  var splitStr:Array = txt2encode.split(“”);
  var length:Number = splitStr.length;
  var nbChar:Number = 256+skipnum;
  var buffer:String = “”;
  var chaine:String = “”;
  var result:String = “”;
  for (var i = 0; i < length; i++)
  {
   var code:Number = txt2encode.charCodeAt(i);
   var current:String = dico[code];
   if (buffer == "")
   {
    buffer = current;
    result += current;
   }
   else
   {
    if (code <= 255+skipnum)
    {
     result += current;
     chaine = buffer + current;
     dico[nbChar] = chaine;
     nbChar++;
     buffer = current;
    }
    else
    {
     chaine = dico[code];
     if (chaine == undefined) chaine = buffer + buffer.slice(0,1);
     result += chaine;
     dico[nbChar] = buffer + chaine.slice(0, 1);
     nbChar++;
     buffer = chaine;
     
    }
   }
  }
  return result;
 }
 
 public static function compress_fp6(str:String):String
 {
  var dico:Array = new Array();
  var skipnum:Number = xmlsafe?5:0;
  for (var i = 0; i < 256; i++)
  {
   dico[String(i)] = i;
  }
  if (xmlsafe)
  {
   var let = String(new String("<").charCodeAt(0));
   var grt = String(new String(">").charCodeAt(0));
   var amp = String(new String("&").charCodeAt(0));
   var bsl = String(new String("\"").charCodeAt(0));
   var apo = String(new String("'").charCodeAt(0));
   dico[let] = 256;
   dico[grt] = 257;
   dico[amp] = 258;
   dico[bsl] = 259;
   dico[apo] = 260;
  }
  var res:String = "";
  var txt2encode:String = str;
  var splitStr:Array = txt2encode.split("");
  var len:Number = splitStr.length;
  var nbChar:Number = 256+skipnum;
  var buffer:Array = new Array();
  for (var i = 0; i <= len; i++)
  {
   var current = splitStr[i];
   if (buffer.length == 0)
   var xstr = String(current.charCodeAt(0));
   else
   var xstr = buffer.join("-") + "-" + String(current.charCodeAt(0));
   if (dico[xstr] !== undefined)
   {
    buffer.push(current.charCodeAt(0));
   }
   else
   {
    res += String.fromCharCode(dico[buffer.join("-")]);
    dico[xstr] = nbChar;
    nbChar++;
    delete buffer;
    buffer = new Array();
    buffer.push(current.charCodeAt(0));
   }
  }
  return res;
 }
}

AS file can be found in the following site:

http://www.razorberry.com/blog/archives/2004/08/22/lzw-compression-methods-in-as2/

Credits given to shoe-box.org weblog & lalex.com weblog.

~TBT~

This entry was posted in Development, Programming. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>