ZipArchiver: treat backslaches in folder names on Unix (#12327)
parent
0e7d519958
commit
e44b2d8872
|
@ -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()) {
|
||||||
|
|
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue