Improved Zip::addRecursive

This commit is contained in:
the-djmaze 2023-11-27 18:05:00 +01:00
parent 086f5ebb06
commit 5effa242ae

View file

@ -200,11 +200,14 @@ class ZIP
return true; return true;
} }
public function addRecursive($dir, $ignore = '#/(\\.hg(/|$)|\\.hgignore)#') : void public function addRecursive(string $dir, string $target_dir = '', string $ignore = '#/(\\.hg(/|$)|\\.hgignore)#') : void
{ {
\clearstatcache(); \clearstatcache();
$dir = \rtrim($dir,'\\/') . '/'; $dir = \rtrim($dir,'\\/') . '/';
$dirl = \strlen($dir); $dirl = \strlen($dir);
if ($target_dir) {
$target_dir = \rtrim($target_dir,'\\/') . '/';
}
$iterator = new \RecursiveIteratorIterator( $iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS /*| \FilesystemIterator::FOLLOW_SYMLINKS*/), new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS /*| \FilesystemIterator::FOLLOW_SYMLINKS*/),
\RecursiveIteratorIterator::SELF_FIRST, \RecursiveIteratorIterator::SELF_FIRST,
@ -215,7 +218,7 @@ class ZIP
continue; continue;
} }
if (!$ignore || !\preg_match($ignore, $name)) { if (!$ignore || !\preg_match($ignore, $name)) {
$this->addFile($name, \substr($name, $dirl)); $this->addFile($name, $target_dir . \substr($name, $dirl));
} }
// like: tar --exclude-caches -czf file.tgz * // like: tar --exclude-caches -czf file.tgz *
if (\strpos($name, 'CACHEDIR.TAG')) { if (\strpos($name, 'CACHEDIR.TAG')) {