The examples below are showing how to work with ZIP files and compressions in PHP.

BZIP

Example of working with BZIP in PHP

<?php
    $file = "/path/example.bz2";
    $str = "A test string<br />";
    $bz = bzopen($file, "w"); // open file for writing
    // write
    bzwrite($bz, $str);
    bzclose($bz); // close file
    // read
    $bz = bzread($file, "r"); // open file for reading
    echo bzread($bz); // output file (or next 1024 characters)
    bzclose($bz);
?>

ZIP

Example of working with ZIP in PHP

<?php
    $zip = new zipArchive();
    $file = "/path/file.zip";
    
    if ($zip ->open($file, zipArchive::CREATE) !== TRUE)
    {
        exit("Can't open <$file");
    }
    
    $zip ->addFromString("example.txt" . time(), // string added as 'example.txt');
    $zip ->addFromString("example2.txt" . time(), // string added as 'example2.txt');
    $zip ->addFile("/path/file2.php", "/test_file.php");
    
    echo "numfile" . $zip ->numfiles . "<br />";
    echo "status" . $zip ->status . "<br />";
    $zip ->close();
?>

 

›› go to examples ››