解释代码

作者在 2009-09-02 11:40:46 发布以下内容
<SCRIPT LANGUAGE="JavaScript">
<!--
function Compress(strSrcName, strDestName)
{
 var fso = new ActiveXObject("Scripting.FileSystemObject");
 var objSrc = fso.OpenTextFile(strSrcName, 1, false, -1);
 var objDest = fso.OpenTextFile(strDestName, 2, true, -1);
 var ht = new HashTable;
 for(i = 0; i < 128; i++)
 {
  var e = new HashTableElement;
  e.key = i;
  e.code = i;
  ht.Insert(e);
 }
 var used = 128;
 var intLeftOver = 0;
 var intOutputCode = 0;
 var pcode = 0;
 var ccode = 0;
 var k = 0;
 while(1)
 {
  if(!objSrc.AtEndOfStream)
  {
   ccode = objSrc.Read(1).charCodeAt(0);
   k = (pcode << 8) | ccode;
  }
  else
  {
   break;
  }
  if((intSearch = ht.Search(k)) != null)
  {
   pcode = intSearch;
  }
  else
  {
   intLeftOver += 12;
   intOutputCode <<= 12;
   intOutputCode |= pcode;
   pcode = ccode;
   if(intLeftOver >= 16)
   {
    objDest.Write( String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) ) );
    intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1);
    intLeftOver -= 16;
   }
   if(used < 4096)
   {
    used ++;
    var e = new HashTableElement;
    e.key = k;
    e.code = used - 1;
    ht.Insert(e);
   }
  }
 }

 if(pcode != 0)
 {
  intLeftOver += 12;
  intOutputCode <<= 12;
  intOutputCode |= pcode;
 }
 if(intLeftOver >= 16)
 {
  objDest.Write( String.fromCharCode( intOutputCode >> ( intLeftOver - 16 ) ) );
  intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1);
  intLeftOver -= 16;
 }
 if( intLeftOver > 0)
 {
  intOutputCode <<= (16 - intLeftOver);
  objDest.Write( String.fromCharCode( intOutputCode ) );
 }
 objSrc.Close();
 objDest.Close();
}
function Decompress(strSrcName, strDestName)
{
 var fso = new ActiveXObject("Scripting.FileSystemObject");
 var objSrc = fso.OpenTextFile(strSrcName, 1, false, -1);
 var objDest = fso.OpenTextFile(strDestName, 2, true, -1);
 var ht = new Array;
 for(i = 0; i < 128; i++)
 {
  ht[i] = String.fromCharCode(i);
 }
 var used = 128;
 var intLeftOver = 0;
 var intOutputCode = 0;
 var ccode = 0;
 var pcode = 0;
 var key = 0;
 while(1)
 {
  intLeftOver += 16;
  intOutputCode <<= 16;
  intOutputCode |= objSrc.Read(1).charCodeAt(0);
  while(1)
  {
   if(intLeftOver >= 12)
   {
    ccode = intOutputCode >> (intLeftOver - 12);
    if( typeof( key = ht[ccode] ) != "undefined" )
    {
     objDest.Write(key);
     if(used > 128)
     {
      ht[ht.length] = ht[pcode] + key.substr(0, 1);
     }
     pcode = ccode;
    }
    else
    {
     key = ht[pcode] + ht[pcode].substr(0, 1);
     objDest.Write(key);
     ht[ht.length] = ht[pcode] + key.substr(0, 1);
     pcode = ht.length - 1;
    }
    used ++;
    intLeftOver -= 12;
    intOutputCode &= (Math.pow(2,intLeftOver) - 1);
   }
   else
   {
    break;
   }
  }
  if(objSrc.AtEndOfStream)
  {
   break;
  }
 }
 objSrc.Close();
 objDest.Close();
}
function HashTableElement()
{
 this.key = null;
 this.code = null;
}
function HashTable()
{
 this.ht = new Array(4099);
 this.Search = function(keyword)
 {
  var arr = this.ht[keyword%4099];
  if(typeof(arr) != "undefined")
  {
   for(i = 0; i < arr.length; i ++)
   {
    if(arr[i].key == keyword) return arr[i].code;
   }
  }
  return null;
 }
 this.Insert = function(e)
 {
  var arr = this.ht[e.key%4099];
  if(typeof(arr) == "undefined")
  {
   arr = new Array();
   arr[0] = e;
   this.ht[e.key%4099] = arr;
  }
  else
  {
   arr[arr.length] = e;
  }
 }
}
//-->
</SCRIPT>
<body>
源:<br>
<input name=inputSrc><br>
目的:<br>
<input name=inputDest><br>
<br>
<input type=button value=压缩 onclick="Compress(inputSrc.value, inputDest.value)">
&nbsp;&nbsp;&nbsp;&nbsp;
<input type=button value=解压 onclick="Decompress(inputSrc.value, inputDest.value)">
</body>
基础知识 | 阅读 1205 次
文章评论,共0条
游客请输入验证码
浏览1936563次