我一直在寻找互联网,并且在PHP中找不到与这些javascript函数输出的数据一起使用的LZW解压缩实现:
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("");
}
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;
debugger;
for (var i=1; i<data.length; 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("");
}
我真的只需要PHP中的解压缩算法,它可以使用上面的压缩javascript函数。
上面的lzw_encode函数将“这是对压缩函数的测试”编码为“这是对压缩函数的测试”
我发现的图书馆要么是错误的(http://code.google.com/p/php-lzw/),要么不要输入UTC字符。
非常感谢任何帮助,
谢谢!
答案 0 :(得分:3)
我已经为你移植并测试了它:
function lzw_decode($s) {
mb_internal_encoding('UTF-8');
$dict = array();
$currChar = mb_substr($s, 0, 1);
$oldPhrase = $currChar;
$out = array($currChar);
$code = 256;
$phrase = '';
for ($i=1; $i < mb_strlen($s); $i++) {
$currCode = implode(unpack('N*', str_pad(iconv('UTF-8', 'UTF-16BE', mb_substr($s, $i, 1)), 4, "\x00", STR_PAD_LEFT)));
if($currCode < 256) {
$phrase = mb_substr($s, $i, 1);
} else {
$phrase = $dict[$currCode] ? $dict[$currCode] : ($oldPhrase.$currChar);
}
$out[] = $phrase;
$currChar = mb_substr($phrase, 0, 1);
$dict[$code] = $oldPhrase.$currChar;
$code++;
$oldPhrase = $phrase;
}
var_dump($dict);
return(implode($out));
}
答案 1 :(得分:0)
现在有一个layoutY!
lzw_decompress_file('3240_05_1948-1998.tar.Z', '3240_05_1948-1998.tar');
$archive = new PharData('/tmp/3240_05_1948-1998.tar');
mkdir('unpacked');
$archive->extractTo('unpacked');