php实现的lzw压缩算法 (1 $bits++; } while ($rest_length > 7) { $rest_length -= 8; $return .= chr($rest >> $rest_length); $rest &= (1 } } return $return . ($rest_length ? chr($rest }/** lzw decompression* @param string compressed binary data* @return string original data*/function lzw_decompress($binary) { // convert binary string to codes $dictionary_count = 256; $bits = 8; // ceil(log($dictionary_count, 2)) $codes = array(); $rest = 0; $rest_length = 0; for ($i=0; $i $rest = ($rest $rest_length += 8; if ($rest_length >= $bits) { $rest_length -= $bits; $codes[] = $rest >> $rest_length; $rest &= (1 $dictionary_count++; if ($dictionary_count > (1 $bits++; } } } // decompression $dictionary = range(\0, \xff); $return = ; foreach ($codes as $i => $code) { $element = $dictionary[$code]; if (!isset($element)) { $element = $word . $word[0]; } $return .= $element; if ($i) { $dictionary[] = $word . $element[0]; } $word = $element; } return $return;}$data = ;$compressed = lzw_compress($data);var_dump($data === lzw_decompress($compressed));?>
复制代码