@@ -46,18 +46,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4646 if (is_file ($ zipFilePath )) {
4747 unlink ($ zipFilePath );
4848 }
49- $ this ->zipDirectory ($ name , $ sourceDir , $ zipFilePath );
49+
50+ $ excludePaths = ['node_modules ' , '.git ' , '.idea ' , '.vscode ' , '__pycache__ ' ];
51+
52+ $ this ->zipDirectory ($ name , $ sourceDir , $ zipFilePath , $ excludePaths );
5053 return self ::SUCCESS ;
5154 }
5255
5356 /**
5457 * @param $name
5558 * @param $sourceDir
5659 * @param $zipFilePath
60+ * @param array $excludePaths
5761 * @return bool
5862 * @throws Exception
5963 */
60- protected function zipDirectory ($ name , $ sourceDir , $ zipFilePath ) {
64+ protected function zipDirectory ($ name , $ sourceDir , $ zipFilePath , array $ excludePaths = []): bool
65+ {
6166 $ zip = new ZipArchive ();
6267
6368 if ($ zip ->open ($ zipFilePath , ZipArchive::CREATE | ZipArchive::OVERWRITE ) !== TRUE ) {
@@ -67,19 +72,34 @@ protected function zipDirectory($name, $sourceDir, $zipFilePath) {
6772 $ sourceDir = realpath ($ sourceDir );
6873
6974 $ files = new RecursiveIteratorIterator (
70- new RecursiveDirectoryIterator ($ sourceDir ),
75+ new RecursiveDirectoryIterator ($ sourceDir, RecursiveDirectoryIterator:: SKIP_DOTS ),
7176 RecursiveIteratorIterator::LEAVES_ONLY
7277 );
7378
7479 foreach ($ files as $ file ) {
7580 if (!$ file ->isDir ()) {
7681 $ filePath = $ file ->getRealPath ();
7782 $ relativePath = $ name . DIRECTORY_SEPARATOR . substr ($ filePath , strlen ($ sourceDir ) + 1 );
83+
84+ // 修正排除目录的判断逻辑,确保所有层级都能排除
85+ $ shouldExclude = false ;
86+ foreach ($ excludePaths as $ excludePath ) {
87+ // 统一路径分隔符为正斜杠,兼容 Windows
88+ $ normalizedRelativePath = str_replace ('\\' , '/ ' , $ relativePath );
89+ $ normalizedExcludePath = str_replace ('\\' , '/ ' , $ excludePath );
90+ if (preg_match ('#/(?: ' . preg_quote ($ normalizedExcludePath , '# ' ) . ')(/|$)#i ' , $ normalizedRelativePath )) {
91+ $ shouldExclude = true ;
92+ break ;
93+ }
94+ }
95+ if ($ shouldExclude ) {
96+ continue ;
97+ }
98+
7899 $ zip ->addFile ($ filePath , $ relativePath );
79100 }
80101 }
81102
82103 return $ zip ->close ();
83104 }
84-
85105}
0 commit comments