getCompressedFilename ($input), "wb"); while ($buf = fread($in, 200000)) { $str = $this->lzw_compress ($buf); fwrite ($out, pack ('I', strlen ($str))); fwrite ($out, $str); } } function decompress ($input) { $in = fopen ($input, "rb"); $out = fopen ($this->getDecompressedFilename ($input), "wb"); while ($size = fread ($in, 4)) { $x = unpack ('I', $size); $size = $x[1]; $buf = fread ($in, $size); fwrite ($out, $this->lzw_decompress ($buf)); } } function lzw_compress ($string) { $save_error_reporting = error_reporting (error_reporting () & ~E_NOTICE); $dictionary = array_flip(range("\0", "\xFF")); $word = ""; $codes = array(); for ($i=0; $i <= strlen($string); $i++) { $x = $string[$i]; if (strlen($x) && isset($dictionary[$word . $x])) { $word .= $x; } elseif ($i) { $codes[] = $dictionary[$word]; $dictionary[$word . $x] = count($dictionary); $word = $x; } } $dictionary_count = 256; $bits = 8; $return = ""; $rest = 0; $rest_length = 0; foreach ($codes as $code) { $rest = ($rest << $bits) + $code; $rest_length += $bits; $dictionary_count++; if ($dictionary_count > (1 << $bits)) { $bits++; } while ($rest_length > 7) { $rest_length -= 8; $return .= chr($rest >> $rest_length); $rest &= (1 << $rest_length) - 1; } } error_reporting ($save_error_reporting); return $return . ($rest_length ? chr($rest << (8 - $rest_length)) : ""); } function lzw_decompress($binary) { $dictionary_count = 256; $bits = 8; $codes = array(); $rest = 0; $rest_length = 0; for ($i=0; $i < strlen($binary); $i++) { $rest = ($rest << 8) + ord($binary[$i]); $rest_length += 8; if ($rest_length >= $bits) { $rest_length -= $bits; $codes[] = $rest >> $rest_length; $rest &= (1 << $rest_length) - 1; $dictionary_count++; if ($dictionary_count > (1 << $bits)) { $bits++; } } } $save_error_level = error_reporting (error_reporting () & ~E_NOTICE & ~E_WARNING); $dictionary = range("\0", "\xFF"); $return = ""; foreach ($codes as $i => $code) { unset ($element); if (isset ($dictionary [$code])) { $element = $dictionary [$code]; } if (!isset($element)) { $element = $word . $word[0]; } $return .= $element; if ($i) { $dictionary[] = $word . $element[0]; } $word = $element; } error_reporting ($save_error_level); return $return; } }