From 8389b4b829429164327ca3f63bde68fd18e60dc4 Mon Sep 17 00:00:00 2001 From: Tom Klingenberg Date: Sat, 2 Apr 2016 20:50:20 +0200 Subject: [PATCH] Skip non-empty directories in zip generation Empty dirs get archived (leafs). Previously it seemed ok to skip all directories in zip generation. References: - Ref: 6066359944642e47d03c2dd367c9e43af787a9f7 - Issue: #4865 Keep empty folders after re-zip a module --- .../Package/Archiver/ArchivableFilesFinder.php | 12 +++++++++++- src/Composer/Package/Archiver/ZipArchiver.php | 6 +++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Composer/Package/Archiver/ArchivableFilesFinder.php b/src/Composer/Package/Archiver/ArchivableFilesFinder.php index 1e94f1e15..92e281a2a 100644 --- a/src/Composer/Package/Archiver/ArchivableFilesFinder.php +++ b/src/Composer/Package/Archiver/ArchivableFilesFinder.php @@ -13,7 +13,9 @@ namespace Composer\Package\Archiver; use Composer\Util\Filesystem; +use FilesystemIterator; use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; /** * A Symfony Finder wrapper which locates files that should go into archives @@ -84,6 +86,14 @@ class ArchivableFilesFinder extends \FilterIterator public function accept() { - return !$this->getInnerIterator()->current()->isDir(); + /** @var SplFileInfo $current */ + $current = $this->getInnerIterator()->current(); + + if (!$current->isDir()) { + return true; + } + + $iterator = new FilesystemIterator($current, FilesystemIterator::SKIP_DOTS); + return !$iterator->valid(); } } diff --git a/src/Composer/Package/Archiver/ZipArchiver.php b/src/Composer/Package/Archiver/ZipArchiver.php index bc5719dce..310addaea 100644 --- a/src/Composer/Package/Archiver/ZipArchiver.php +++ b/src/Composer/Package/Archiver/ZipArchiver.php @@ -37,7 +37,11 @@ class ZipArchiver implements ArchiverInterface /** @var $file \SplFileInfo */ $filepath = $file->getPath()."/".$file->getFilename(); $localname = str_replace($sources."/", '', $filepath); - $zip->addFile($filepath, $localname); + if ($file->isDir()) { + $zip->addEmptyDir($localname); + } else { + $zip->addFile($filepath, $localname); + } } if ($zip->close()) { return $target;