downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

readgzfile> <gzuncompress
[edit] Last updated: Fri, 10 Feb 2012

view this page in

gzwrite

(PHP 4, PHP 5)

gzwriteバイナリセーフな gz ファイル書き込み

説明

int gzwrite ( resource $zp , string $string [, int $length ] )

gzwrite()string の内容を与えられた gz ファイルに書き込みます。

パラメータ

zp

gz ファイルポインタを指定します。これは有効なファイルポインタであり、 かつ、gzopen() によりオープンできたファイルを指している必要があります。

string

書き込む文字列を指定します。

length

書き込む解凍されたバイト数を指定します。 もし指定された場合、 length バイトのデータが書き込まれたか、 string の終わりに達した時に 書き込みは終了します。

注意:

引数 length が指定された場合、 magic_quotes_runtime 設定オプションは無視されて string から スラッシュが取り除かれなくなることに注意してください。

返り値

与えられた gz ファイルストリームに書き込まれた (解凍された) バイト数を返します。

例1 gzwrite() の例

<?php
$string 
'Some information to compress';
$gz gzopen('somefile.gz','w9');
gzwrite($gz$string);
gzclose($gz);
?>

参考

  • gzread() - バイナリ対応のgzファイル読み込み
  • gzopen() - gz ファイルを開く



readgzfile> <gzuncompress
[edit] Last updated: Fri, 10 Feb 2012
 
add a note add a note User Contributed Notes gzwrite
Take Heed 07-Nov-2010 12:50
Read the description of gzwrite() very carefully.  If the 'length' option is not specified, then the input data will have slashes stripped on systems where magic quotes are enabled.  This is important information to know when compressing files.

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

Is the correct way to avoid issues with magic quotes.
calmarius at nospam dot atw dot hu 05-Feb-2009 08:07
gz compression is often used with tar archives. Building tar archives is quite easy. Here is a code snipplet which can be used for building tar archives before compressing them to tar.gz.

<?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',''));
}

?>
repository at lamaresh dot net 03-Feb-2009 10:37
This function add a line to a .gz compressed file

<?php
function AppendLineGz($file,$string) {
    if (
file_exists($file)) {
       
$lines = gzfile($file);
       
$lines[count($lines)] = "$string\r\n";
       
$input=implode($lines);
    } else {
$input="$string\r\n";}
   
$gz = gzopen($file,'w9');
   
gzwrite($gz, $input);
   
gzclose($gz);   
}
?>
Anonymous 27-Mar-2008 12:34
katzlbtjunk's method is certainly shorter, but it is unusable for anything except small files.  It would try to load the whole file into memory, then create an entire compressed copy, and only then write it to disk.  The other method given below will not exhaust memory like that.
katzlbtjunk at hotmail dot com 21-Feb-2008 11:24
How about this instead:
$s = file_get_contents('file.tar');
file_put_contents('file.tar.gz',gzencode($s,9));
Kioob 13-Aug-2003 04:30
This is a short example of use gzwrite function.
<?php
function 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;
    }
?>

the function gzcompressfile() compress a file 'data.csv' to 'data.csv.gz'. the function return false if error, and the new file name if it's ok.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites