NewLife/LuckyClover

如果fullName是当前工作目录的子目录,则以当前工作目录为根目录
智能大石头 authored at 2025-05-29 17:28:47
ba34145
Tree
1 Parent(s) 3b269f2
Summary: 2 changed files with 16 additions and 0 deletions.
Modified +8 -0
Modified +8 -0
Modified +8 -0
diff --git a/LuckyClover/TarCommand.cs b/LuckyClover/TarCommand.cs
index 59044a2..e5fbd3a 100644
--- a/LuckyClover/TarCommand.cs
+++ b/LuckyClover/TarCommand.cs
@@ -36,8 +36,10 @@ internal class TarCommand
         else
         {
             var dst = args[1];
+            var root = Environment.CurrentDirectory;
 
             Console.WriteLine("Tar打包多个文件到 \e[32;1m{0}\e[0m", dst);
+            Console.WriteLine("当前工作目录 \e[31;1m{0}\e[0m", root);
 
             if (File.Exists(dst)) File.Delete(dst);
 
@@ -79,6 +81,12 @@ internal class TarCommand
                 var fullName = di.FullName;
                 Console.WriteLine("压缩目录:\e[32;1m{0}\e[0m 匹配:\e[32;1m{1}\e[0m", fullName, pt);
 
+                // 如果fullName是当前工作目录的子目录,则以当前工作目录为根目录
+                if (fullName.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
+                {
+                    fullName = root;
+                }
+
                 //// 没有匹配项时,该路径作为一个子目录
                 //if (String.IsNullOrEmpty(pt))
                 //{
Modified +8 -0
diff --git a/LuckyClover/ZipCommand.cs b/LuckyClover/ZipCommand.cs
index 24ca8d4..548bfb5 100644
--- a/LuckyClover/ZipCommand.cs
+++ b/LuckyClover/ZipCommand.cs
@@ -31,8 +31,10 @@ internal class ZipCommand
         {
 #if NET7_0_OR_GREATER
             var dst = args[1];
+            var root = Environment.CurrentDirectory;
 
             Console.WriteLine("Zip压缩多个文件到 \e[31;1m{0}\e[0m", dst);
+            Console.WriteLine("当前工作目录 \e[31;1m{0}\e[0m", root);
 
             if (File.Exists(dst)) File.Delete(dst);
 
@@ -71,6 +73,12 @@ internal class ZipCommand
                 var fullName = di.FullName;
                 Console.WriteLine("压缩目录:\e[32;1m{0}\e[0m 匹配:\e[32;1m{1}\e[0m", fullName, pt);
 
+                // 如果fullName是当前工作目录的子目录,则以当前工作目录为根目录
+                if (fullName.StartsWith(root + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase))
+                {
+                    fullName = root;
+                }
+
                 // 没有匹配项时,该路径作为一个子目录
                 if (String.IsNullOrEmpty(pt))
                 {