gzdecode

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

gzdecode解码 gzip 压缩字符串

说明

gzdecode(string $data, int $max_length = 0): string|false

此函数返回输入 data 的解码版本。

参数

data

要解码的数据,由 gzencode() 编码。

max_length

要解码数据的最大长度。

返回值

解码后的字符串,或者在失败时为 false

错误/异常

在失败的情况下,将发出 E_WARNING 级别错误。

参见

添加备注

用户贡献的笔记 6 notes

20
anonymous at dekho-ji dot com
11 年前
要解码/解压缩 PHP 代码中接收到的 HTTP POST 数据,请使用 HTTP POST GZIP/DEFLATE 压缩格式从 Java/Android 应用程序接收请求数据

1) 从 Java Android 应用程序发送到 PHP 的数据,使用 DeflaterOutputStream java 类,并在 PHP 中接收,如下所示
echo gzinflate( substr($HTTP_RAW_POST_DATA,2,-4) ) . PHP_EOL . PHP_EOL;

2) 从 Java Android 应用程序发送到 PHP 的数据,使用 GZIPOutputStream java 类,并在 PHP 代码中接收,如下所示
echo gzinflate( substr($HTTP_RAW_POST_DATA,10,-8) ) . PHP_EOL . PHP_EOL;

从 Java Android 端(API 级别 10+),以 DEFLATE 压缩格式发送数据
String body = "Lorem ipsum shizzle ma nizle";
URL url = new URL("http://www.url.com/postthisdata.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-encoding", "deflate");
conn.setRequestProperty("Content-type", "application/octet-stream");
DeflaterOutputStream dos = new DeflaterOutputStream(
conn.getOutputStream());
dos.write(body.getBytes());
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String decodedString = "";
while ((decodedString = in.readLine()) != null) {
Log.e("dump",decodedString);
}
in.close();

在 PHP 端(v 5.3.1),用于解压缩此 DEFLATE 数据的代码将是
echo substr($HTTP_RAW_POST_DATA,2,-4);

从 Java Android 端(API 级别 10+),以 GZIP 压缩格式发送数据

String body1 = "Lorem ipsum shizzle ma nizle";
URL url1 = new URL("http://www.url.com/postthisdata.php");
URLConnection conn1 = url1.openConnection();
conn1.setDoOutput(true);
conn1.setRequestProperty("Content-encoding", "gzip");
conn1.setRequestProperty("Content-type", "application/octet-stream");
GZIPOutputStream dos1 = new GZIPOutputStream(conn1.getOutputStream());
dos1.write(body1.getBytes());
dos1.flush();
dos1.close();
BufferedReader in1 = new BufferedReader(new InputStreamReader(
conn1.getInputStream()));
String decodedString1 = "";
while ((decodedString1 = in1.readLine()) != null) {
Log.e("dump",decodedString1);
}
in1.close();

在 PHP 端(v 5.3.1),用于解压缩此 GZIP 数据的代码将是
echo substr($HTTP_RAW_POST_DATA,10,-8);

有用的 PHP 代码,用于使用所有可用格式打印出压缩数据。

$data = "Lorem ipsum shizzle ma nizle";
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzcompress($data,$i))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzdeflate($data,$i))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_GZIP))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_DEFLATE))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";

希望这有帮助。如果这为您节省了大量的努力和时间,请点赞。
11
tomas at slax dot org
12 年前
我对压缩算法和格式没有深入了解,所以我不知道这是否在所有平台上都以相同的方式工作。但通过在我的 Linux 盒子上进行多次实验,我发现如何获取 gzdecoded 数据,而无需使用临时文件。如下所示

<?php
function gzdecode($data)
{
return
gzinflate(substr($data,10,-8));
}
?>

