zlib://

bzip2://

zip://

zlib:// -- bzip2:// -- zip://Flujos de compresión

Descripción

zlib: PHP 4.0.4 - PHP 4.2.3 (sólo en sistemas con fopencookie)

compress.zlib:// and compress.bzip2:// PHP 4.3.0 y superior

zlib: funciona como gzopen(), solo que usando el flujo, se puede utilizar con fread() al igual que con otras funciones del sistema de ficheros. Esto quedó obsoleto en PHP 4.3.0 debido a la ambigüedad que surge con los nombres de fichero que contienen el caracter ':'; en su lugar, debe usarse compress.zlib://.

compress.zlib:// y compress.bzip2:// son equivalentes a gzopen() y a bzopen() respectivamente, y funcionan incluso en sistemas que no tienen soporte para fopencookie.

La extensión ZIP registra la envoltura zip:.

Uso

  • compress.zlib://file.gz
  • compress.bzip2://file.bz2
  • zip://archive.zip#dir/file.txt

Opciones

Resumen de la Envoltura
Atributo Permitido
Restringido por allow_url_fopen No
Permite Lecturas
Permite Escrituras Sí (excepto zip://)
Permite Añadir contenidos Sí (excepto zip://)
Permite Lecturas y Escrituras Simultaneas No
Permite usar la función stat() No, utilice la envoltura file:// para consultar la información de un fichero.
Permite usar la función unlink() No, utilice la envoltura file:// para eliminar ficheros comprimidos.
Permite usar la función rename() No
Permite usar la función mkdir() No
Permite usar la función rmdir() No

add a note add a note

User Contributed Notes 6 notes

up
16
lewa::cpan.org
7 years ago
One-liners to gzip and ungzip a file:

copy('file.txt', 'compress.zlib://' . 'file.txt.gz');

copy('compress.zlib://' . 'file.txt.gz', 'file.txt');
up
4
feodor at ctm dot ru
9 years ago
Prior to PHP 5.6 i used code like
<?php
file_get_contents
("compress.zlib://php://input");
?>
to read gz-compressed or plain input file. Not it doesn't work.
Simple workaround :
<?php
//file_get_contents("compress.zlib://php://input");

class gzip_header_filter8 extends php_user_filter {
    private
$filtered = 0;
    public function
filter($in, $out, &$consumed, $closing) {
        while (
$bucket = stream_bucket_make_writeable($in)) {
            if(
$this->filtered == 0) {
               
$header_len = 8;
               
$header = substr($bucket->data, 0, 8);
               
$flags = ord($header[1]);
                if(
$flags & 0x08) {
                   
// a filename is present
                   
$header_len = strpos($bucket->data, "\0", 8) + 1;
                }
               
$bucket->data = substr($bucket->data, $header_len);
               
$this->filtered = $header_len;
            }
           
$consumed += $bucket->datalen;
           
stream_bucket_append($out, $bucket);
        }
        return
PSFS_PASS_ON;
    }
}

stream_filter_register('gzip_header_filter8', 'gzip_header_filter8');

$READ_LEN = 64*1024;
$MAX_BUF_LEN = $READ_LEN*1024;

$mode_plain=true;
$src=fopen("php://input","rb"); //test if OK!!!!
$magic_number=fread($src,2);
$input="";
    if(
strlen($magic_number)==2)
        if(
            (
ord($magic_number[0])==31)
        &&  (
ord($magic_number[1])==139)
            )
        {
       
$mode_plain=false;
       
stream_filter_append($src, "gzip_header_filter8", STREAM_FILTER_READ);
       
stream_filter_append($src, "zlib.inflate", STREAM_FILTER_READ);
        }
else
        {
       
$input=$magic_number;
        }

/* read starts here*/
$k = 0;
while (!
feof($src) && $k <= $MAX_BUF_LEN) {
     
$inp = fread($src,$READ_LEN);
     
$k += strlen($inp);
     
$input.=$inp;
    }
echo
$input;
fclose($src);
?>
up
11
alvaro at demogracia dot com
13 years ago
Example on how to read an entry from a ZIP archive (file "bar.txt" inside "./foo.zip"):

<?php

$fp
= fopen('zip://./foo.zip#bar.txt', 'r');
if(
$fp ){
    while( !
feof($fp) ){
        echo
fread($fp, 8192);
    }
   
fclose($fp);
}

?>

Also, apparently, the "zip:" wrapper does not allow writing as of PHP/5.3.6. You can read http://php.net/ziparchive-getstream for further reference since the underlying code is probably the same.
up
2
joshualross at gmail dot com
17 years ago
I had a difficult time finding how to use compress.zlib with an http resource so I thought I would post what I found
<?php
$file
= 'compress.zlib://http://www.example.com/myarchive.gz';
$fr = fopen($file, 'rb');
?>

Per the bugreport I found here (http://bugs.php.net/bug.php?id=29045)
up
0
arava dot box at gmail dot com
5 years ago
This is the solution how to read stream from zip:

$fp = fopen('zip://foo.zip#bar.txt', 'r');
if( $fp ){
    while( !feof($fp) ){
        echo fread($fp, 8192);
    }
    fclose($fp);
}
up
-18
eragonjml at googlemail dot com
11 years ago
@alvaro at demogracia dot com

well in fact that is wrong!
right code is:

<?php

$fp
= fopen('compress.zip://./foo.zip#bar.txt', 'r');
if(
$fp ){
    while( !
feof($fp) ){
        echo
fread($fp, 8192);
    }
   
fclose($fp);
}

?>

as you might see you just have to add a "compress."

maybe when you posted this note is was right (2 years ago) but today its wrong... :/

sry for my english i am german :)
To Top