经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
文件夹比较工具
来源:cnblogs  作者:wzwyc  时间:2023/5/25 9:07:20  对本文有异议

文件比较平常都是用Beyond Compare,可以说离不开的神器,特别是针对代码比较这块,确实挺好用的。

不过Beyond Compare平常拿它主要是用来做代码比较,用来做一些大批量的二进制文件比较,其实有点不是很方便。

于是造轮子,重新写了一个简单的文件夹比较的小工具。

平常主要是拿来做一些Nuget包的比对,应用包版本的比较。

文件夹比较逻辑,采用迭代比较的方式:

  1. using CgdataBase;
  2. using FolderCompare.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace FolderCompare.Helpers
  11. {
  12. public static class CompareHelper
  13. {
  14. public static void CompareDirectory(CgDirectoryInfo path1, CgDirectoryInfo path2)
  15. {
  16. if (path1.Children.IsNullOrEmpty())
  17. {
  18. if (path2.Children.IsNullOrEmpty())
  19. {
  20. path1.Result = ECompareResult.匹配;
  21. path2.Result = ECompareResult.匹配;
  22. }
  23. else
  24. {
  25. path1.Result = ECompareResult.空;
  26. path2.Result = ECompareResult.孤立;
  27. SetCompareResult(path2, ECompareResult.匹配);
  28. }
  29. return;
  30. }
  31. if (path2.Children.IsNullOrEmpty())
  32. {
  33. path1.Result = ECompareResult.孤立;
  34. path2.Result = ECompareResult.空;
  35. SetCompareResult(path1, ECompareResult.匹配);
  36. return;
  37. }
  38. var dirList = new List<string>();
  39. var fileList = new List<string>();
  40. dirList.AddRange(path1.Children.Where(s => s.IsDirectory).Select(s => s.Name));
  41. dirList.AddRange(path2.Children.Where(s => s.IsDirectory).Select(s => s.Name));
  42. fileList.AddRange(path1.Children.Where(s => !s.IsDirectory).Select(s => s.Name));
  43. fileList.AddRange(path2.Children.Where(s => !s.IsDirectory).Select(s => s.Name));
  44. var index = 0;
  45. if (dirList.HadItems())
  46. {
  47. var items = dirList.Distinct().ToList();
  48. items.Sort();
  49. foreach (var item in items)
  50. {
  51. var dir1 = path1.Children.OfType<CgDirectoryInfo>().SingleOrDefault(s => s.Name == item);
  52. if (dir1 == null)
  53. {
  54. dir1 = new CgDirectoryInfo();
  55. dir1.Result = ECompareResult.空;
  56. path1.Children.Insert(index, dir1);
  57. var dir2 = path2.Children.OfType<CgDirectoryInfo>().Single(s => s.Name == item);
  58. dir2.Result = ECompareResult.孤立;
  59. var old = path2.Children.IndexOf(dir2);
  60. path2.Children.Move(old, index);
  61. SetCompareResult(dir2, ECompareResult.匹配);
  62. }
  63. else
  64. {
  65. var dir2 = path2.Children.OfType<CgDirectoryInfo>().SingleOrDefault(s => s.Name == item);
  66. if (dir2 == null)
  67. {
  68. dir2 = new CgDirectoryInfo();
  69. dir2.Result = ECompareResult.空;
  70. path2.Children.Insert(index, dir2);
  71. dir1.Result = ECompareResult.孤立;
  72. var old = path1.Children.IndexOf(dir1);
  73. path1.Children.Move(old, index);
  74. SetCompareResult(dir1, ECompareResult.匹配);
  75. }
  76. else
  77. {
  78. CompareDirectory(dir1, dir2);
  79. var old = path1.Children.IndexOf(dir1);
  80. path1.Children.Move(old, index);
  81. old = path2.Children.IndexOf(dir2);
  82. path2.Children.Move(old, index);
  83. }
  84. dir2.Index = index;
  85. }
  86. dir1.Index = index;
  87. index++;
  88. }
  89. }
  90. if (fileList.HadItems())
  91. {
  92. var items = fileList.Distinct().ToList();
  93. items.Sort();
  94. foreach (var item in items)
  95. {
  96. var file1 = path1.Children.OfType<CgFileInfo>().SingleOrDefault(s => s.Name == item);
  97. if (file1 == null)
  98. {
  99. file1 = new CgFileInfo();
  100. file1.Result = ECompareResult.空;
  101. path1.Children.Insert(index, file1);
  102. var file2 = path2.Children.OfType<CgFileInfo>().Single(s => s.Name == item);
  103. file2.Result = ECompareResult.孤立;
  104. var old = path2.Children.IndexOf(file2);
  105. path2.Children.Move(old, index);
  106. }
  107. else
  108. {
  109. var file2 = path2.Children.OfType<CgFileInfo>().SingleOrDefault(s => s.Name == item);
  110. if (file2 == null)
  111. {
  112. file2 = new CgFileInfo();
  113. file2.Result = ECompareResult.空;
  114. path2.Children.Insert(index, file2);
  115. file1.Result = ECompareResult.孤立;
  116. var old = path1.Children.IndexOf(file1);
  117. path1.Children.Move(old, index);
  118. }
  119. else
  120. {
  121. CompareFile(file1, file2);
  122. var old = path1.Children.IndexOf(file1);
  123. path1.Children.Move(old, index);
  124. old = path2.Children.IndexOf(file2);
  125. path2.Children.Move(old, index);
  126. }
  127. file2.Index = index;
  128. }
  129. file1.Index = index;
  130. index++;
  131. }
  132. }
  133. path1.Result = GetCompareResult(path1.Children);
  134. path2.Result = GetCompareResult(path2.Children);
  135. }
  136. private static void CompareFile(CgFileSystemInfo info1, CgFileSystemInfo info2)
  137. {
  138. if (info1.Name.IsNullOrEmpty())
  139. {
  140. info1.Result = ECompareResult.空;
  141. info2.Result = ECompareResult.孤立;
  142. return;
  143. }
  144. if (info2.Name.IsNullOrEmpty())
  145. {
  146. info1.Result = ECompareResult.孤立;
  147. info2.Result = ECompareResult.空;
  148. return;
  149. }
  150. if (info1.Length == info2.Length && info1.LastWriteTime == info2.LastWriteTime)
  151. {
  152. info1.Result = ECompareResult.匹配;
  153. info2.Result = ECompareResult.匹配;
  154. }
  155. else
  156. {
  157. if (info1.LastWriteTime > info2.LastWriteTime)
  158. {
  159. info1.Result = ECompareResult.不匹配;
  160. info2.Result = ECompareResult.较旧的;
  161. }
  162. else
  163. {
  164. info1.Result = ECompareResult.较旧的;
  165. info2.Result = ECompareResult.不匹配;
  166. }
  167. }
  168. }
  169. private static void SetCompareResult(CgDirectoryInfo info, ECompareResult result)
  170. {
  171. if (info.Children.HadItems())
  172. {
  173. foreach (var item in info.Children)
  174. {
  175. if (item is CgDirectoryInfo dir)
  176. {
  177. dir.Result = result;
  178. SetCompareResult(dir, result);
  179. }
  180. else if (item is CgFileInfo file)
  181. {
  182. file.Result = result;
  183. }
  184. }
  185. }
  186. }
  187. public static ECompareResult GetCompareResult(ObservableCollection<CgFileSystemInfo> items)
  188. {
  189. if (items.IsNullOrEmpty())
  190. return ECompareResult.空;
  191. if (items.Any(s => s.Result == ECompareResult.不匹配 || s.Result == ECompareResult.较旧的))
  192. return ECompareResult.不匹配;
  193. if (items.Any(s => s.Result == ECompareResult.孤立))
  194. return ECompareResult.孤立;
  195. return ECompareResult.匹配;
  196. }
  197. }
  198. }

 

源码:https://gitee.com/wzwyc/FolderCompare

 

原文链接:https://www.cnblogs.com/wzwyc/p/17427322.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号