PHP compression Stream Wrappers


In PHP, zlib://, bzip2:// and zip:// represent wrappers for respective compression streams.


This works similar to gzopen() function, however, it can be used with filesystem functions like fread() and others.


This is similar to bzopen() function. Both stream wrappers operate even on systems not capable of supporting fopencookie.


The ZIP extension registers this wrapper. From PHP 7.2.0 onwards, archives encrypted with passwords are supported. It is possible to set password with password context option.


zlib compression can be applied with following PHP code

file_put_contents("compress.zlib://test.txt.gz","Hello World\r
"); ?>

To uncompress, we can use following syntax

echo file_get_contents("compress.zlib://test.txt.gz");

We can also use built-in copy() function to build compressed zlib file and uncompress the same

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