就是这样。只需剥离头和尾字节,您就可以获得用于膨胀的原始压缩数据。
9
katzlbtjunk at hotmail dot com
16 年前
发现并修复了 3 个错误
1. 当 gz 包含文件名时无法工作 - 已修复
2. 在 64 位架构上无法工作(校验和) - 已修复
3. 当 gz 包含注释时无法工作 - 无法验证。
返回一些错误(并非全部!)和文件名。
<?php function gzdecode($data,&$filename='',&$error='',$maxlength=null)
{
$len = strlen($data);
if (
$len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
$error = "Not in GZIP format.";
return
null; // Not GZIP format (See RFC 1952)
}
$method = ord(substr($data,2,1)); // Compression method
$flags = ord(substr($data,3,1)); // Flags
if ($flags & 31 != $flags) {
$error = "Reserved bits not allowed.";
return
null;
}
// NOTE: $mtime may be negative (PHP integer limitations)
$mtime = unpack("V", substr($data,4,4));
$mtime = $mtime[1];
$xfl = substr($data,8,1);
$os = substr($data,8,1);
$headerlen = 10;
$extralen = 0;
$extra = "";
if (
$flags & 4) {
// 2-byte length prefixed EXTRA data in header
if ($len - $headerlen - 2 < 8) {
return
false; // invalid
}
$extralen = unpack("v",substr($data,8,2));
$extralen = $extralen[1];
if (
$len - $headerlen - 2 - $extralen < 8) {
return
false; // invalid
}
$extra = substr($data,10,$extralen);
$headerlen += 2 + $extralen;
}
$filenamelen = 0;
$filename = "";
if (
$flags & 8) {
// C-style string
if ($len - $headerlen - 1 < 8) {
return
false; // invalid
}
$filenamelen = strpos(substr($data,$headerlen),chr(0));
if (
$filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
return
false; // invalid
}
$filename = substr($data,$headerlen,$filenamelen);
$headerlen += $filenamelen + 1;
}
$commentlen = 0;
$comment = "";
if (
$flags & 16) {
// C-style string COMMENT data in header
if ($len - $headerlen - 1 < 8) {
return
false; // invalid
}
$commentlen = strpos(substr($data,$headerlen),chr(0));
if (
$commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
return
false; // Invalid header format
}
$comment = substr($data,$headerlen,$commentlen);
$headerlen += $commentlen + 1;
}
$headercrc = "";
if (
$flags & 2) {
// 2-bytes (lowest order) of CRC32 on header present
if ($len - $headerlen - 2 < 8) {
return
false; // invalid
}
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
$headercrc = unpack("v", substr($data,$headerlen,2));
$headercrc = $headercrc[1];
if (
$headercrc != $calccrc) {
$error = "Header checksum failed.";
return
false; // Bad header CRC
}
$headerlen += 2;
}
// GZIP FOOTER
$datacrc = unpack("V",substr($data,-8,4));
$datacrc = sprintf('%u',$datacrc[1] & 0xFFFFFFFF);
$isize = unpack("V",substr($data,-4));
$isize = $isize[1];
// decompression:
$bodylen = $len-$headerlen-8;
if (
$bodylen < 1) {
// IMPLEMENTATION BUG!
return null;
}
$body = substr($data,$headerlen,$bodylen);
$data = "";
if (
$bodylen > 0) {
switch (
$method) {
case
8:
// Currently the only supported compression method:
$data = gzinflate($body,$maxlength);
break;
default:
$error = "Unknown compression method.";
return
false;
}
}
// zero-byte body content is allowed
// Verifiy CRC32
$crc = sprintf("%u",crc32($data));
$crcOK = $crc == $datacrc;
$lenOK = $isize == strlen($data);
if (!
$lenOK || !$crcOK) {
$error = ( $lenOK ? '' : 'Length check FAILED. ') . ( $crcOK ? '' : 'Checksum FAILED.');
return
false;
}
return
$data;
}
?>
4
katzlbtjunk at hotmail dot com
16 年前
Aaron G. 2004 年 8 月 7 日 03:29 在 gzencode 注释中发布了函数 gzdecode()。
我修复了错误:if($flags & 1) 为正确的 if($flags & 2)

不幸的是,函数 gzencode() 没有追加 CRC,所以直到我尝试上传用 gzip 本身压缩的文件时,我才注意到这一点。

<?php

