This article describes how PHP extracts a zip file to a specified folder. Share to everyone for your reference, as follows:
/** * Function: Unzip file in ZIP format * author:friker * date:2015-15-14 * reference:http://php.cn/manual/zh/ref.zip.php * All rights reserved:php.cn */class unzip{Public Function __construct () {//init code here ... header ("Content-type:text/html;c Harset=utf8 "); /** * Unzip the file to the specified directory * * @param the path of a string zip compressed file * @param the destination path of a string decompression file * @param boolean to create a destination folder with the name of the compressed file * @param boolean whether to overwrite files that already exist * * @return Boolean to return success or failure */Public 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_di r = = True)? "." : "/"; if ($dest _dir = = = False) {$dest _dir = substr ($src _file, 0, Strrpos ($src _file, $splitter)). " /"; }//If there is no create target extract directory $this->create_dirs ($dest _dir); Unzip each file while ($zip _entry = Zip_read ($zip)) {//The file is not in the root directory $pos _last_slash = Strrpos (zip_entry_n Ame ($zip _entry), "/"); if ($pos _last_Slash!== false) {//Create directory at the end of the band/$this->create_dirs ($dest _dir.substr (Zip_entry_name ($zip _entry), 0, $pos _last_slash+1)); }//Open Package if (Zip_entry_open ($zip, $zip _entry, "R")) {//file name is saved on disk $file _name = $dest _dir.zip_entry_name ($zip _entry); Check if file needs to override if ($overwrite = = = True | | $overwrite = = = False &&!is_file ($file _name)) {// Reads the contents of the compressed file $fstream = Zip_entry_read ($zip _entry, zip_entry_filesize ($zip _entry)); @file_put_contents ($file _name, $fstream); Set Permissions chmod ($file _name, 0777); echo "Save:". $file _name. " <br/> "; }//Close the entrance zip_entry_close ($zip _entry); }}//Close compressed package zip_close ($zip); }}else{return false; } return true; }/** * Create directory * * Public 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); }}}}}/* using: $z = new Unzip (); $z->unzip ("./bootstrap-3.3.4.zip", './unzipres/', true, false); */