PHP: Extract a Zip File’s Contents CodeUnit 09 DEC 2009

PHP-logoThe other day I needed to automate the unzipping and processing of files contained within a zip archive. Using the existing PHP Zip library and some help off the web, this is what I was left with:

(Note, you need to enable php_zip.dll extension within php.ini in order to use the code below)

$zip = zip_open($zipfilename);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen($targetdir . zip_entry_name($zip_entry), "w");
if (zip_entry_open($zip, $zip_entry, "r")) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp,"$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);

Nifty.

Related Posts:

About Craig Lotter

Software developer, husband and dad to two little girls. Writer behind An Exploring South African. I don't have time for myself any more.