$SRC _file as the file path, upload the file back to the compressed package path
Public function Unzip ($src _file, $dest _dir=false, $create _zip_name_dir=true, $overwrite =true) {
$filePath _arr = [];
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)). " /";
}
If the Create target decompression directory does not exist
$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_name ($zip _entry), "/");
if ($pos _last_slash!== false) {
Create a directory at the end of the band/
$this->create_dirs ($dest _dir.substr (Zip_entry_name ($zip _entry), 0, $pos _last_slash+1));
}
Open the 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 the file needs to be rewritten
if ($overwrite = = = True | | $overwrite = = = False &&!is_file ($file _name)) {
Read the contents of a 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);
}
}
To close a compressed package
Zip_close ($zip);
}
}else{
return false;
}
return $filePath _arr; Returns all file paths after compression
}
/**
* Create a 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);
}
}
}
}
Original link: 45768467 (thank bloggers for sharing)
PHP Unzip the package file to the specified directory implementation