LZW compression algorithm

Source: Internet
Author: User
The LZW compression algorithm implemented by PHP
  1. /**
  2. * @link http://code.google.com/p/php-lzw/
  3. * @author Jakub Vrana, http://php.vrana.cz/
  4. * @copyright 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 ("n", "\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 ("n", "\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. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.