0

0

LZW压缩算法

php中文网

php中文网

发布时间:2016-07-25 09:07:59

|

1643人浏览过

|

来源于php中文网

原创

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 $x = $string[$i];
  17. if (strlen($x) && isset($dictionary[$word . $x])) {
  18. $word .= $x;
  19. } elseif ($i) {
  20. $codes[] = $dictionary[$word];
  21. $dictionary[$word . $x] = count($dictionary);
  22. $word = $x;
  23. }
  24. }
  25. // convert codes to binary string
  26. $dictionary_count = 256;
  27. $bits = 8; // ceil(log($dictionary_count, 2))
  28. $return = "";
  29. $rest = 0;
  30. $rest_length = 0;
  31. foreach ($codes as $code) {
  32. $rest = ($rest $rest_length += $bits;
  33. $dictionary_count++;
  34. if ($dictionary_count > (1 $bits++;
  35. }
  36. while ($rest_length > 7) {
  37. $rest_length -= 8;
  38. $return .= chr($rest >> $rest_length);
  39. $rest &= (1 }
  40. }
  41. return $return . ($rest_length ? chr($rest }
  42. /** LZW decompression
  43. * @param string compressed binary data
  44. * @return string original data
  45. */
  46. function lzw_decompress($binary) {
  47. // convert binary string to codes
  48. $dictionary_count = 256;
  49. $bits = 8; // ceil(log($dictionary_count, 2))
  50. $codes = array();
  51. $rest = 0;
  52. $rest_length = 0;
  53. for ($i=0; $i $rest = ($rest $rest_length += 8;
  54. if ($rest_length >= $bits) {
  55. $rest_length -= $bits;
  56. $codes[] = $rest >> $rest_length;
  57. $rest &= (1 $dictionary_count++;
  58. if ($dictionary_count > (1 $bits++;
  59. }
  60. }
  61. }
  62. // decompression
  63. $dictionary = range("\0", "\xFF");
  64. $return = "";
  65. foreach ($codes as $i => $code) {
  66. $element = $dictionary[$code];
  67. if (!isset($element)) {
  68. $element = $word . $word[0];
  69. }
  70. $return .= $element;
  71. if ($i) {
  72. $dictionary[] = $word . $element[0];
  73. }
  74. $word = $element;
  75. }
  76. return $return;
  77. }
  78. $data = "";
  79. $compressed = lzw_compress($data);
  80. var_dump($data === lzw_decompress($compressed));
  81. ?>
复制代码


相关专题

更多
Java 项目构建与依赖管理(Maven / Gradle)
Java 项目构建与依赖管理(Maven / Gradle)

本专题系统讲解 Java 项目构建与依赖管理的完整体系,重点覆盖 Maven 与 Gradle 的核心概念、项目生命周期、依赖冲突解决、多模块项目管理、构建加速与版本发布规范。通过真实项目结构示例,帮助学习者掌握 从零搭建、维护到发布 Java 工程的标准化流程,提升在实际团队开发中的工程能力与协作效率。

11

2026.01.12

c++主流开发框架汇总
c++主流开发框架汇总

本专题整合了c++开发框架推荐,阅读专题下面的文章了解更多详细内容。

106

2026.01.09

c++框架学习教程汇总
c++框架学习教程汇总

本专题整合了c++框架学习教程汇总,阅读专题下面的文章了解更多详细内容。

64

2026.01.09

学python好用的网站推荐
学python好用的网站推荐

本专题整合了python学习教程汇总,阅读专题下面的文章了解更多详细内容。

139

2026.01.09

学python网站汇总
学python网站汇总

本专题整合了学python网站汇总,阅读专题下面的文章了解更多详细内容。

13

2026.01.09

python学习网站
python学习网站

本专题整合了python学习相关推荐汇总,阅读专题下面的文章了解更多详细内容。

19

2026.01.09

俄罗斯手机浏览器地址汇总
俄罗斯手机浏览器地址汇总

汇总俄罗斯Yandex手机浏览器官方网址入口,涵盖国际版与俄语版,适配移动端访问,一键直达搜索、地图、新闻等核心服务。

93

2026.01.09

漫蛙稳定版地址大全
漫蛙稳定版地址大全

漫蛙稳定版地址大全汇总最新可用入口,包含漫蛙manwa漫画防走失官网链接,确保用户随时畅读海量正版漫画资源,建议收藏备用,避免因域名变动无法访问。

480

2026.01.09

php学习网站大全
php学习网站大全

精选多个优质PHP入门学习网站,涵盖教程、实战与文档,适合零基础到进阶开发者,助你高效掌握PHP编程。

52

2026.01.09

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号