Updated April 18, 2023
Introduction to PHP Zip
The PHP Zip is one of the default methods in php that is used for to archive or compressed the file using the formats like .zip, .tar, .rar etc. based on the above formats, the datas are compressed in the single format folder; it’s a convenient approach for a bunch of datas are joined together with same storage areas and sharing with the same and also depending on the operating system the archive file extension is supported.T. T. These functions will support according to the corresponding libraries note that the zip method or some other default methods the extension is more necessary for to plugin. In contrast, we use it in the PHP code.
Syntax
PHP code each method have different usages, and the functionalities may vary depends upon the requirement. Likewise, zip is one of the built-in methods in PHP; it contains one or more files to be compressed.
<?php
$zip=new ZipArchive;
----some php code logics----
$zip -> close();
?>
The above codes are the basic syntax for zip method usages, and the ZipArchive is the default class for creating the instance of the zip method. Using the ZipArchive class methods, it will be coordinate and performed with the zip method.
How does the Zip Method work in PHP?
The zip is an archive method for compressed the files that can be held with the other format files, and the datas are encrypted using the extract option; we will extract the datas to the same location or customized path in the machine. It’s one of the convenient methods that can be used to keep up the bunch of files are joined and together with the storing and retrieving the datas. The PHP zip file function allows the scripts to work with the zip files; using these function; the corresponding libraries will be imported to the PHP scripts. Using the PHP zip extension, the pho version 5 and above to be supported. Some default PHP dll and ini files should must be enabled in the php installation before running the PHP scripts.
The zip methods come from the ziparchive class; the calls contain the default method for achieving the compression and extract the zip folders. zip_close() is used for to close the zip files in PHP, zip_entry_close () is used for closed the PHP zip file in the particular entries. Using the zip_entry_compressedsize() is used for returning the sizes of the PHP zip entries its already compressed folder zip_entry_compressionmethod() will always return the compression method of the PHP zip files. The PHP $zip method has some additional child methods like zip_entry_filesize() will calculate the compressed file size and return the exact file sizes already created in the PHP zip files. Likewise zip_entry_name(), open(), read(), zip_entry_open() and zip_entry_read() these are the some default methods for performed the operations regarding the compressed concepts in the PHP code. It is also possible to create a zip file containing multiple files, and always it sends the zip file to the browsers for downloading or extracting the folders.
Once we created the instance of the ZipArchive class, we can use the default method open (datatype filename, datatype flag) function to open the zip files for crud operations. The ziparchive has some flags for overriding the zip file that is already existed in the file. Using create a flag, the ziparchive class has created the zip without existing the method. Once the zip file is opened, we can add the additional files to that using the addFile(datatype path, datatype filename) method for adding the files to the zip file. If the zip files already exist in the file, the ZipArchive class will identify the files and remove them from the code.
Examples
Different examples are mentioned below:
Example #1
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$zi = new ZipArchive();
$zippaths = 'F:\first.zip';
$fg = (file_exists($zippaths))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;
if($zi->open($zippaths, $fg) === true){
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBCXJjsdkfhjndmhdkjhfdmnkjdndsjkbjdsfnbdndfbHNZBXHjhbjnhsabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBCXJHNZBXHjhyteeeuikjwqhkjqmnk12323443bjnhsabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBCXJHNZBXHjhbjrrreeewnhsabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBCXJHNZBrttyyyyyXHjhbjnh5sabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBCXJHNZBXHjhbjnhsabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.', 'Have a Nice dayej,SHJKHJDHHBSJXNBgfghghyyytyyyyyyyyyyyyyyyyyyyyyyyyyyyuiueuiohjhiouoikjkuikjhiukCXJHN45rty6565656ZBXHjhbjnhsabshjhmbsjnsm');
$zi->addFromString('first.txt', 'wWlcome To My Domain.','Have a Nice dayej,SHJKHJDHHBSJXNBCXJHNZBXHjhbjnhsabshjhmbsjnsm');
$zi->close();
}
else{
echo "Thank you user have a nice day";
}
?>
</body>
</html>
Output:
Example #2
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$z1 = new ZipArchive();
$filep = 'F:\second.zip';
$f1 = (file_exists($filep))? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;
do
{
if($z1->open($filep, $f1) === true){
$z1->addFile('second.txt', '82365o97429032bIf the directory of the zip file is not exist, then it will throw an error. If you will not set the local name, then the default name and directory of the file will be the first parameter. And if the file doesnt exist, nothing will happen and it will return a false value.So you should create first a valid directory before you open or add a zip file.');
$z1->close();
}
else{
echo "Thank you user have a nice day";
}
}
while(!is_null($z1));
?>
</body>
</html>
Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$zq = zip_open("F:\third.zip");
if ($zq) {
while ($zip_entry = zip_read($zq)) {
echo "<p>";
echo "Welcome To My DOmainNull is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it. Tip: If a variable is created without a value, it is automatically assigned a value of NULL " . zip_entry_name($zip_entry) . "<br>";
echo "Have a Nice Day: loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. " .zip_entry_filesize($zip_entry);
echo "</p>";
}
zip_close($zq);
}
?>
</body>
</html>
Output:
Conclusion
In the php_zip file, the method is fully compressed on the server-side scripting; if we require to decompressed data, it will be used to stream the datas from the customer side. The file datas with the extension also be transferred the datas without any interruptions. So it can be avoided the data losses from both clients to the server and its vice-versa.
Recommended Articles
This is a guide to PHP Zip. Here we discuss the introduction, syntax, and working of the zip method in PHP along with different examples and code implementation. You may also have a look at the following articles to learn more –