PathUtils.RemoveLastPart

This commit is contained in:
龙腾猫跃 committed 2026-05-08 21:27:04 +08:00
1 parent 256db1e973
commit 4f1664459d
7 files changed
+54 -26

No files matched your search

+1 -2
View File
@@ -5,8 +5,7 @@ public static class DirectoryUtils {
/// 创建文件夹,或文件所在的文件夹。
/// 文件夹已存在时不会抛出异常。
/// </summary>
public static void Create(string path, bool isFilePath = false) {
if (isFilePath) path = PathUtils.RemoveFileName(path);
public static void Create(string path) {
if (DirectoryUtils.Exists(path)) return;
Logger.Trace($"新建文件夹:{path}");
path = PathUtils.WithLongPath(path);
+7 -6
View File
@@ -68,7 +68,7 @@ public static class FileUtils {
/// 若文件已存在,则会覆盖原文件。
/// </summary>
public static void Write(string filePath, byte[] content) {
DirectoryUtils.Create(filePath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath));
Logger.Trace($"写入文件:{filePath}({content.Length} 字节)");
ResilientUtils.RetryOn<IOException>(()
=> File.WriteAllBytes(PathUtils.WithLongPath(filePath), content));
@@ -90,7 +90,7 @@ public static class FileUtils {
/// 若文件已存在,则会覆盖原文件。
/// </summary>
public static FileStream CreateAsStream(string filePath) {
DirectoryUtils.Create(filePath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(filePath));
Logger.Trace($"创建文件流:{filePath}");
return ResilientUtils.RetryOn<IOException, FileStream>(()
=> new FileStream(PathUtils.WithLongPath(filePath), FileMode.Create));
@@ -111,7 +111,7 @@ public static class FileUtils {
Logger.Info($"尝试复制文件,但原文件不存在,已跳过复制:{sourceFilePath} → {destFilePath}");
return;
}
DirectoryUtils.Create(destFilePath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath));
Logger.Trace($"复制文件:{sourceFilePath} → {destFilePath}");
ResilientUtils.RetryOn<IOException>(()
=> File.Copy(PathUtils.WithLongPath(sourceFilePath), PathUtils.WithLongPath(destFilePath), true));
@@ -123,7 +123,7 @@ public static class FileUtils {
/// </summary>
public static void Move(string sourceFilePath, string destFilePath) {
if (sourceFilePath == destFilePath) return; // 如果移动同一个文件则跳过
DirectoryUtils.Create(destFilePath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(destFilePath));
FileUtils.Delete(destFilePath);
Logger.Trace($"剪切文件:{sourceFilePath} → {destFilePath}");
ResilientUtils.RetryOn<IOException>(()
@@ -265,6 +265,7 @@ public static class FileUtils {
using var fileStream = FileUtils.ReadAsStream(compressionFile);
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
FileUtils.Write(outFilePath, gzipStream);
progressHandler?.Invoke(1);
return;
}
// 解压 zip
@@ -294,7 +295,7 @@ public static class FileUtils {
public static void CreateZipFromDirectory(string outputFullPath, string sourceDirectory) {
outputFullPath = PathUtils.WithLongPath(outputFullPath);
sourceDirectory = PathUtils.WithLongPath(sourceDirectory);
DirectoryUtils.Create(outputFullPath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(outputFullPath));
FileUtils.Delete(outputFullPath);
Logger.Trace($"将文件夹中的内容压缩为 zip 文件:{sourceDirectory} → {outputFullPath}");
ResilientUtils.RetryOn<IOException>(()
@@ -322,7 +323,7 @@ public static class FileUtils {
/// <param name="sources">键为 zip 文件下的路径,值为文件的本地路径。</param>
public static void CreateZipFromFiles(string outputFullPath, IDictionary<string, string> sources) {
outputFullPath = PathUtils.WithLongPath(outputFullPath);
DirectoryUtils.Create(outputFullPath, isFilePath: true);
DirectoryUtils.Create(PathUtils.RemoveLastPart(outputFullPath));
FileUtils.Delete(outputFullPath);
using var archive = ResilientUtils.RetryOn<IOException, ZipArchive>(()
=> ZipFile.Open(outputFullPath, ZipArchiveMode.Create));
+16 -8
View File
@@ -34,7 +34,7 @@ public static class PathUtils {
if (fullName.EndsWithF(".jar", true)) keepFileName = true; // jar 文件的文件名需要保留原样,否则会导致 Forge 1.20.1 无法通过文件名识别模块名
if (keepFileName && FileUtils.Exists(fullName)) {
pathToKeep = Path.GetFileName(fullName);
pathToShorten = PathUtils.RemoveFileName(fullName);
pathToShorten = PathUtils.RemoveLastPart(fullName);
}
// 逐级向上寻找已存在的文件夹,将不存在的部分挪到 suffix,不再缩短
@@ -85,13 +85,21 @@ public static class PathUtils {
#region 路径处理
/// <summary>
/// 去除路径末尾的文件名。
/// 若路径以分隔符结尾,则不作处理。
/// </summary>
public static string RemoveFileName(string? path) {
if (path is null || string.IsNullOrEmpty(path)) return "";
if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path;
return Path.GetDirectoryName(path) ?? "";
/// 去除路径的最后一级,并移除末尾的分隔符。
/// <code>
/// "C:\foo\bar.txt" → "C:\foo"
/// "C:\foo\bar" → "C:\foo"
/// "C:\foo\bar\" → "C:\foo"
/// "C:\foo\" → "C:"
/// "https://foo.bar/file/pack.zip?arg=1" → "https://foo.bar/file"
/// </code></summary>
public static string RemoveLastPart(string? path) {
if (path!.Contains("://")) { // 网络路径
path = PathUtils.WithoutSeparator(path.BeforeFirst("#").BeforeFirst("?")); // 去除参数
return PathUtils.WithoutSeparator(path).BeforeLast("/");
} else { // 文件路径
return PathUtils.WithoutSeparator(path!).BeforeLast("\\");
}
}
/// <summary>