by Bhumi // Apr 4,2012 // 1 comment.
Before Some days I have created one function with the use of zip archive class in PHP.For one site, I want to create zip file of all documents generated in one directory. so user can download all documents via single zip file and for that I have used Zip Archive Class of PHP So lets start it!
Zip file is a compressed file which can mainly stored as .zip extension and its well known compression format to gather datas and its very simple with PHP with Zip Archive class
To create zip File using PHP
Let’s see one example which shows you how to create zip file.Here, I have create one function which create zip file.
|
1 2 3 4 5 6 7 8 9 10 11 |
function Create_zip($source, $destination,$overwrite='') { $zip = new ZipArchive(); if($zip->open($destination,$overwrite? ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE) { $zip->addFile($source);// Add the files to the .zip file $zip->close(); // Closing the zip file } } Create_zip("file.php","Compressed.zip"); |
To extract zip File using PHP
Now, Let’s see how to extract zip file
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function Extract_zip($source, $destination) { $zip = new ZipArchive(); // Call object of ZipArchive if ($zip->open($source) === TRUE) // Open Compressed.zip for extracting all files { $destination = str_replace('\', '/', realpath($destination)); // Will extract all files from master.zip to given path. $zip->extractTo($destination); $zip->close(); } } Extract_zip("Compressed.zip", 'link/'); |
How to Zip a directory in PHP?
Upto now, We have checked how to zip and unzip file but now let’s see how to zip whole directory which contain multiple files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
public function create_zip($source,$destination,$overwrite=’’) { if (!extension_loaded('zip') || !file_exists($source)) { return false; } $zip = new ZipArchive(); $source = str_replace('\', '/', realpath($source)); // to replace path of path to real path if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); foreach ($files as $file) { $file = str_replace('\', '/', realpath($file)); $ext = substr(strrchr($file,'.'),1); // To get extension of files in directory if($ext=='zip') { unlink($file); // if overwrite file,its overwrite zip folder // also within a directory so will unlink it continue; } } if (!$zip->open($destination,$overwrite?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)) { return false; } if (is_dir($source) === true) { $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); //To find all the files (recursively) in a certain directory foreach ($files as $file) { if (is_dir($file) === true) { $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); } else if (is_file($file) === true) { $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); //To add a file to a ZIP archive with the use of its contents } } } else if (is_file($source) === true) { $zip->addFromString(basename($source), file_get_contents($source)); } return $zip->close(); } } |
I hope you have enjoyed this short post. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
To grab content from nested tags in PHP
Recently I am working with scraping of data from external url and for scraping we always need to use preg_match function to get matched data. READ MORE
Check Email address using Network Functions
Today we are going back to basics of PHP with a quick tutorial on how to check email address using network functions of PHP.Network functions READ MORE
WordPress site into maintenance mode Without Plugin
With explained hook function over here,you can make WordPress site in “maintenance mode” easily.This is useful if you want to test a design change or READ MORE
Get English Ordinal Number Suffix in PHP
A post after very long time. Actually busy with work,here i am going to share one quick post about how to get English Ordinal Number READ MORE
To Display XML content using PHP Header
Nowadays web services are become more and more popular and Most commonly used format for exchanging information in web services are XML because XML is READ MORE
Be a Contributor at CreativeDev.Write an article or tutorial to the community and share some of your knowledge!
Fully great stuff here.. Thanks!But if you can do some, maybe you should pidvroe us the web version.Its hard for me to translate all the elements into HTML.Oooh.. I’m a web designer and really hoping I can see the web version for this one, or for the entire stuff here maybe..Thanks.