Ajax获取gzip,ajax - JavaScript implementation of Gzip - Stack Overflow

Edit There appears to be a better LZW solution that handles Unicode strings correctly at http://pieroxy.net/blog/pages/lz-string/index.html (Thanks to pieroxy in the comments).

I don't know of any gzip implementations, but the jsolait library (the site seems to have gone away) has functions for LZW compression/decompression. The code is covered under the LGPL.

// LZW-compress 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

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[i] = String.fromCharCode(out[i]);

}

return out.join("");

}

// Decompress an LZW-encoded string

function lzw_decode(s) {

var dict = {};

var data = (s + "").split("");

var currChar = data[0];

var oldPhrase = currChar;

var out = [currChar];

var code = 256;

var phrase;

for (var i=1; i

var currCode = data[i].charCodeAt(0);

if (currCode < 256) {

phrase = data[i];

}

else {

phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);

}

out.push(phrase);

currChar = phrase.charAt(0);

dict[code] = oldPhrase + currChar;

code++;

oldPhrase = phrase;

}

return out.join("");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值