note 61159 modified in ref.zip by danbrown

From: Date: Sun, 25 Oct 2009 13:12:32 +0000
Subject: note 61159 modified in ref.zip by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162061@lists.php.net to get a copy of this message
this function extract all files and subdirectories, you can choose verbose mode for get paths of files extracted. the function return a msg what indicate the error, if msg is OK, all is done. --- code: <?php function unzip($dir, $file, $verbose = 0) { $dir_path = "$dir$file"; $zip_path = "$dir$file.zip"; $ERROR_MSGS[0] = "OK"; $ERROR_MSGS[1] = "Zip path $zip_path doesn't exists."; $ERROR_MSGS[2] = "Directory $dir_path for unzip the pack already exists, impossible continue."; $ERROR_MSGS[3] = "Error while opening the $zip_path file."; $ERROR = 0; if (file_exists($zip_path)) { if (!file_exists($dir_path)) { mkdir($dir_path); if (($link = zip_open($zip_path))) { while (($zip_entry = zip_read($link)) && (!$ERROR)) { if (zip_entry_open($link, $zip_entry, "r")) { $data = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $dir_name = dirname(zip_entry_name($zip_entry)); $name = zip_entry_name($zip_entry); if ($name[strlen($name)-1] == '/') { $base = "$dir_path/"; foreach ( explode("/", $name) as $k) { $base .= "$k/"; if (!file_exists($base)) mkdir($base); } } else { $name = "$dir_path/$name"; if ($verbose) echo "extracting: $name<br>"; $stream = fopen($name, "w"); fwrite($stream, $data); } zip_entry_close($zip_entry); } else $ERROR = 4; } zip_close($link); } else $ERROR = "3"; } else $ERROR = 2; } else $ERROR = 1; return $ERROR_MSGS[$ERROR]; } ?> --- example: <?php $error = unzip("d:/www/dir/", "zipname", 1); echo $error; ?> --- i hope this help you, good bye. --was-- this function extract all files and subdirectories, you can choose verbose mode for get paths of files extracted. the function return a msg what indicate the error, if msg is OK, all is done. --- code: function unzip($dir, $file, $verbose = 0) { $dir_path = "$dir$file"; $zip_path = "$dir$file.zip"; $ERROR_MSGS[0] = "OK"; $ERROR_MSGS[1] = "Zip path $zip_path doesn't exists."; $ERROR_MSGS[2] = "Directory $dir_path for unzip the pack already exists, impossible continue."; $ERROR_MSGS[3] = "Error while opening the $zip_path file."; $ERROR = 0; if (file_exists($zip_path)) { if (!file_exists($dir_path)) { mkdir($dir_path); if (($link = zip_open($zip_path))) { while (($zip_entry = zip_read($link)) && (!$ERROR)) { if (zip_entry_open($link, $zip_entry, "r")) { $data = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $dir_name = dirname(zip_entry_name($zip_entry)); $name = zip_entry_name($zip_entry); if ($name[strlen($name)-1] == '/') { $base = "$dir_path/"; foreach ( explode("/", $name) as $k) { $base .= "$k/"; if (!file_exists($base)) mkdir($base); } } else { $name = "$dir_path/$name"; if ($verbose) echo "extracting: $name<br>"; $stream = fopen($name, "w"); fwrite($stream, $data); } zip_entry_close($zip_entry); } else $ERROR = 4; } zip_close($link); } else $ERROR = "3"; } else $ERROR = 2; } else $ERROR = 1; return $ERROR_MSGS[$ERROR]; } --- example: $error = unzip("d:/www/dir/", "zipname", 1); echo $error; --- i hope this help you, good bye. http://php.net/manual/en/ref.zip.php

« previous php.notes (#162061) next »