设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 5457|回复: 1
收起左侧

[PHP/ASP/JSP] PHP7实现的超简单数据无损压缩算法解压算法 采用2~256进制任意转换+专用编解码算法

[复制链接]
发表于 2019-4-11 10:11:40 | 显示全部楼层 |阅读模式



  1. <?php


  2. function bin_carry_convert($data,$old,$new){
  3.         $old &= 255;
  4.         $new &= 255;
  5.         $k = 0;
  6.         $return = array();
  7.         $len = strlen($data);
  8.         $data_array = array();
  9.                 if($old <=0 || $new <= 0 || $len <= 0){
  10.                         return;
  11.                 }elseif($old == $new) {
  12.                         return $data;
  13.                         }
  14.         $old2 = $old+1;$new2 = $new+1;
  15.        
  16.                 for($i=0;$i<$len;$i++){
  17.                         $data_array[$i] = ord($data[$i]);
  18.                         if($data_array[$i] > $old){
  19.                                 return;
  20.                         }
  21.                 }
  22.         while($len >= 1){
  23.                 $j = 0;
  24.                 for($i=0;$i<$len;$i++){
  25.                         $j = $j * $old2 + $data_array[$i];
  26.                         $data_array[$i] = floor($j / $new2);
  27.                         $j %= $new2;
  28.                 }
  29.                         $return[$k++] = chr($j);
  30.                         $j = 0;
  31.                 while($j < $len && $data_array[$j] == 0){
  32.                         $j++;
  33.                 }
  34.                         array_splice($data_array,0,$j);
  35.                         $len=sizeof($data_array);
  36.                 }
  37.         return implode(array_reverse($return));
  38. }


  39. function logic2bin($logic){
  40.         $len = sizeof($logic);
  41.         if($len %8) return;
  42.         $i=0;
  43.         $char =0;
  44.         $return = null;
  45.         foreach($logic as $bit){
  46.                 $bit &=1;
  47.                 $char|= $bit ? 1<< $i : 0;
  48.                         $i++;
  49.                         $i&=7;
  50.                 if(!$i) {
  51.                 $return.=chr($char);
  52.                 $char = 0;
  53.                 }
  54.         }
  55.         return $return;
  56. }

  57. function bin2logic($bin){
  58.         $len = strlen($bin);
  59.         $return = array();
  60.         $k=0;
  61.         for($i=0;$i<$len;$i++){
  62.         $char=ord($bin[$i]);
  63.                 for($j=0;$j<8;$j++){
  64.                 $return[$k++]=$char&(1<<$j) ? 1 :0;
  65.                 }
  66.         }
  67.         return $return;

  68. }

  69. function appear_char($data){
  70.         $return = array();
  71.         $len = strlen($data);
  72.         $acn = 0;
  73.         for($i=0;$i<256;$i++){
  74.                 $return[$i] = 0;
  75.         }
  76.         for($i=0;$i<$len;$i++){
  77.                 $char = ord($data[$i]);
  78.                 if(!$return[$char]){
  79.                 $return[$char] = 1;
  80.                 $acn++;
  81.                 if($acn >= 256) break;
  82.                 }
  83.         }
  84.         return logic2bin($return);
  85. }

  86. function bin_encode($data,&$key){
  87.         $len = strlen($data);
  88.         $key = appear_char($data);
  89.         $key_array = bin2logic($key);
  90.         $key_table = array();
  91.         $j = 0;
  92.         $return = null;
  93.         for($i=0;$i<256;$i++){
  94.                 if($key_array[$i]){
  95.                 $key_table[$i] = $j++;
  96.                 }
  97.         }
  98.         for($i=0;$i<$len;$i++){
  99.         $return .= chr($key_table[ord($data[$i])]);
  100.         }
  101.         return $return;
  102. }

  103. function bin_decode($data,$key){
  104.         $len = strlen($data);
  105.         $key_array = bin2logic($key);
  106.         $key_table = array();
  107.         $return = null;
  108.         $j = 0;
  109.         for($i=0;$i<256;$i++){
  110.                 if($key_array[$i]){
  111.                 $key_table[$j++] = $i;
  112.                 }
  113.         }
  114.         for($i=0;$i<$len;$i++){
  115.         $return .= chr($key_table[ord($data[$i])]);
  116.         }
  117.        
  118.         return $return;

  119. }

  120. function max_bin($bin){
  121.         $len = strlen($bin);
  122.         $return = 0;
  123.         for($i=0;$i<$len;$i++){
  124.         $return = max($return,ord($bin[$i]));
  125.                 if($return == 255){
  126.                         break;
  127.                 }
  128.         }
  129.         return $return;

  130. }

  131. function within_encode($data){
  132.         $carry = max_bin($data);
  133.         $carry = $carry ? $carry : 1;
  134.         return chr($carry).bin_carry_convert(chr(1).$data,$carry,255);
  135. }

  136. function within_decode($data){
  137.         return substr(bin_carry_convert(substr($data,1),255,ord($data[0])),1);
  138. }

  139. //=====================公开函数===================


  140. function compression_encode($data){        //数据压缩
  141.         $data = bin_encode($data,$key);
  142.         return $key . within_encode($data);
  143. }

  144. function compression_decode($data){        //数据解压
  145.         $key = substr($data,0,32);
  146.         $data = substr($data,32);
  147.         return bin_decode(within_decode($data),$key);
  148. }


  149. //===================测试函数======================


  150. /*
  151. 该源码与以下使用同一种算法,且互相兼容。
  152. https://www.fedcba9876543210.com/thread-1464-1-1.html
  153. */

  154. function test(){
  155. $data = file_get_contents("Test.txt");
  156. $data2 = compression_encode($data);
  157. $len = strlen($data);
  158. $len2 = strlen($data2);

  159. echo "压缩前:{$len} 压缩后:{$len2}\r\n";

  160. echo compression_decode($data2) == $data ? "OK!!" : "Err!";
  161. }
  162. test();
  163. ?>



复制代码

相关帖子

发表于 2019-4-11 11:15:03 来自手机 | 显示全部楼层
看不懂的到此一游
您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-4-19 19:00 , Processed in 0.682039 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表