最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP LZW Binary Decompression Function - Stack Overflow

programmeradmin3浏览0评论

I've been looking on the internets and couldn't find an LZW depression implementation in PHP that works with the data outputted by these javascript functions:

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("");
}

I really just need a depression algorithm in PHP that can work with the pression javascript function above.

The lzw_encode function above encodes "This is a test of the pression function" as "This Ă a test ofĈhe prĊsion functěn"

The libraries I've found are either buggy (/) or don't take input of UTC characters.

Any help would be greatly appreciated,

Thanks!

I've been looking on the internets and couldn't find an LZW depression implementation in PHP that works with the data outputted by these javascript functions:

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("");
}

I really just need a depression algorithm in PHP that can work with the pression javascript function above.

The lzw_encode function above encodes "This is a test of the pression function" as "This Ă a test ofĈhe prĊsion functěn"

The libraries I've found are either buggy (http://code.google./p/php-lzw/) or don't take input of UTC characters.

Any help would be greatly appreciated,

Thanks!

Share Improve this question asked Apr 26, 2012 at 23:47 xd44xd44 8413 gold badges10 silver badges15 bronze badges 2
  • 1 Why not use the JS from link? There are ready made PHP implementations for that online. Eg: link. – BogdanM Commented Sep 25, 2013 at 12:14
  • Why is i=1 in here: for (var i=1; i<data.length; i++) {? Should it not be 0? – BogdanM Commented Sep 25, 2013 at 12:18
Add a ment  | 

2 Answers 2

Reset to default 3

I've ported and tested it for you to 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));
}

There is now a PHP extension for this!

lzw_depress_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');
发布评论

评论列表(0)

  1. 暂无评论