ZipArchive()
[ Create Zip file using class in PHP ]
ZipArchive()
Is a class used to perform ZIP operations in PHP. Using ZipArchive()
the 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 above code creates two text files with some content and adds them to a zip file.
Output:
ZipArchive()
Create Zip
files using classes in PHP
Let's use PHP's ZipArchive()
class to extract the zip file created in the first code.
<?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 code above will extract the file created in the first example New.zip
.
Output:
How to use PHP Zip
extension to get ZIP
information about all member files of
The PHP ZIP
extension can obtain ZIP
information about all files within.
<?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 above code uses the built-in ZIP
function to obtain ZIP
the file information within.
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.
[Create a Zip file using PHP]
The following example code will create a zip file tutorial.zip
and add the files.
Within our htdocs
directory we have Files to zip
folders containing two files. We will zip the directory Files to zip
.
<?php
// Enter the name of directory
$pathdir = "Files to zip/";
//Create a name for the zip folder
$zipcreated = "Files to zip.zip";
// Create new zip class
$zip = new ZipArchive;
if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
// Store the files in our `Files to zip` zip file.
$dir = opendir($pathdir);
while($file = readdir($dir)) {
if(is_file($pathdir.$file)) {
$zip -> addFile($pathdir.$file, $file);
}
}
}
?>
This code creates a new zip in our root directory, Files to zip
as shown below.
We use the directory Files to zip
to create the zip folder Files to zip
. Our directory has two files, insert.php
and Loader.php
.
We should find the above files in our zip folder. let's see.
[Unzip Zip files using PHP]
Let's see how to unzip a zip file using PHP.
example:
<?php
// Create new zip class
$zip = new ZipArchive;
// Add zip filename which needs to unzip
$zip->open('Files to zip.zip');
// Extracts to current directory
$zip->extractTo('./Files to zip');
$zip->close();
?>
Output:
We use code to Files to zip.zip
unzip to Files to zip
a folder.