update page now

gzwrite

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

gzwriteBir dizgeyi bir gzipli dosyaya ikil kipte yazar

Açıklama

gzwrite(resource $dt, string $dizge, ?int $length = null): int|false

gzwrite() işleve belirtilen dizge dizgesini belirtilen dosya tanıtıcısına sıkıştırarak yazar.

Bağımsız Değişkenler

dt

Gzipli dosya tanıtıcısı. gzopen() tarafından açılmış bir dosyayı gösteren geçerli bir tanıtıcı olmalıdır.

dizge

Sıkıştırılarak dosyaya yazılacak dizge.

uzunluk

Yazılacak sıkıştırılmamış bayt sayısı. Belirtilmişse yazma işlemi sıkıştırılmamış uzunluk bayt yazıldıktan sonra veya dosya sonuna gelindiğinde (hangisi önce olursa) durur.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 uzunluk artık null olabiliyor; evvelce 0 öntanımlıydı.
7.4.0 Başarısızlık durumunda artık false dönüyor; evvelce 0 dönerdi.

Dönen Değerler

Belirtilen sıkıştırılmış dosya akımına yazılan sıkıştırılmamış bayt sayısı ile döner, başarısızlık durumunda false döner.

Örnekler

Örnek 1 - gzwrite() örneği

<?php
$dizge
= 'Sıkıştırılacak bir takım bilgiler';
$gz = gzopen('birdosya.gz','w9');
gzwrite($gz, $dizge);
gzclose($gz);
?>

Ayrıca Bakınız

add a note

User Contributed Notes 1 note

up
4
calmarius at nospam dot atw dot hu
17 years ago
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',''));
}

?>
To Top