Last active
December 27, 2015 16:49
-
-
Save alexsegura/7357978 to your computer and use it in GitHub Desktop.
Simple function to add an entire folder to a ZipArchive. Uses getSubPathName to avoid recreating whole disk structure. Uses iterators, no recursion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function addFolderToZip(\ZipArchive $zip, $dir) { | |
$iterator = new \RecursiveIteratorIterator( | |
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), | |
\RecursiveIteratorIterator::SELF_FIRST); | |
$zip->addEmptyDir(basename($dir)); | |
foreach ($iterator as $item) { | |
if ($item->isDir()) { | |
$zip->addEmptyDir(basename($dir) . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); | |
} else { | |
$zip->addFile($item->getPathname(), basename($dir) . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment