Skip to content

Commit c703ffe

Browse files
committed
Fix windows 7z unzip strip function
1 parent 487980c commit c703ffe

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/SPC/store/FileSystem.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ private static function emitSourceExtractHook(string $name, string $target): voi
635635

636636
private static function extractWithType(string $source_type, string $filename, string $extract_path): void
637637
{
638-
logger()->debug('Extracting source [' . $source_type . ']: ' . $filename);
638+
logger()->debug("Extracting source [{$source_type}]: {$filename}");
639639
/* @phpstan-ignore-next-line */
640640
match ($source_type) {
641641
SPC_SOURCE_ARCHIVE => self::extractArchive($filename, $extract_path),
@@ -680,23 +680,38 @@ private static function unzipWithStrip(string $zip_file, string $extract_path):
680680
if (count($contents) === 1 && is_dir($subdir)) {
681681
rename($subdir, $extract_path);
682682
} else {
683-
// else, move all contents to extract_path
684-
self::createDir($extract_path);
683+
// else, if it contains only one dir, strip dir and copy other files
684+
$dircount = 0;
685+
$dir = [];
686+
$top_files = [];
685687
foreach ($contents as $item) {
686-
$subdir = self::convertPath("{$temp_dir}/{$item}");
687-
if (is_dir($subdir)) {
688-
// move all dir contents to extract_path (strip top-level)
689-
$sub_contents = self::scanDirFiles($subdir, false, true, true);
690-
if ($sub_contents === false) {
691-
throw new FileSystemException('Cannot scan unzip temp sub-dir: ' . $subdir);
692-
}
693-
foreach ($sub_contents as $sub_item) {
694-
rename(self::convertPath("{$subdir}/{$sub_item}"), self::convertPath("{$extract_path}/{$sub_item}"));
695-
}
688+
if (is_dir(self::convertPath("{$temp_dir}/{$item}"))) {
689+
++$dircount;
690+
$dir[] = $item;
696691
} else {
692+
$top_files[] = $item;
693+
}
694+
}
695+
// extract dir contents to extract_path
696+
self::createDir($extract_path);
697+
// extract move dir
698+
if ($dircount === 1) {
699+
$sub_contents = self::scanDirFiles("{$temp_dir}/{$dir[0]}", false, true, true);
700+
if ($sub_contents === false) {
701+
throw new FileSystemException("Cannot scan unzip temp sub-dir: {$dir[0]}");
702+
}
703+
foreach ($sub_contents as $sub_item) {
704+
rename(self::convertPath("{$temp_dir}/{$dir[0]}/{$sub_item}"), self::convertPath("{$extract_path}/{$sub_item}"));
705+
}
706+
} else {
707+
foreach ($dir as $item) {
697708
rename(self::convertPath("{$temp_dir}/{$item}"), self::convertPath("{$extract_path}/{$item}"));
698709
}
699710
}
711+
// move top-level files to extract_path
712+
foreach ($top_files as $top_file) {
713+
rename(self::convertPath("{$temp_dir}/{$top_file}"), self::convertPath("{$extract_path}/{$top_file}"));
714+
}
700715
}
701716
}
702717
}

0 commit comments

Comments
 (0)