use*_*276 7 php gzip chunked inflate
在PHP中进行gzip deflate请求后,我收到了偏移块中的缩减字符串,如下所示
示例缩短为显示格式:
00001B4E
¾”kŒj…Øæ’ìÑ«F1ìÊ`+ƒQì¹UÜjùJƒZ\µy¡ÓUžGr‡J&=KLËÙÍ~=ÍkR
0000102F
ñÞœÞôΑüo[¾”+’Ñ8#à»0±R-4VÕ’n›êˆÍ.MCŽ…ÏÖr¿3M—èßñ°r¡\+
00000000
Run Code Online (Sandbox Code Playgroud)
因为分块格式,我无法膨胀.用Hex编辑器手动删除偏移并读取gzip存档后,我可以确认数据没有损坏.我想知道是否有一个正确的方法来解析这个分块的gzip缩减响应到一个可读的字符串?
我可能能够将这些偏移分开并将数据连接在一起以调用gzinflate,但似乎必须有一种更简单的方法.
Dav*_*dom 10
缩小响应的适当方法大致如下:
initialise string to hold result
for each chunk {
check that the stated chunk length equals the string length of the chunk
append the chunk data to the result variable
}
Run Code Online (Sandbox Code Playgroud)
这是一个方便的PHP函数为你做(固定):
function unchunk_string ($str) {
// A string to hold the result
$result = '';
// Split input by CRLF
$parts = explode("\r\n", $str);
// These vars track the current chunk
$chunkLen = 0;
$thisChunk = '';
// Loop the data
while (($part = array_shift($parts)) !== NULL) {
if ($chunkLen) {
// Add the data to the string
// Don't forget, the data might contain a literal CRLF
$thisChunk .= $part."\r\n";
if (strlen($thisChunk) == $chunkLen) {
// Chunk is complete
$result .= $thisChunk;
$chunkLen = 0;
$thisChunk = '';
} else if (strlen($thisChunk) == $chunkLen + 2) {
// Chunk is complete, remove trailing CRLF
$result .= substr($thisChunk, 0, -2);
$chunkLen = 0;
$thisChunk = '';
} else if (strlen($thisChunk) > $chunkLen) {
// Data is malformed
return FALSE;
}
} else {
// If we are not in a chunk, get length of the new one
if ($part === '') continue;
if (!$chunkLen = hexdec($part)) break;
}
}
// Return the decoded data of FALSE if it is incomplete
return ($chunkLen) ? FALSE : $result;
}
Run Code Online (Sandbox Code Playgroud)