I am trying to unzip all subdirectory and files from a zip file.
Folder Structure
- A folder
- B1 folder (in b1 folder) C1 folder - C2 file
- B2 file
- A1 file
Below my tried codes
Try 1
functio unZip($source, $destiny) {
$zip = zip_open($source);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$fp = fopen("zip/" . zip_entry_name($zip_entry), 'w');
if (zip_entry_open($zip, $zip_entry, 'r')) {
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
fwrite($fp, "$buf");
zip_entry_close($zip_entry);
fclose($fp);
}
}
zip_close($zip);
}
}
Try 2
function unZip($source, $destiny) {
$zips = new ZipArchive;
if($zips->open($source) == TRUE){
$zips->extractTo($destiny);
$zips->close();
echo 'ok';
} else {
echo 'fail';
}
}
Try 3
function unZip($path_file, $destiny) {
if (!file_exists($destiny)) {
mkdir($destiny);
}
$zips = zip_open($path_file);
if ($zips) {
while ($zips_entry = zip_read($zips)) {
$zips_entry_name = str_replace('\\', '/', zip_entry_name($zips_entry));
$file_open = fopen($destiny, 'w');
if (zip_entry_open($zips, $zips_entry, 'w')) {
$buf = zip_entry_read($zips_entry, zip_entry_filesize($zips_entry));
fwrite($file_open, $buf);
zip_entry_close($zips_entry);
fclose($file_open);
}
}
zip_close($zips);
}
}
Error
Warning: fopen(). failed to open stream: Invalid argument