PHP Conference Japan 2024

gzwrite

(PHP 4, PHP 5, PHP 7, PHP 8)

gzwrite二進位安全 gz 檔案寫入

說明

gzwrite(資源 $stream, 字串 $data, ?int $length = null): int|false

gzwrite()data 的內容寫入指定的 gz 檔案。

參數

stream

gz 檔案指標。它必須有效,且必須指向一個已成功使用 gzopen() 開啟的檔案。

資料 (data)

要寫入的字串。

長度 (length)

要寫入的未壓縮位元組數。如果提供此參數,寫入操作將在寫入 length 個(未壓縮的)位元組或到達 data 的結尾時停止,以先發生的情況為準。

回傳值

返回寫入指定 gz 檔案串流的(未壓縮)位元組數,如果失敗則返回 false

更新日誌

版本 說明
8.0.0 length 現在可以為 null;之前預設值為 0
7.4.0 此函數現在在失敗時返回 false;之前返回 0

範例

範例 #1 gzwrite() 範例

<?php
$string
= '一些要壓縮的資訊';
$gz = gzopen('somefile.gz','w9');
gzwrite($gz, $string);
gzclose($gz);
?>

參見

新增註解

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

calmarius at nospam dot atw dot hu
15 年前
gz 壓縮常與 tar 封存檔一起使用。建立 tar 封存檔相當容易。以下是一段程式碼片段,可用於在壓縮成 tar.gz 之前建立 tar 封存檔。

<?php

//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
// Adds file header to the tar file, it is used before adding file content.
// f: file resource (provided by eg. fopen)
// phisfn: path to file
// archfn: path to file in archive, directory names must be followed by '/'
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
function TarAddHeader($f,$phisfn,$archfn)
{
$info=stat($phisfn);
$ouid=sprintf("%6s ", decoct($info[4]));
$ogid=sprintf("%6s ", decoct($info[5]));
$omode=sprintf("%6s ", decoct(fileperms($phisfn)));
$omtime=sprintf("%11s", decoct(filemtime($phisfn)));
if (@
is_dir($phisfn))
{
$type="5";
$osize=sprintf("%11s ", decoct(0));
}
else
{
$type='';
$osize=sprintf("%11s ", decoct(filesize($phisfn)));
clearstatcache();
}
$dmajor = '';
$dminor = '';
$gname = '';
$linkname = '';
$magic = '';
$prefix = '';
$uname = '';
$version = '';
$chunkbeforeCS=pack("a100a8a8a8a12A12",$archfn, $omode, $ouid, $ogid, $osize, $omtime);
$chunkafterCS=pack("a1a100a6a2a32a32a8a8a155a12", $type, $linkname, $magic, $version, $uname, $gname, $dmajor, $dminor ,$prefix,'');

$checksum = 0;
for (
$i=0; $i<148; $i++) $checksum+=ord(substr($chunkbeforeCS,$i,1));
for (
$i=148; $i<156; $i++) $checksum+=ord(' ');
for (
$i=156, $j=0; $i<512; $i++, $j++) $checksum+=ord(substr($chunkafterCS,$j,1));

fwrite($f,$chunkbeforeCS,148);
$checksum=sprintf("%6s ",decoct($checksum));
$bdchecksum=pack("a8", $checksum);
fwrite($f,$bdchecksum,8);
fwrite($f,$chunkafterCS,356);
return
true;
}
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
// Writes file content to the tar file must be called after a TarAddHeader
// f:file resource provided by fopen
// phisfn: path to file
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
function TarWriteContents($f,$phisfn)
{
if (@
is_dir($phisfn))
{
return;
}
else
{
$size=filesize($phisfn);
$padding=$size % 512 ? 512-$size%512 : 0;
$f2=fopen($phisfn,"rb");
while (!
feof($f2)) fwrite($f,fread($f2,1024*1024));
$pstr=sprintf("a%d",$padding);
fwrite($f,pack($pstr,''));
}
}
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
// Adds 1024 byte footer at the end of the tar file
// f: file resource
//\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
function TarAddFooter($f)
{
fwrite($f,pack('a1024',''));
}

?>
Kioob
21 年前
這是一個使用 gzwrite 函式的簡短範例。
<?php
函數 gzcompressfile($source,$level=false){
$dest=$source.'.gz';
$mode='wb'.$level;
$error=false;
if(
$fp_out=gzopen($dest,$mode)){
if(
$fp_in=fopen($source,'rb')){
while(!
feof($fp_in))
gzwrite($fp_out,fread($fp_in,1024*512));
fclose($fp_in);
}
else
$error=true;
gzclose($fp_out);
}
else
$error=true;
if(
$error) return false;
else return
$dest;
}
?>

gzcompressfile() 函數會將 'data.csv' 檔案壓縮成 'data.csv.gz'。如果出現錯誤,該函數會返回 false,如果成功則返回新的檔案名稱。
注意
14 年前
仔細閱讀 gzwrite() 的說明。如果未指定「長度」選項,則在啟用魔術引號的系統上,輸入數據中的反斜線將會被移除。在壓縮檔案時,務必了解這項重要資訊。

<?php
$data
= fread($fp, 5000);
gzwrite($fp2, $data, strlen($data));
?>

這是避免魔術引號問題的正確方法。
匿名
16 年前
katzlbtjunk 的方法確實比較簡短,但除了小型檔案外,它無法用於其他任何用途。它會嘗試將整個檔案載入到記憶體中,然後建立一個完整的壓縮副本,最後才會將其寫入磁碟。下面提供的其他方法不會像這樣耗盡記憶體。
repository at lamaresh dot net
15 年前
此函數會將一行新增至 .gz 壓縮檔案

<?php
函數 AppendLineGz($file,$string) {
如果 (
file_exists($file)) {
$lines = gzfile($file);
$lines[count($lines)] = "$string\r\n";
$input=implode($lines);
} 否則 {
$input="$string\r\n";}
$gz = gzopen($file,'w9');
gzwrite($gz, $input);
gzclose($gz);
}
?>
katzlbtjunk at hotmail dot com
16 年前
可以改用這樣
$s = file_get_contents('file.tar');
file_put_contents('file.tar.gz',gzencode($s,9));
To Top