8

我一直在网上寻找,在 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 函数将“This is a test of the compression function”编码为“This Ă a test ofĈhe comprĊsion functěn”

我发现的库要么有问题(http://code.google.com/p/php-lzw/),要么不输入 UTC 字符。

任何帮助将不胜感激,

谢谢!

4

2 回答 2

3

我已经为您移植并测试了它到 PHP:

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));
}
于 2013-09-27T11:53:42.113 回答
0

现在有一个PHP 扩展

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');
于 2016-02-18T18:57:12.537 回答