From eaf35fd73ea9f67eda9bf4cc34ab11f02f9a1628 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, 5 May 2026 07:26:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E5=8C=85=E7=9B=B8=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Shared/MeloongCore.csproj | 2 +- Shared/Utils/FileUtils.cs | 83 ++++++++++++++++++++++++++++++++++++--- Shared/Utils/PathUtils.cs | 13 ++---- 3 files changed, 82 insertions(+), 16 deletions(-) diff --git a/Shared/MeloongCore.csproj b/Shared/MeloongCore.csproj index 8c4240f..ddaa778 100644 --- a/Shared/MeloongCore.csproj +++ b/Shared/MeloongCore.csproj @@ -24,8 +24,8 @@ + - diff --git a/Shared/Utils/FileUtils.cs b/Shared/Utils/FileUtils.cs index 6316434..249a9ff 100644 --- a/Shared/Utils/FileUtils.cs +++ b/Shared/Utils/FileUtils.cs @@ -1,4 +1,4 @@ -using System.Runtime.ExceptionServices; +using System.IO.Compression; namespace MeloongCore; public static class FileUtils { @@ -38,7 +38,7 @@ public static class FileUtils { /// 如果文件或文件夹不存在,则会自动创建。若已存在,则会覆盖原文件。 /// public static void Write(string filePath, Stream stream) { - using FileStream fileStream = OpenCreate(PathUtils.WithLongPath(filePath)); + using FileStream fileStream = CreateAsStream(PathUtils.WithLongPath(filePath)); if (stream.CanSeek && stream.Position != 0) stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); } @@ -84,8 +84,8 @@ public static class FileUtils { if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) { Run(target); } else { - ExceptionDispatchInfo? internalEx = null; // 捕获内部异常 - var thread = new Thread(() => { try { Run(target); } catch (Exception ex) { internalEx = ExceptionDispatchInfo.Capture(ex); } }); + System.Runtime.ExceptionServices.ExceptionDispatchInfo? internalEx = null; // 捕获内部异常 + var thread = new Thread(() => { try { Run(target); } catch (Exception ex) { internalEx = System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(ex); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); @@ -130,20 +130,91 @@ public static class FileUtils { /// /// 打开该文件的只读 。 /// - public static FileStream OpenRead(string filePath) { + public static FileStream ReadAsStream(string filePath) { return new(PathUtils.WithLongPath(filePath), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } /// /// 在指定路径创建文件,并打开 。 /// - public static FileStream OpenCreate(string filePath) { + public static FileStream CreateAsStream(string filePath) { DirectoryUtils.Create(filePath, isFilePath: true); return new(PathUtils.WithLongPath(filePath), FileMode.Create); } #endregion + #region 压缩包 + + /// + /// 尝试根据后缀名判断文件种类并解压文件,支持 gz 与 zip,会尝试将 jar 以 zip 方式解压。 + /// 会自动创建文件夹。会覆盖已有文件,但不会删除多余文件。 + /// 解压时会先尝试 UTF8 编码,失败后换用 GB18030。 + /// + public static void ExtractToDirectory(string compressionFile, string outputDirectory, Action? progressIncrementHandler = null) { + compressionFile = PathUtils.WithLongPath(compressionFile); + DirectoryUtils.Create(outputDirectory); + // 解压 gz(gz 不需要考虑编码) + if (compressionFile.EndsWithF(".gz", true)) { + using var stream = new GZipStream(FileUtils.ReadAsStream(compressionFile), CompressionMode.Decompress); + FileUtils.Write(Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(compressionFile)), stream); + return; + } + // 解压 zip + void ExtractWithEncoding(Encoding encoding) { + using var archive = ZipFile.Open(compressionFile, ZipArchiveMode.Read, encoding); + int entryCount = archive.Entries.Count; + foreach (var entry in archive.Entries) { + if (progressIncrementHandler != null && entryCount > 0) progressIncrementHandler(1.0 / entryCount); + if (string.IsNullOrEmpty(entry.Name)) continue; // 跳过文件夹条目(ZipArchive 会将文件夹也作为一个 entry,但它们的 Name 为空) + // ZipSlip 修复 + string outputFilePath = Path.GetFullPath(Path.Combine(outputDirectory, entry.FullName)); + if (!outputFilePath.StartsWithF(PathUtils.WithSeparator(Path.GetFullPath(outputDirectory)))) + throw new UnauthorizedAccessException($"Zip 文件项 {entry.FullName} 的路径在压缩包之外,这可能导致安全问题"); + // 实际的解压 + using var entryStream = entry.Open(); + FileUtils.Write(outputFilePath, entryStream); + } + } + try { // 尝试两种编码 + ExtractWithEncoding(new UTF8Encoding(false, true)); + } catch (InvalidDataException) { + ExtractWithEncoding(Encoding.GetEncoding("GB18030")); + } + } + + /// + /// 将指定文件夹的内容打包为一个 zip 压缩包。 + /// 会自动创建文件夹。会覆盖已有文件。 + /// + public static void CreateZipFromDirectory(string outputFullPath, string sourceDirectory) { + outputFullPath = PathUtils.WithLongPath(outputFullPath); + sourceDirectory = PathUtils.WithLongPath(sourceDirectory); + DirectoryUtils.Create(outputFullPath, isFilePath: true); + FileUtils.Delete(outputFullPath); + ZipFile.CreateFromDirectory(sourceDirectory, outputFullPath); + } + + /// + /// 将多个文件打包为一个压缩文件,所有文件都会被放在压缩文件的根目录。 + /// 会自动创建文件夹。会覆盖已有文件。 + /// + public static void CreateZipFromFiles(string outputFullPath, params string[] sourceFiles) { + outputFullPath = PathUtils.WithLongPath(outputFullPath); + sourceFiles = sourceFiles.Select(PathUtils.WithLongPath).ToArray(); + DirectoryUtils.Create(outputFullPath, isFilePath: true); + FileUtils.Delete(outputFullPath); + using var archive = ZipFile.Open(outputFullPath, ZipArchiveMode.Create); + foreach (var source in sourceFiles) { + if (!FileUtils.Exists(source)) throw new FileNotFoundException($"未找到需要被压缩的文件({source})", source); + using var sourceStream = FileUtils.ReadAsStream(source); + using var entryStream = archive.CreateEntry(Path.GetFileName(source), CompressionLevel.Optimal).Open(); + sourceStream.CopyTo(entryStream); + } + } + + #endregion + /// /// 确定指定的文件是否存在。 /// diff --git a/Shared/Utils/PathUtils.cs b/Shared/Utils/PathUtils.cs index c9d380f..fb6c33b 100644 --- a/Shared/Utils/PathUtils.cs +++ b/Shared/Utils/PathUtils.cs @@ -3,20 +3,15 @@ public static class PathUtils { #region 分隔符 - /// - /// 检查路径是否以文件夹分隔符结尾。 - /// - public static bool EndsWithSeparator(string path) => path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar); - /// /// 确保路径的结尾包含文件夹分隔符。 /// - public static string WithSeparator(string folder) => EndsWithSeparator(folder) ? folder : folder + Path.DirectorySeparatorChar; + public static string WithSeparator(string folder) => folder.EndsWithF(Path.DirectorySeparatorChar) ? folder : folder + Path.DirectorySeparatorChar; /// /// 确保路径的结尾不包含文件夹分隔符。 /// - public static string WithoutSeparator(string folder) => EndsWithSeparator(folder) ? folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) : folder; + public static string WithoutSeparator(string folder) => folder.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); #endregion @@ -60,12 +55,12 @@ public static class PathUtils { private static extern int GetShortPathNameW(string lpszLongPath, [Out] char[] buffer, int cchBuffer); /// - /// 将路径转换为以 \\?\ 开头的标准长路径格式。 + /// 将完整路径转换为以 \\?\ 开头的标准长路径格式。 /// 这会去除路径末尾的分隔符,且将 / 替换为 \。 /// public static string WithLongPath(string path) { if (string.IsNullOrWhiteSpace(path)) return path; - if (path.StartsWithF(@"\\")) return path; // 已有长路径前缀 + if (path.StartsWithF(@"\\?\")) return path; // 已经是长路径 path = WithoutSeparator(path).Replace('/', '\\'); // API 要求这个格式…… return $@"\\?\{path}"; }