Compressing and Uncompressing with Zlib
The pseudo protocol compress.zlib://
can be used to access ZIP files as you would access regular files. You can use the files using, for instance, file_get_contents()
and file_put_contents()
.
Example: Compressing a File
The following code loads the file.txt
with file_get_contents()
), zips it with file_put_contents("compress.zlib://$file",$data)
, and writes it to the hard disk.
<?php $file = 'file.txt'; $zip = "$file.zip"; $data = file_get_contents($file); file_put_contents("compress.zlib://$zip", $data); echo "$file size: ".filesize($file).'<br>' . " $zip size: ".filesize($zip); /* Prints: file.txt size: 19096 file.txt.zip size: 159 */
Example: Uncompressing a Zip File
The following code uncompresses the file.txt.zip
with file_get_contents("compress.zlib://$zip")
, and writes it to the hard disk with file_put_contents($file, $data)
.
<?php $zip = 'file.txt.zip'; $file = strchr($zip, '.zip',true); $data = file_get_contents("compress.zlib://$zip"); file_put_contents($file, $data); echo "$file size: ".filesize($file).'<br>' . " $zip size: ".filesize($zip); /* Prints: file.txt size: 19096 file.txt.zip size: 159 */
Alternatively, you can use PHP’s special Zlib functions. They work very similarly to PHP’s file functions. However, for writing, you have to use gzopen()
, gzwrite()
, and gzclose()
instead of fopen(), fwrite(), and fclose(), as the following code shows:
Example: Compressing a file with gzopen
and gzwrite
:
<?php $file = 'file.txt'; $zip = "$file.gz"; $data = file_get_contents($file); if ($fp = gzopen($zip, 'wb9')) { $size = gzwrite($fp, $data); gzclose($fp); echo "$file (size: $size) compressed to $zip."; } else { echo 'Error'; } //file.txt (size: 19068) compressed to file.txt.gz.
Note: In fopen()
you use rb
, wb
, etc. as the file mode but in gzopen
you can also include a compression level, for example, wb9
.
Example: Decompressing a file with gzread
:
<?php $zip = 'file.txt.gz'; $file = strchr($zip, '.gz',true); if ($fp = gzopen($zip, 'r')) { $data = ''; while ($bytes = gzread($fp, 4096)){ $data .= $bytes; } gzclose($fp); //Writing uncompressed data to file file_put_contents($file, $data); echo "$zip uncompressed to $file"; } else { echo 'Error'; } //file.txt.gz uncompressed to file.txt
Zipping and Unzipping a File with Bzip2
Compressing:file_put_contents("compress.bzip2://$file", $data);
Decompressing:file_get_contents("compress.bzip2://$file")
Another file format that offers great compression rates is Bzip2. PHP also has a built-in stream wrapper compress.bzip2://
to compress or decompress files, as can be seen in the preceding code, which uses file_get_contents()
and file_put_contents()
.
Example: Compressing a File
<?php $file = 'file.txt'; $zip = "$file.bz2"; $data = file_get_contents($file); file_put_contents("compress.bzip2://$zip", $data); echo "$file size: ".filesize($file).'<br>' . " $zip size: ".filesize($zip); /* Prints: file.txt size: 19096 file.txt.bz2 size: 72 */
Example: Decompressing a File
<?php $zip = 'file.txt.bz2'; $file = strchr($zip, '.bz2',true); $data = file_get_contents("compress.bzip2://$zip"); file_put_contents($file, $data); echo "$file size: ".filesize($file).'<br>' . " $zip size: ".filesize($zip); /* Prints: file.txt size: 19096 file.txt.bz2 size: 72 */
Similarly to Zlib, you can also use PHP’s bz2 functions. They work very similarly to PHP’s file and zlib functions. However, for writing, you have to use bzopen()
, bzwrite()
, and bzclose()
:
Example: Decompressing a file with bzwrite
:
<?php $file = 'abc.txt'; $zip = "$file.bz2"; $data = file_get_contents($file); if ($fp = bzopen($zip, 'w')) { $size = bzwrite($fp, $data); bzclose($fp); echo "$file (size: $size) compressed to $zip."; } else { echo 'Error'; } //abc.txt (size: 23) compressed to abc.txt.bz2.
Example: Decompressing a file with bzread
:
<?php $zip = 'abc.txt.bz2'; $file = strchr($zip, '.bz2',true); if ($fp = bzopen($zip, 'r')) { $data = ''; while ($bytes = bzread($fp, 4096)){ $data .= $bytes; } bzclose($fp); //Writing uncompressed data to file file_put_contents($file, $data); echo "$zip uncompressed to $file"; } else { echo 'Error'; } //abc.txt.bz2 uncompressed to abc.txt
Working with Files in PHP:
- Returning or Downloading Files with an HTTP Request
- Reading a File into a String or Array
- PHP Opening and Closing Files
- Reading files by line or by character
- Writing and appending to files
- Reading and Writing CSV Files
- Parsing INI Files and Strings
- Check File Type (whether a file is a directory or a file)
- Understanding file Permissions in PHP
- Reading Information About Files in PHP
- Copying, Moving and Deleting Files in PHP
- Reading Directories Contents in PHP
- Browse directories recursively
- Zipping and Unzipping a File with Zlib and Bzip2
- Zip and Unzip Archives with ZipArchive
- Using Relative Paths for File Access