Compressed files are always the easy and fastest method to download or upload files. Uploading file one by one not only consumes time but also cost more bandwidth. In this article, we are going to learn about how to zip and unzip files in PHP.
Compress and decompress files in PHP
PHP comes loaded with a lot of extensions to make zip and unzip files much easier. In order to do so, we will use the PHP ZipArchive class to zip and unzip our files.
How to compress files using ZipArchive?
To compress files you need to add files or a whole directory at a time. Firstly we need to create a new instance of ZipArchive and then we need to call the open($filename, [$flags]) method of ZipArchive.
In the open method, $filename is the name of our zip file in which we will add files and $flag is the parameter to define how to handle the zip archive in different situations.
Below are some main constants that the $flag parameter accept:
- ZipArchive::CREATE – Using this flag will create a new zip archive if it does not exists.
- ZipArchive::OVERWRITE – Using this flag will overwrite the contents in the specified archive if it already exists.
- ZipArchive::RDONLY – Using this flag will open the archive in read-only mode. It is available on PHP Version above 7.4.3.
- ZipArchive::EXCL – Using this flag will give an error if the archive with the provided filename already exists.
- ZipArchive::CHECKCONS – Using this flag will perform additional consistency checks on the archive and result in an error if they fail.
You can read about this method and different flag parameters in the documentation maintained by php.net.
Once the archive gets opened successfully, you can add files using addFile($filename, $localname, $start, $length) method. Here $filename is the path of the file that you want to add to the archive, $localname parameter is used to specify the name for the file when it gets added inside the archive.
After adding all the files to your archive, you need to call the close() method to save the archive with all changes.
You can use the below code reference to understand how ZipArchive can be used to compress files:
$zip = new ZipArchive(); $zip->open('MyZip.zip', ZipArchive::CREATE); //store a.png as first.png inside archive $zip->addFile('images/a.png', 'first.png'); //store b.png as same name in archive $zip->addFile('images/b.png', 'b.png'); $zip->addFile('images/c.png', 'c.png'); $zip->addFile('images/d.png', 'd.png'); $zip->close();
The above code will create a zip file named MyZip with the files added from the images folder.
How to decompress files using ZipArchive?
To decompress files we will use extractTo($destination, $entries) to extract the contents of the archive. Here the $destination is the path where the contents of the archive will be extracted. The $entries parameter is used to extract a specific file from the archive.
You can use the below code reference to understand how ZipArchive can be used to decompress files:
$zip = new ZipArchive(); $zip->open('MyZip.zip', ZipArchive::CREATE); $zip->extractTo('UnZip/', 'first.png'); $zip->close();
The above code will extract the “first.png” of “MyZip.zip” into the “UnZip” directory.
You can remove the second parameter in order to extract all the contents from the archive.
$zip = new ZipArchive(); $zip->open('MyZip.zip', ZipArchive::CREATE); $zip->extractTo('UnZip/'); $zip->close();
The above code will extract all files from MyZip.zip into the UnZip directory.
In this article, we have covered the basic functionality of the ZipArchive class to zip and unzip files.
Thank you for reading this article, learning small things will help you make something bigger. Please share this guide with your friends and provide your valuable feedback in the comment section below.