diff --git a/General/DirectoryUtils.cs b/General/DirectoryUtils.cs new file mode 100644 index 0000000..4a3bd02 --- /dev/null +++ b/General/DirectoryUtils.cs @@ -0,0 +1,32 @@ +using MeloongCore.Extensions; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MeloongCore { + public static class DirectoryUtils { + + /// + /// 确保路径以 / 或 \ 结尾。 + /// + public static string ToDirectoryFormat(string folder) => IsDirectoryFormat(folder) ? folder : folder + Path.DirectorySeparatorChar; + + /// + /// 检查路径是否以 / 或 \ 结尾。 + /// + public static bool IsDirectoryFormat(string path) => path.EndsWithF("/") || path.EndsWithF("\\"); + + /// + /// 创建文件夹,或文件所在的文件夹。 + /// 支持长路径,文件夹已存在时不会抛出异常。 + /// + public static void Create(string path) { + if (!IsDirectoryFormat(path)) path = Path.GetDirectoryName(path); + Directory.CreateDirectory(PathUtils.Shorten(path)); + } + + } +} diff --git a/General/MsDelta.cs b/General/MsDelta.cs index c8ae74e..d985dc8 100644 --- a/General/MsDelta.cs +++ b/General/MsDelta.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.IO; using System.Runtime.InteropServices; namespace MeloongCore { @@ -12,7 +13,12 @@ namespace MeloongCore { /// /// 生成增量补丁文件:比较 和 ,将补丁写入 。 /// + /// + /// public static void Create(string oldFilePath, string newFilePath, string deltaFilePath) { + if (!File.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath); + if (!File.Exists(newFilePath)) throw new FileNotFoundException($"新文件不存在:{newFilePath}", newFilePath); + DirectoryUtils.Create(deltaFilePath); // 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, @@ -23,7 +29,12 @@ namespace MeloongCore { /// /// 应用增量补丁文件:将 补丁应用到 ,将结果写入 。 /// + /// + /// public static void Apply(string oldFilePath, string deltaFilePath, string newFilePath) { + if (!File.Exists(oldFilePath)) throw new FileNotFoundException($"旧文件不存在:{oldFilePath}", oldFilePath); + if (!File.Exists(deltaFilePath)) throw new FileNotFoundException($"补丁文件不存在:{deltaFilePath}", deltaFilePath); + DirectoryUtils.Create(newFilePath); // 1L: AllowLegacy if (!ApplyDelta(1L, PathUtils.Shorten(oldFilePath), PathUtils.Shorten(deltaFilePath), PathUtils.Shorten(newFilePath))) throw new Win32Exception();