function gzdecode($data) {
$len = strlen($data);
if (
$len < 18 || strcmp(substr($data,0,2),"\x1f\x8b")) {
return
null; // Not GZIP format (See RFC 1952)
}
$method = ord(substr($data,2,1)); // Compression method
$flags = ord(substr($data,3,1)); // Flags
if ($flags & 31 != $flags) {
// Reserved bits are set -- NOT ALLOWED by RFC 1952
return null;
}
// NOTE: $mtime may be negative (PHP integer limitations)
$mtime = unpack("V", substr($data,4,4));
$mtime = $mtime[1];
$xfl = substr($data,8,1);
$os = substr($data,8,1);
$headerlen = 10;
$extralen = 0;
$extra = "";
if (
$flags & 4) {
// 2-byte length prefixed EXTRA data in header
if ($len - $headerlen - 2 < 8) {
return
false; // Invalid format
}
$extralen = unpack("v",substr($data,8,2));
$extralen = $extralen[1];
if (
$len - $headerlen - 2 - $extralen < 8) {
return
false; // Invalid format
}
$extra = substr($data,10,$extralen);
$headerlen += 2 + $extralen;
}

$filenamelen = 0;
$filename = "";
if (
$flags & 8) {
// C-style string file NAME data in header
if ($len - $headerlen - 1 < 8) {
return
false; // Invalid format
}
$filenamelen = strpos(substr($data,8+$extralen),chr(0));
if (
$filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
return
false; // Invalid format
}
$filename = substr($data,$headerlen,$filenamelen);
$headerlen += $filenamelen + 1;
}

$commentlen = 0;
$comment = "";
if (
$flags & 16) {
// C-style string COMMENT data in header
if ($len - $headerlen - 1 < 8) {
return
false; // Invalid format
}
$commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0));
if (
$commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
return
false; // Invalid header format
}
$comment = substr($data,$headerlen,$commentlen);
$headerlen += $commentlen + 1;
}

$headercrc = "";
if (
$flags & 2) {
// 2-bytes (lowest order) of CRC32 on header present
if ($len - $headerlen - 2 < 8) {
return
false; // Invalid format
}
$calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
$headercrc = unpack("v", substr($data,$headerlen,2));
$headercrc = $headercrc[1];
if (
$headercrc != $calccrc) {
return
false; // Bad header CRC
}
$headerlen += 2;
}

// GZIP FOOTER - These be negative due to PHP's limitations
$datacrc = unpack("V",substr($data,-8,4));
$datacrc = $datacrc[1];
$isize = unpack("V",substr($data,-4));
$isize = $isize[1];

// Perform the decompression:
$bodylen = $len-$headerlen-8;
if (
$bodylen < 1) {
// This should never happen - IMPLEMENTATION BUG!
return null;
}
$body = substr($data,$headerlen,$bodylen);
$data = "";
if (
$bodylen > 0) {
switch (
$method) {
case
8:
// Currently the only supported compression method:
$data = gzinflate($body);
break;
default:
// Unknown compression method
return false;
}
} else {
// I'm not sure if zero-byte body content is allowed.
// Allow it for now... Do nothing...
}

// Verifiy decompressed size and CRC32:
// NOTE: This may fail with large data sizes depending on how
// PHP's integer limitations affect strlen() since $isize
// may be negative for large sizes.
if ($isize != strlen($data) || crc32($data) != $datacrc) {
// Bad format! Length or CRC doesn't match!
return false;
}
return
$data;
}

?>
1
webmaster at askapache dot com
16 年前
我使用此简单函数来从网络下载的 gzip 文件中解码 gzdecode。

<?php

function gzdecode($data){
$g=tempnam('/tmp','ff');
@
file_put_contents($g,$data);
ob_start();
readgzfile($g);
$d=ob_get_clean();
return
$d;
}

?>
0
Artem
7 年前
这可以帮助在某些情况下解压缩来自 tar 存档的数据,希望这对某些人有帮助
<?php
$tarName
= 'example.ini';

try {
/** 对于带有 tar.gz 扩展名的文件 **/
$phar = new \PharData($path, 0, $tarName);
} catch (
$e) {
/** 首先解码文件 **/
$decodedFile = file_put_contents($path, gzdecode(file_get_contents($path)));
/** 其次创建 PharData 对象 **/
$phar = new \PharData($path, 0, $tarName);
}
?>
To Top