PHP Zip Extension

Sheeraz Gul Jan 30, 2023
  1. Install ZIP Extensions in Ubuntu
  2. Use the ZipArchive() Class to Create a Zip File in PHP
  3. Use the ZipArchive() Class to Create a Zip File in PHP
  4. How to Use PHP Zip Extension to Get Information of All the Member Files of a ZIP
PHP Zip Extension

Zip extension is one of the most important items in file handling. If you want to compress files or move multiple files through one file, ZIP will be required for this.

We need to install libzip to perform operations involving the ZIP extension for other platforms.

Install ZIP Extensions in Ubuntu

ZIP is already included in windows PHP. We will try to install this library for Ubuntu.

Run the following commands in Ubuntu to install the libzip library.

$ sudo apt-get update
$ sudo apt-get install -y
$ sudo apt-get install libzip-dev

Output:

Setting up libzip-dev:amd64 (1.7.3-1+ubuntu20.04.1+deb.sury.org+2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

The next step is to install a PHP extension for ZIP.

$ sudo apt install php7.4-zip

Once it is successfully installed, you can use it in PHP.

Use the ZipArchive() Class to Create a Zip File in PHP

ZipArchive() is the class used to perform ZIP operations in PHP. Using the ZipArchive() class, create a Zip file.

<?php
$create_zip = new ZipArchive();
$file_name = "./New.zip";

if ($create_zip->open($file_name, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open the zip file <$file_name>\n");
}
$current_dir=getcwd();
//Create files to add to the zip
$create_zip->addFromString("file1 ". time().".txt" , "#1 This is This is the test file number one.\n"); 
$create_zip->addFromString("file2 ". time().".txt", "#2 This is This is the test file number one.\n");
//add files to the zip
$create_zip->addFile($current_dir . "/too.php","/testfromfile.php");
echo "Number of files added: " . $create_zip->numFiles;
echo "<br>";
echo "Failed to add:" . $create_zip->status ;
$create_zip->close();
?>

The code above creates two text files with some content and adds them into a zip file.

Output:

Create Zip File in PHP

Use the ZipArchive() Class to Create a Zip File in PHP

Let’s extract the zip file created in the first code using the ZipArchive() class of PHP.

<?php
$extract_zip = new ZipArchive;
$open_zip = $extract_zip->open('New.zip');
if ($open_zip === TRUE) {
	$extract_to = getcwd();
    $extract_zip->extractTo($extract_to); //extract to the current working directory.
    echo "Number of Files to be Extracted:" . $extract_zip->numFiles . "<br>";
	$extract_zip->close();
    echo 'Files Successfully Extracted!';
} 
else {
    echo 'Cannot Extract!';
}
?>

The above code will extract the New.zip file created in the first example.

Output:

Extract Zip File in PHP

How to Use PHP Zip Extension to Get Information of All the Member Files of a ZIP

The PHP ZIP extension can get the information of all the files inside a ZIP.

<?php
$zip_file = zip_open("New.zip");
if ($zip_file) {
    while ($zip_members = zip_read($zip_file)) {
        echo "Name of the file:               " . zip_entry_name($zip_members) . "<br>";
        echo "Original Size of the File:    " . zip_entry_filesize($zip_members) . "<br>";
        echo "Compressed Size of the File:    " . zip_entry_compressedsize($zip_members) . "<br>";
        echo "Method of Compression: " . zip_entry_compressionmethod($zip_members) . "<br>";

        if (zip_entry_open($zip_file, $zip_members, "r")) {
            echo "Content of the file:<br>";
            $buf = zip_entry_read($zip_members, zip_entry_filesize($zip_members));
            echo "$buf <br>";

            zip_entry_close($zip_members);
        }
        echo "<br>";
    }
    zip_close($zip_file);
}
?>

The code above uses built-in ZIP functions to get the information of files inside a ZIP.

Output:

Name of the file: file1 1644842283.txt
Original Size of the File: 45
Compressed Size of the File: 39
Method of Compression: deflated
Content of the file:
#1 This is test file number one.

Name of the file: file2 1644842283.txt
Original Size of the File: 45
Compressed Size of the File: 39
Method of Compression: deflated
Content of the file:
#2 This is test file number two.
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Zip