PHP Conference Japan 2024

gzdecode

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

gzdecode解碼 gzip 壓縮的字串

說明

gzdecode(字串 $data, 整數 $max_length = 0): 字串|false

此函式會傳回已解碼的輸入 data 版本。

參數

data

要解碼的資料,由 gzencode() 編碼。

max_length

要解碼的資料的最大長度。

返回值

解碼後的字串,或者在失敗時返回 false

錯誤/例外

如果失敗,會發出 E_WARNING 等級的錯誤。

參見

新增註解

使用者貢獻的註解 6 則註解

anonymous at dekho-ji dot com
11 年前
要在 PHP 程式碼中解碼/解壓縮收到的 HTTP POST 資料,請求資料來自 Java/Android 應用程式,透過 HTTP POST GZIP/DEFLATE 壓縮格式傳送。

1) 從 Java Android 應用程式使用 DeflaterOutputStream Java 類別傳送到 PHP 的資料,在 PHP 中接收如下所示:
echo gzinflate( substr($HTTP_RAW_POST_DATA,2,-4) ) . PHP_EOL . PHP_EOL;

2) 從 Java Android 應用程式使用 GZIPOutputStream Java 類別傳送到 PHP 的資料,在 PHP 程式碼中接收如下所示:
echo gzinflate( substr($HTTP_RAW_POST_DATA,10,-8) ) . PHP_EOL . PHP_EOL;

從 Java Android 端(API level 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 level 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";

希望這有幫助。如果這幫您省下很多精力和時間,請按讚。
tomas at slax dot org
13 年前
我對壓縮演算法和格式沒有深入的了解,所以我不知道這是否在所有平台上都相同。但是在我 Linux 系統上經過幾次實驗,我發現如何在沒有臨時檔案的情況下取得 gzdecoded 資料。方法如下:

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

就是這樣。只需去除標頭和尾部的位元組,您就可以獲得用於解壓縮的原始 deflated 資料。
katzlbtjunk at hotmail dot com
16 年前
發現並修復了另外 3 個錯誤
1. 當 gz 包含檔名時無法運作 - 已修復
2. 在 64 位元架構 (checksum) 上無法運作 - 已修復
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;
}
?>
katzlbtjunk at hotmail dot com
16 年前
Aaron G. 於 2004 年 8 月 7 日 03:29 張貼了函式 gzdecode()
到 gzencode 註釋中。我修復了錯誤:將 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;
}

?>
webmaster at askapache dot com
16 年前
我使用了這個簡單的函式來解壓縮從網路上下載的 gzip 檔案。

<?php

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

?>
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