ZSTD_compressBound($len); $ret['max_len'] = $max; $comp = str_repeat(' ', $max); $clen = self::$ffi->ZSTD_compress($comp, $max, $src, $len, 6); if (self::$ffi->ZSTD_isError($clen)) { throw new \RuntimeException("Compression fails"); } $ret['out_len'] = $clen; if (file_put_contents($out, substr($comp, 0, $clen)) !== $clen) { throw new \RuntimeException("Save fails"); } return $ret; } public static function decompress($in, $out) { self::init(); $ret = []; $comp = file_get_contents($in); if ($comp === false) { throw new \RuntimeException("Read fails"); } $clen = strlen($comp); $ret['in_len'] = $clen; $max = self::$ffi->ZSTD_decompressBound($comp, $clen); $ret['max_len'] = $max; $unco = str_repeat(' ', $max); $ulen = self::$ffi->ZSTD_decompress($unco, $max, $comp, $clen); if (self::$ffi->ZSTD_isError($clen)) { throw new \RuntimeException("Compression fails"); } $ret['out_len'] = $ulen; if (file_put_contents($out, substr($unco, 0, $ulen)) !== $ulen) { throw new \RuntimeException("Save fails"); } return $ret; } }