2011-11-12 17 views

回答

6

一個新的解決方案來壓縮數據的維基百科文章,現在可供選擇:jszip

+1

_JSZip是一個用於創建,閱讀並用一個可愛而簡單的API編輯.zip文件。它聽起來不像OP所要求的。也許代碼示例會說服。 – Phil

3

我知道沒有gzip實現,但還有其他壓縮方法可供您使用。

這將LZW編碼使用JavaScript字符串:

// lzw-encode a string 
function lzw_encode(s) { 
    var dict = {}; 
    var data = (s + "").split(""); 
    var out = []; 
    var currChar; 
    var phrase = data[0]; 
    var code = 256; 
    for (var i=1; i<data.length; i++) { 
     currChar=data[i]; 
     if (dict[phrase + currChar] != null) { 
      phrase += currChar; 
     } 
     else { 
      out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 
      dict[phrase + currChar] = code; 
      code++; 
      phrase=currChar; 
     } 
    } 
    out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 
    for (var i=0; i<out.length; i++) { 
     out[i] = String.fromCharCode(out[i]); 
    } 
    return out.join(""); 
} 
+0

來自:http://stackoverflow.com/questions/294297/javascript-implementation-of-gzip –

相關問題