From d4af33f5be580153a3cde5b04ef5a6fc0e15c237 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com>
Date: Tue, 12 May 2026 15:12:32 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A9=B1=E5=8A=A8=E5=99=A8=E6=A0=B9?=
=?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=85=BC=E5=AE=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Shared/General/Logger.cs | 2 +-
Shared/General/MsDelta.cs | 4 ++--
Shared/Utils/DirectoryUtils.cs | 27 ++++++++++++-------------
Shared/Utils/FileUtils.cs | 37 ++++++++++++++++------------------
Shared/Utils/PathUtils.cs | 26 ++++++++++++++++--------
5 files changed, 51 insertions(+), 45 deletions(-)
diff --git a/Shared/General/Logger.cs b/Shared/General/Logger.cs
index f06d946..6bd9f3f 100644
--- a/Shared/General/Logger.cs
+++ b/Shared/General/Logger.cs
@@ -314,7 +314,7 @@ public class FileLogger : BaseLogger, IDisposable {
Logger.Warn(ex, "整理日志文件失败");
}
// 新建 writer
- writer = new StreamWriter(PathUtils.WithLongPath(Path.Combine(logFolder, $"{fileNamePrefix}1{fileNameSuffix}")), append: true) { AutoFlush = true };
+ writer = new StreamWriter(PathUtils.ForApi(Path.Combine(logFolder, $"{fileNamePrefix}1{fileNameSuffix}")), append: true) { AutoFlush = true };
}
///
diff --git a/Shared/General/MsDelta.cs b/Shared/General/MsDelta.cs
index 3333641..f511dc4 100644
--- a/Shared/General/MsDelta.cs
+++ b/Shared/General/MsDelta.cs
@@ -20,7 +20,7 @@ public static class MsDelta {
// fileTypeSet:https://learn.microsoft.com/en-us/previous-versions/bb417345(v=msdn.10)?redirectedfrom=MSDN#file-type-sets (15L: DELTA_FILE_TYPE_SET_EXECUTABLES)
// 131072L: IgnoreFileSizeLimit, 32u: Crc32
if (!CreateDelta(15L, 131072L, 0L,
- PathUtils.WithLongPath(oldFilePath), PathUtils.WithLongPath(newFilePath), null, null, new DeltaInput(), IntPtr.Zero, 32u, PathUtils.WithLongPath(deltaFilePath)))
+ PathUtils.ForApi(oldFilePath), PathUtils.ForApi(newFilePath), null, null, new DeltaInput(), IntPtr.Zero, 32u, PathUtils.ForApi(deltaFilePath)))
throw new Win32Exception();
}
@@ -35,7 +35,7 @@ public static class MsDelta {
DirectoryUtils.Create(PathUtils.RemoveLastPart(newFilePath));
Logger.Info($"正在应用补丁:原始文件 {oldFilePath} + 补丁文件 {deltaFilePath} → 输出至 {newFilePath}");
// 1L: AllowLegacy
- if (!ApplyDelta(1L, PathUtils.WithLongPath(oldFilePath), PathUtils.WithLongPath(deltaFilePath), PathUtils.WithLongPath(newFilePath)))
+ if (!ApplyDelta(1L, PathUtils.ForApi(oldFilePath), PathUtils.ForApi(deltaFilePath), PathUtils.ForApi(newFilePath)))
throw new Win32Exception();
}
diff --git a/Shared/Utils/DirectoryUtils.cs b/Shared/Utils/DirectoryUtils.cs
index 8b5e2d5..53fb8db 100644
--- a/Shared/Utils/DirectoryUtils.cs
+++ b/Shared/Utils/DirectoryUtils.cs
@@ -8,21 +8,20 @@ public static class DirectoryUtils {
public static void Create(string path) {
if (DirectoryUtils.Exists(path)) return;
Logger.Trace($"新建文件夹:{path}");
- path = PathUtils.WithLongPath(path);
- Directory.CreateDirectory(path);
+ Directory.CreateDirectory(PathUtils.ForApi(path));
}
///
/// 判断文件夹是否存在。
///
public static bool Exists(string path)
- => Directory.Exists(PathUtils.WithLongPath(path));
+ => Directory.Exists(PathUtils.ForApi(path));
///
/// 获取 对象。
///
public static DirectoryInfo GetInfo(string path)
- => new(PathUtils.WithLongPath(path));
+ => new(PathUtils.ForApi(path));
///
/// 返回指定路径下的所有文件,不以 \\?\ 开头。
@@ -30,7 +29,7 @@ public static class DirectoryUtils {
///
public static IEnumerable GetFiles(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
- return Directory.EnumerateFiles(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
+ return Directory.EnumerateFiles(PathUtils.ForApi(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
Select(PathUtils.WithoutLongPath);
}
@@ -40,7 +39,7 @@ public static class DirectoryUtils {
///
public static IEnumerable GetDirectories(string path, bool topDirectoryOnly = false, string searchPattern = "*") {
if (!DirectoryUtils.Exists(path)) return [];
- return Directory.EnumerateDirectories(PathUtils.WithLongPath(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
+ return Directory.EnumerateDirectories(PathUtils.ForApi(path), searchPattern, topDirectoryOnly ? SearchOption.TopDirectoryOnly : SearchOption.AllDirectories).
Select(PathUtils.WithoutLongPath);
}
@@ -50,7 +49,7 @@ public static class DirectoryUtils {
///
public static bool IsEmpty(string path) {
if (!DirectoryUtils.Exists(path)) return true;
- return !Directory.EnumerateFileSystemEntries(PathUtils.WithLongPath(path)).Any();
+ return !Directory.EnumerateFileSystemEntries(PathUtils.ForApi(path)).Any();
}
///
@@ -63,8 +62,8 @@ public static class DirectoryUtils {
progressHandler?.Invoke(1);
return;
}
- sourceFolder = PathUtils.Normalize(sourceFolder, true);
- destFolder = PathUtils.Normalize(destFolder, true);
+ sourceFolder = PathUtils.ForCompare(sourceFolder);
+ destFolder = PathUtils.ForCompare(destFolder);
int doneCount = 0;
Lazy totalCount = new(() => DirectoryUtils.GetFiles(sourceFolder).Count());
Logger.Trace($"复制文件夹:{sourceFolder} → {destFolder}");
@@ -82,7 +81,7 @@ public static class DirectoryUtils {
if (!DirectoryUtils.Exists(sourceFolder)) return;
DirectoryUtils.Delete(destFolder); // Move 要求不存在对应文件夹
Logger.Trace($"剪切文件夹:{sourceFolder} → {destFolder}");
- Directory.Move(PathUtils.WithLongPath(sourceFolder), PathUtils.WithLongPath(destFolder));
+ Directory.Move(PathUtils.ForApi(sourceFolder), PathUtils.ForApi(destFolder));
}
///
@@ -92,8 +91,8 @@ public static class DirectoryUtils {
// 安全检查
if (!DirectoryUtils.Exists(folder)) return;
Logger.Trace($"{(toRecycleBin ? "将文件夹删除到回收站" : "删除文件夹")}:{folder}");
- folder = PathUtils.Normalize(folder, true);
- if (folder == Path.GetPathRoot(folder))
+ folder = PathUtils.ForCompare(folder);
+ if (folder == PathUtils.ForCompare(Path.GetPathRoot(folder)))
throw new UnauthorizedAccessException($"不应删除磁盘根目录:{folder}");
if (criticalFolders.Value.Any(f => f.StartsWithF(folder, ignoreCase: true)))
throw new UnauthorizedAccessException($"不应删除的文件夹:{folder}");
@@ -104,7 +103,7 @@ public static class DirectoryUtils {
DeleteInternal(folder); // 永久删除
static void DeleteInternal(string folder) {
try {
- folder = PathUtils.WithLongPath(folder);
+ folder = PathUtils.ForApi(folder);
foreach (string filePath in DirectoryUtils.GetFiles(folder, true)) FileUtils.Delete(filePath); // 删除文件
foreach (string str in DirectoryUtils.GetDirectories(folder, true)) DeleteInternal(str); // 递归删除子文件夹
ResilientUtils.RetryOn(() => Directory.Delete(folder, true)); // 最终删除文件夹
@@ -125,6 +124,6 @@ public static class DirectoryUtils {
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads") // 下载文件夹没有 SpecialFolder 枚举
- }.Select(f => PathUtils.Normalize(f, true))));
+ }.Select(f => PathUtils.ForCompare(f))));
}
diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs
index b8740b8..7c8069f 100644
--- a/Shared/Utils/FileUtils.cs
+++ b/Shared/Utils/FileUtils.cs
@@ -9,13 +9,13 @@ public static class FileUtils {
/// 打开指定文件的只读 。
///
public static FileStream ReadAsStream(string filePath)
- => new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
+ => new(PathUtils.ForApi(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
///
/// 读取文件中的所有内容。
///
public static byte[] ReadAsBytes(string filePath) {
- using Stream fs = ReadAsStream(filePath); // 不能使用 File.ReadAllBytes,它不指定 FileShare.ReadWrite,会在文件被占用时抛出异常
+ using Stream fs = FileUtils.ReadAsStream(filePath); // 不能使用 File.ReadAllBytes,它不指定 FileShare.ReadWrite,会在文件被占用时抛出异常
using MemoryStream ms = new();
fs.CopyTo(ms);
return ms.ToArray();
@@ -75,7 +75,7 @@ public static class FileUtils {
DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath));
Logger.Trace($"写入文件:{filePath}({content.Length} 字节)");
ResilientUtils.RetryOn(()
- => File.WriteAllBytes(PathUtils.WithLongPath(filePath), content));
+ => File.WriteAllBytes(PathUtils.ForApi(filePath), content));
}
///
@@ -97,7 +97,7 @@ public static class FileUtils {
DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath));
Logger.Trace($"创建文件流:{filePath}");
return ResilientUtils.RetryOn(()
- => new FileStream(PathUtils.WithLongPath(filePath), FileMode.Create));
+ => new FileStream(PathUtils.ForApi(filePath), FileMode.Create));
}
#endregion
@@ -118,7 +118,7 @@ public static class FileUtils {
DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath));
Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}");
ResilientUtils.RetryOn(()
- => File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true));
+ => File.Copy(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath), true));
}
///
@@ -131,7 +131,7 @@ public static class FileUtils {
FileUtils.Delete(destFilePath);
Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
ResilientUtils.RetryOn(()
- => File.Move(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath)));
+ => File.Move(PathUtils.ForApi(sourceFilePath), PathUtils.ForApi(destFilePath)));
}
#endregion
@@ -148,7 +148,7 @@ public static class FileUtils {
DeleteToRecycleBin(filePath);
} else {
ResilientUtils.RetryOn(()
- => File.Delete(PathUtils.WithLongPath(filePath)));
+ => File.Delete(PathUtils.ForApi(filePath)));
}
}
@@ -234,7 +234,7 @@ public static class FileUtils {
public static ZipArchive OpenZip(string zipFilePath) {
ZipArchive TryOpen(Encoding encoding) {
Logger.Trace($"尝试以 {encoding.EncodingName} 编码打开压缩包:{zipFilePath}");
- var result = ZipFile.Open(PathUtils.WithLongPath(zipFilePath), ZipArchiveMode.Read, encoding);
+ var result = ZipFile.Open(PathUtils.ForApi(zipFilePath), ZipArchiveMode.Read, encoding);
try {
_ = result.Entries; // 如果编码有误,会在这里抛出 DecoderFallbackException;如果文件异常,会在这里抛出 InvalidDataException
return result;
@@ -260,7 +260,7 @@ public static class FileUtils {
///
/// 参数为已完成的总比例(0~1)。
public static void ExtractToDirectory(string compressionFile, string outputDirectory, Action? progressHandler = null) {
- compressionFile = PathUtils.WithLongPath(compressionFile);
+ compressionFile = PathUtils.ForApi(compressionFile);
DirectoryUtils.Create(outputDirectory);
// 解压 gz(gz 不需要考虑编码)
if (compressionFile.EndsWithF(".gz", true)) {
@@ -282,8 +282,8 @@ public static class FileUtils {
if (totalCount > 0) progressHandler?.Invoke((double) doneCount / totalCount);
if (string.IsNullOrEmpty(entry.Name)) continue; // 跳过文件夹条目(ZipArchive 会将文件夹也作为一个 entry,但它们的 Name 为空)
// ZipSlip 修复
- string outputFilePath = PathUtils.WithLongPath(Path.GetFullPath(Path.Combine(outputDirectory, entry.FullName)));
- if (!outputFilePath.StartsWithF(PathUtils.WithSeparator(PathUtils.WithLongPath(Path.GetFullPath(outputDirectory))), ignoreCase:true))
+ string outputFilePath = PathUtils.ForCompare(Path.Combine(outputDirectory, entry.FullName));
+ if (!outputFilePath.StartsWithF(PathUtils.WithSeparator(PathUtils.ForCompare(outputDirectory)), ignoreCase:true))
throw new ZipSlipException($"Zip 文件项 {entry.FullName} 的路径在压缩包之外,这可能导致安全问题");
// 实际的解压
using var entryStream = entry.Open();
@@ -297,13 +297,11 @@ public static class FileUtils {
/// 会自动创建文件夹。会覆盖已有文件。
///
public static void CreateZipFromDirectory(string outputFullPath, string sourceDirectory) {
- outputFullPath = PathUtils.WithLongPath(outputFullPath);
- sourceDirectory = PathUtils.WithLongPath(sourceDirectory);
DirectoryUtils.Create(PathUtils.RemoveLastPart(outputFullPath));
FileUtils.Delete(outputFullPath);
Logger.Trace($"将文件夹中的内容压缩为 zip 文件:{sourceDirectory} → {outputFullPath}");
ResilientUtils.RetryOn(()
- => ZipFile.CreateFromDirectory(sourceDirectory, outputFullPath));
+ => ZipFile.CreateFromDirectory(PathUtils.ForApi(sourceDirectory), PathUtils.ForApi(outputFullPath)));
}
///
@@ -317,7 +315,7 @@ public static class FileUtils {
if (sources.ContainsKey(fileName)) throw new ArgumentException($"尝试将多个同文件名的文件放进压缩包中({fileName})", nameof(sourceFiles));
sources.Add(fileName, source);
}
- CreateZipFromFiles(outputFullPath, sources);
+ FileUtils.CreateZipFromFiles(outputFullPath, sources);
}
///
@@ -326,13 +324,12 @@ public static class FileUtils {
///
/// 键为 zip 文件下的路径,值为文件的本地路径。
public static void CreateZipFromFiles(string outputFullPath, IDictionary sources) {
- outputFullPath = PathUtils.WithLongPath(outputFullPath);
DirectoryUtils.Create(PathUtils.RemoveLastPart(outputFullPath));
FileUtils.Delete(outputFullPath);
using var archive = ResilientUtils.RetryOn(()
- => ZipFile.Open(outputFullPath, ZipArchiveMode.Create));
+ => ZipFile.Open(PathUtils.ForApi(outputFullPath), ZipArchiveMode.Create));
Logger.Trace($"创建 zip 文件:{sources.Count} 个文件 → {outputFullPath}\n{sources.Select(p => $"- {p.Value} → {p.Key}").Join('\n')}");
- foreach (var pair in sources) archive.CreateEntryFromFile(PathUtils.WithLongPath(pair.Value), pair.Key.Replace('\\', '/'), CompressionLevel.Optimal);
+ foreach (var pair in sources) archive.CreateEntryFromFile(PathUtils.ForApi(pair.Value), pair.Key.Replace('\\', '/'), CompressionLevel.Optimal);
}
#endregion
@@ -341,12 +338,12 @@ public static class FileUtils {
/// 确定指定的文件是否存在。
///
public static bool Exists(string filePath)
- => File.Exists(PathUtils.WithLongPath(filePath));
+ => File.Exists(PathUtils.ForApi(filePath));
///
/// 获取 对象。
///
public static FileInfo GetInfo(string path)
- => new(PathUtils.WithLongPath(path));
+ => new(PathUtils.ForApi(path));
}
diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs
index e851934..890320b 100644
--- a/Shared/Utils/PathUtils.cs
+++ b/Shared/Utils/PathUtils.cs
@@ -21,12 +21,12 @@ public static class PathUtils {
///
/// 若路径较长,则尽量将其转换为短路径。
- /// 若输入的是文件夹路径,不保证其结尾是否有文件夹分隔符。
+ /// 结果的开头不含 \\?\、结尾不含分隔符。
///
public static string ToShortPath(string fullName, bool keepFileName = false) {
if (string.IsNullOrEmpty(fullName)) return fullName;
+ fullName = PathUtils.ForCompare(fullName);
if (!fullName.Contains(":")) return fullName;
- fullName = PathUtils.Normalize(fullName, false);
if (fullName.Length <= 200) return fullName;
// 保留文件名
@@ -49,10 +49,10 @@ public static class PathUtils {
// 缩短路径
char[] buffer = new char[260];
- int result = GetShortPathNameW(PathUtils.WithLongPath(pathToShorten), buffer, buffer.Length);
+ int result = GetShortPathNameW(PathUtils.ForApi(pathToShorten), buffer, buffer.Length);
if (result == 0) return fullName;
string shortPath = new(buffer, 0, result);
- return PathUtils.WithoutLongPath(Path.Combine(shortPath, pathToKeep));
+ return PathUtils.WithoutSeparator(PathUtils.WithoutLongPath(Path.Combine(shortPath, pathToKeep)));
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer);
@@ -147,11 +147,21 @@ public static class PathUtils {
}
///
- /// 将路径转换为标准格式:将短路径展开,将分隔符改为 \,去除前导的 \\?\,统一末尾是否包含分隔符。
+ /// 统一路径格式,以便比较。
+ /// 具体而言:将短路径展开,去除前导的 \\?\,将分隔符改为 \,去除末尾的分隔符。
///
- public static string Normalize(string path, bool withSeparator) {
- path = PathUtils.WithoutLongPath(Path.GetFullPath(path)).Replace("/", @"\");
- return withSeparator ? PathUtils.WithSeparator(path) : PathUtils.WithoutSeparator(path);
+ public static string ForCompare(string path)
+ => PathUtils.WithoutSeparator(PathUtils.WithoutLongPath(Path.GetFullPath(path)).Replace("/", @"\"));
+
+ ///
+ /// 将路径转换为兼容各种 Windows API 的格式。
+ /// 具体而言:将分隔符改为 \。添加前导的 \\?\。若为驱动器则添加末尾分隔符,否则去除分隔符。
+ ///
+ public static string ForApi(string path) {
+ path = PathUtils.WithLongPath(path);
+ if (path.EndsWithF(":")) path += @"\"; // 驱动器路径必须保留末尾的分隔符
+ if (path.EndsWithF(@":\")) return path;
+ return PathUtils.WithoutSeparator(path);
}
#endregion