The PHP `Zip` extension allows you to efficiently work with zip archives.
Before you can use PHP's Zip extension, you must ensure that it is installed and enabled. Debian/Ubuntu:
sudo apt-get install php-zip
Red Hat/Fedora:
sudo yum install php-pecl-zip
Windows: Uncomment (or add) the following line in `php.ini`:
extension=php_zip.dll
To create a new zip archive and add files to it:
$zip = new ZipArchive();
$filename = "./myzip.zip";
if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) {
exit("Cannot open $filename\n");
}
$zip->addFromString("testfile1.txt", "Hello World!");
$zip->addFile("path/to/my/file.txt", "folder/file.txt"); // Add a file from path to archive with specific name in archive
$zip->close();
To extract a zip archive to a specific destination:
$zip = new ZipArchive();
$filename = "./myzip.zip";
if ($zip->open($filename) !== TRUE) {
exit("Cannot open $filename\n");
}
$zip->extractTo('/my/destination/dir/');
$zip->close();
To list all files in a zip archive:
$zip = new ZipArchive();
$filename = "./myzip.zip";
if ($zip->open($filename) !== TRUE) {
exit("Cannot open $filename\n");
}
for($i = 0; $i < $zip->numFiles; $i++) {
echo $zip->getNameIndex($i) . "\n";
}
$zip->close();
To remove a file from an existing zip archive:
$zip = new ZipArchive();
$filename = "./myzip.zip";
if ($zip->open($filename) !== TRUE) {
exit("Cannot open $filename\n");
}
$zip->deleteName("folder/file.txt");
$zip->close();
To read the content of a file from a zip archive:
$zip = new ZipArchive();
$filename = "./myzip.zip";
if ($zip->open($filename) !== TRUE) {
exit("Cannot open $filename\n");
}
$content = $zip->getFromName("testfile1.txt");
echo $content; // Outputs: Hello World!
$zip->close();
You can also rename files, set comments, and more:
$zip->renameName("oldname.txt", "newname.txt");
$zip->setCommentName("newname.txt", "This is a comment for this file.");
$zip->setArchiveComment("This is a comment for the entire archive.");
UPCET Exam
Click Here
SAAT Exam
Click Here
MHT CET Exam
Click Here
IPU CET Exam
Click Here
KCET Exam
Click Here
COMEDK UG Exam
Click Here
VITEEE Exam
Click Here
BITSAT
Click Here
DSAT: Dayanand Sagar Admission Test
Click Here
Career In Animation in india
Click Here
Merchant Navy Courses in india
Click Here
Interior Design Career in india
Click Here
UGC NET Exam
Click Here
B. Ed Exam
Click Here
AFCAT - Air Force Common Admission Test
Click Here
GATE Exam
Click Here
Joint Entrance Examination (JEE)
Click Here
Common Admission Test (CAT)
Click Here
CDS - Combined Defence Services Exam
Click Here