1
0
Fork 0

ZipArchiver: treat backslaches in folder names on Unix (#12327)

main
Philipp Scheit 2025-02-25 12:45:57 +01:00 committed by GitHub
parent 0e7d519958
commit e44b2d8872
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 8 deletions

View File

@ -12,8 +12,9 @@
namespace Composer\Package\Archiver; namespace Composer\Package\Archiver;
use ZipArchive;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Util\Platform;
use ZipArchive;
/** /**
* @author Jan Prieser <jan@prieser.net> * @author Jan Prieser <jan@prieser.net>
@ -44,15 +45,17 @@ class ZipArchiver implements ArchiverInterface
$files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters); $files = new ArchivableFilesFinder($sources, $excludes, $ignoreFilters);
foreach ($files as $file) { foreach ($files as $file) {
/** @var \Symfony\Component\Finder\SplFileInfo $file */ /** @var \Symfony\Component\Finder\SplFileInfo $file */
$filepath = strtr($file->getPath()."/".$file->getFilename(), '\\', '/'); $filepath = $file->getPathname();
$localname = $filepath; $relativePath = $file->getRelativePathname();
if (strpos($localname, $sources . '/') === 0) {
$localname = substr($localname, strlen($sources . '/')); if (Platform::isWindows()) {
$relativePath = strtr($relativePath, '\\', '/');
} }
if ($file->isDir()) { if ($file->isDir()) {
$zip->addEmptyDir($localname); $zip->addEmptyDir($relativePath);
} else { } else {
$zip->addFile($filepath, $localname); $zip->addFile($filepath, $relativePath);
} }
/** /**
@ -64,7 +67,7 @@ class ZipArchiver implements ArchiverInterface
/** /**
* Ensure to preserve the permission umasks for the filepath in the archive. * Ensure to preserve the permission umasks for the filepath in the archive.
*/ */
$zip->setExternalAttributesName($localname, ZipArchive::OPSYS_UNIX, $perms << 16); $zip->setExternalAttributesName($relativePath, ZipArchive::OPSYS_UNIX, $perms << 16);
} }
} }
if ($zip->close()) { if ($zip->close()) {

View File

@ -37,6 +37,17 @@ class ZipArchiverTest extends ArchiverTestCase
]; ];
} }
public function testFolderWithBackslashes(): void
{
if (Platform::isWindows()) {
$this->markTestSkipped('Folder names cannot contain backslashes on Windows.');
}
$this->testZipArchive([
'folder\with\backslashes/README.md' => '# doc',
]);
}
/** /**
* @param array<string, string> $files * @param array<string, string> $files
*/ */