LZW壓縮演算法

WBOY
發布: 2016-07-25 09:07:59
原創
1589 人瀏覽過
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("", "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("", "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. ?>
复制代码


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!