note 31060 deleted from ref.zip by danbrown
| From: | danbrown@php.net | Date: | Sun, 25 Oct 2009 13:18:06 +0000 |
| Subject: | note 31060 deleted from ref.zip by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162075@lists.php.net to get a copy of this message | ||
Note Submitter: NOSP at M dot patrick_allaert(AROBAS)pandora dot be
----
If you just want to unzip everything of a zip file in a directory (and create unexisting
directories), let's try this:
function unzip($file, $path) {
$zip = zip_open($file);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
if (zip_entry_filesize($zip_entry) > 0) {
// str_replace must be used under windows to convert "/" into "\"
$complete_path =
$path.str_replace('/','\\',dirname(zip_entry_name($zip_entry)));
$complete_name = $path.str_replace
('/','\\',zip_entry_name($zip_entry));
if(!file_exists($complete_path)) {
$tmp = '';
foreach(explode('\\',$complete_path) AS $k) {
$tmp .= $k.'\\';
if(!file_exists($tmp)) {
mkdir($tmp, 0777);
}
}
}
if (zip_entry_open($zip, $zip_entry, "r")) {
$fd = fopen($complete_name, 'w');
fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
fclose($fd);
zip_entry_close($zip_entry);
}
}
}
zip_close($zip);
}
}
To use this function:
unzip('c:\\file.zip','c:\\temp\\'); // BE CAREFULL: second argument *MUST*
finish by a "\" ! ("/" under *nix)
Patrick