PHP Zip: Extract contents of a directory -
i need extract contents of directory within zip archive in output directory.
the directory name inside zip anything. directory in base of zip archive. there number of files in directory, in zip archive, though.
the file structure inside zip along theses lines:
- d0001 - folder - view.php - tasks.txt - file1.txt - picture1.png - document.doc
the contents of output directory needs this:
- folder - view.php - tasks.txt - file1.txt - picture1.png - document.doc
the code have deletes contents of output directory , extracts entire zip archive in directory:
function unzip($source, $destination) { $zip = new ziparchive; $res = $zip->open($source); if($res === true) { $zip->extractto($destination); $zip->close(); return true; } else { return false; } } function rrmdir($dir, $removebase = true) { if(is_dir($dir)) { $objects = scandir($dir); foreach($objects $object) { if($object != "." && $object != "..") { if(filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); if($removebase == true) rmdir($dir); } } $filename = '/home/files.zip'; $dest = '/home/myfiles/'; if(is_dir($dest)) { rrmdir($dest, false); $unzip = unzip($filename, $dest); if($unzip === true) { echo 'success'; } else echo 'extraction of zip failed.'; } else echo 'the output directory not exist!';
all function rrmdir()
remove contents of output directory.
i managed work working file streams manually instead of using extractto()
.
the script extracts files in base of archive , files in directories in base of archive in output folder.
for example, if archives contents:
- d0001 - folder - view.php - tasks.txt - file1.txt - picture1.png - document.doc - d2 - another1.png - pic2.gif - doc1.txt - mylist.txt
the contents of output directory this:
- folder - view.php - tasks.txt - file1.txt - picture1.png - document.doc - another1.png - pic.gif - doc1.txt - mylist.txt
the code:
function rrmdir($dir, $removebase = true) { if(is_dir($dir)) { $objects = scandir($dir); foreach($objects $object) { if($object != "." && $object != "..") { if(filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); } } reset($objects); if($removebase == true) rmdir($dir); } } $filename = '/home/files.zip'; $dest = '/home/myfiles/'; if(is_dir($dest)) { // remove directory's contents rrmdir($dest, false); // load zip $zip = new ziparchive; $unzip = $zip->open($filename); if($unzip === true) { for($i=0; $i<$zip->numfiles; $i++) { $name = $zip->getnameindex($i); // remove first directory in string if necessary $parts = explode('/', $name); if(count($parts) > 1) { array_shift($parts); } $file = $dest . '/' . implode('/', $parts); // create directories if necessary $dir = dirname($file); if(!is_dir($dir)) mkdir($dir, 0777, true); // check if $name file or directory if(substr($file, -1) == "/") { // $name directory // create directory if(!is_dir($file)) mkdir($file, 0777, true); } else { // $name file // read zip , write disk $fpr = $zip->getstream($name); $fpw = fopen($file, 'w'); while($data = fread($fpr, 1024)) { fwrite($fpw, $data); } fclose($fpr); fclose($fpw); } } echo 'success'; } else echo 'extraction of zip failed.'; } else echo 'the output directory not exist!';
Comments
Post a Comment