note 31059 added to ref.zip
| From: | NOSP at M dot patrick_allaert | Date: | Tue, 08 Apr 2003 15:08:18 +0000 |
| Subject: | note 31059 added to ref.zip | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-46361@lists.php.net to get a copy of this message | ||
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
--
http://www.php.net/manual/en/ref.zip.php
http://master.php.net/manage/user-notes.php?action=edit+31059
http://master.php.net/manage/user-notes.php?action=delete+31059
http://master.php.net/manage/user-notes.php?action=reject+31059