note 75079 added to ref.zip

From: Date: Fri, 11 May 2007 09:29:06 +0000
Subject: note 75079 added to ref.zip
Groups: php.notes 
Request: Send a blank email to php-notes+get-126405@lists.php.net to get a copy of this message
This is the function I use to unzip a file. It includes the following options: * Unzip in any directory you like * Unzip in the directory of the zip file * Unzip in a directory with the zipfile's name in the directory of the zip file. (i.e.: C:\test.zip will be unzipped in C:\test\) * Overwrite existing files or not * It creates non existing directories with the function Create_dirs($path) You should use absolute paths with slashes (/) instead of backslashes (\). I tested it with PHP 5.2.0 with php_zip.dll extension loaded <?php /** * Unzip the source_file in the destination dir * * @param string The path to the ZIP-file. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!) * @param boolean Overwrite existing files (true) or not (false) * * @return boolean Succesful or not */ function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true) { if ($zip = zip_open($src_file)) { if ($zip) { $splitter = ($create_zip_name_dir === true) ? "." : "/"; if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/"; // Create the directories to the destination dir if they don't already exist create_dirs($dest_dir); // For every file in the zip-packet while ($zip_entry = zip_read($zip)) { // Now we're going to create the directories in the destination directories // If the file is not in the root dir $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/"); if ($pos_last_slash !== false) { // Create the directory where the zip-entry should be saved (with a "/" at the end) create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1)); } // Open the entry if (zip_entry_open($zip,$zip_entry,"r")) { // The name of the file to save on the disk $file_name = $dest_dir.zip_entry_name($zip_entry); // Check if the files should be overwritten or not if ($overwrite === true || $overwrite === false && !is_file($file_name)) { // Get the content of the zip entry $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); file_put_contents($file_name, $fstream ); // Set the rights chmod($file_name, 0777); echo "save: ".$file_name."<br />"; } // Close the entry zip_entry_close($zip_entry); } } // Close the zip-file zip_close($zip); } } else { return false; } return true; } /** * This function creates recursive directories if it doesn't already exist * * @param String The path that should be created * * @return void */ function create_dirs($path) { if (!is_dir($path)) { $directory_path = ""; $directories = explode("/",$path); array_pop($directories); foreach($directories as $directory) { $directory_path .= $directory."/"; if (!is_dir($directory_path)) { mkdir($directory_path); chmod($directory_path, 0777); } } } } // Extract C:/zipfiletest/zip-file.zip to C:/zipfiletest/zip-file/ and overwrites existing files unzip("C:/zipfiletest/zip-file.zip", false, true, true); // Extract C:/zipfiletest/zip-file.zip to C:/another_map/zipfiletest/ and doesn't overwrite existing files. NOTE: It doesn't create a map with the zip-file-name! unzip("C:/zipfiletest/zip-file.zip", "C:/another_map/zipfiletest/", true, false); ?> ---- Server IP: 83.137.20.120 Probable Submitter: 82.204.37.70 ---- Manual Page -- http://www.php.net/manual/en/ref.zip.php Edit -- https://master.php.net/note/edit/75079 Del: integrated -- https://master.php.net/note/delete/75079/integrated Del: useless -- https://master.php.net/note/delete/75079/useless Del: bad code -- https://master.php.net/note/delete/75079/bad+code Del: spam -- https://master.php.net/note/delete/75079/spam Del: non-english -- https://master.php.net/note/delete/75079/non-english Del: in docs -- https://master.php.net/note/delete/75079/in+docs Del: other reasons-- https://master.php.net/note/delete/75079 Reject -- https://master.php.net/note/reject/75079 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#126405) next »