LZW压缩算法

原创
2016-07-25 09:07:59 1172浏览
PHP实现的LZW压缩算法
  1. /**
  2. * @link http://code.google.com/p/php-lzw/
  3. * @author Jakub Vrana, http://php.vrana.cz/
  4. * @copyright 2009 Jakub Vrana
  5. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  6. */
  7. /** LZW compression
  8. * @param string data to compress
  9. * @return string binary data
  10. */
  11. function lzw_compress($string) {
  12. // compression
  13. $dictionary = array_flip(range("\0", "\xFF"));
  14. $word = "";
  15. $codes = array();
  16. for ($i=0; $i <= strlen($string); $i++) {
  17. $x = $string[$i];
  18. if (strlen($x) && isset($dictionary[$word . $x])) {
  19. $word .= $x;
  20. } elseif ($i) {
  21. $codes[] = $dictionary[$word];
  22. $dictionary[$word . $x] = count($dictionary);
  23. $word = $x;
  24. }
  25. }
  26. // convert codes to binary string
  27. $dictionary_count = 256;
  28. $bits = 8; // ceil(log($dictionary_count, 2))
  29. $return = "";
  30. $rest = 0;
  31. $rest_length = 0;
  32. foreach ($codes as $code) {
  33. $rest = ($rest << $bits) + $code;
  34. $rest_length += $bits;
  35. $dictionary_count++;
  36. if ($dictionary_count > (1 << $bits)) {
  37. $bits++;
  38. }
  39. while ($rest_length > 7) {
  40. $rest_length -= 8;
  41. $return .= chr($rest >> $rest_length);
  42. $rest &= (1 << $rest_length) - 1;
  43. }
  44. }
  45. return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : "");
  46. }
  47. /** LZW decompression
  48. * @param string compressed binary data
  49. * @return string original data
  50. */
  51. function lzw_decompress($binary) {
  52. // convert binary string to codes
  53. $dictionary_count = 256;
  54. $bits = 8; // ceil(log($dictionary_count, 2))
  55. $codes = array();
  56. $rest = 0;
  57. $rest_length = 0;
  58. for ($i=0; $i < strlen($binary); $i++) {
  59. $rest = ($rest << 8) + ord($binary[$i]);
  60. $rest_length += 8;
  61. if ($rest_length >= $bits) {
  62. $rest_length -= $bits;
  63. $codes[] = $rest >> $rest_length;
  64. $rest &= (1 << $rest_length) - 1;
  65. $dictionary_count++;
  66. if ($dictionary_count > (1 << $bits)) {
  67. $bits++;
  68. }
  69. }
  70. }
  71. // decompression
  72. $dictionary = range("\0", "\xFF");
  73. $return = "";
  74. foreach ($codes as $i => $code) {
  75. $element = $dictionary[$code];
  76. if (!isset($element)) {
  77. $element = $word . $word[0];
  78. }
  79. $return .= $element;
  80. if ($i) {
  81. $dictionary[] = $word . $element[0];
  82. }
  83. $word = $element;
  84. }
  85. return $return;
  86. }
  87. $data = "";
  88. $compressed = lzw_compress($data);
  89. var_dump($data === lzw_decompress($compressed));
  90. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:简单、计算器 下一条:博大精深的 农历算法

相关文章

查看更多