UpgradeFilePathReader.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Concurrent;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace vCloud.Windows.ForceUpgrade
  12. {
  13. public class UpgradeFilePathReader
  14. {
  15. private Dictionary<string, UpgradeFileInfo> _fileVersionDic = new Dictionary<string, UpgradeFileInfo>();
  16. private string _rootPath;
  17. private void ReaderFiles(string path)
  18. {
  19. var files = Directory.GetFiles(path);
  20. foreach (var file in files)
  21. {
  22. try
  23. {
  24. var fileInfo = new FileInfo(file);
  25. var fileVersionInfo = FileVersionInfo.GetVersionInfo(file);
  26. var lastWriteTime = fileInfo.LastWriteTimeUtc;
  27. var fileSize = fileInfo.Length;
  28. var fileVersion = lastWriteTime.ToString("yyyyMMddHHmmss") + "." + fileSize;
  29. var productVersion = fileVersionInfo.FileVersion;
  30. var fileShortPath = file.Replace(_rootPath.TrimEnd('\\') + "\\", "RootPath\\");
  31. _fileVersionDic.Add(fileShortPath, new UpgradeFileInfo
  32. {
  33. FileVersion = fileVersion,
  34. ProductVersion = productVersion,
  35. Md5Code = GetMd5(file),
  36. LastWriteTime = lastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"),
  37. Size = fileSize
  38. });
  39. }
  40. catch (Exception ex)
  41. {
  42. FileHelper.AppendWriteFile("InstallLog.txt", ex.Message + "|" + ex.StackTrace);
  43. }
  44. }
  45. var dirs = Directory.GetDirectories(path);
  46. if (dirs != null && dirs.Length > 0)
  47. {
  48. foreach(var dir in dirs)
  49. {
  50. ReaderFiles(dir);
  51. }
  52. }
  53. }
  54. private string GetMd5(string FilePath)
  55. {
  56. var file = File.ReadAllBytes(FilePath);
  57. var md5 = System.Security.Cryptography.MD5.Create();
  58. var md5Str = BitConverter.ToString(md5.ComputeHash(file)).Replace("-", "");
  59. return md5Str;
  60. }
  61. private object Clone(object obj)
  62. {
  63. using (MemoryStream memoryStream = new MemoryStream())
  64. {
  65. BinaryFormatter formatter = new BinaryFormatter();
  66. formatter.Serialize(memoryStream, obj);
  67. memoryStream.Position = 0;
  68. return formatter.Deserialize(memoryStream);
  69. }
  70. }
  71. /// <summary>
  72. /// 获取文件版本字典
  73. /// </summary>
  74. /// <param name="path"></param>
  75. /// <returns></returns>
  76. public Dictionary<string, UpgradeFileInfo> StartReader(string path)
  77. {
  78. _fileVersionDic.Clear();
  79. _rootPath = path;
  80. ReaderFiles(path);
  81. return Clone(_fileVersionDic) as Dictionary<string, UpgradeFileInfo>;
  82. }
  83. /// <summary>
  84. /// 比对不同文件夹,文件版本差异,生成更新文件列表
  85. /// </summary>
  86. /// <param name="path"></param>
  87. /// <param name="diffPath"></param>
  88. /// <returns></returns>
  89. public Dictionary<string, UpgradeFileInfo> DiffReader(string path, string diffPath)
  90. {
  91. var dic = StartReader(path);
  92. var diffDic = StartReader(diffPath);
  93. foreach (var fileInfo in diffDic)
  94. {
  95. if (dic.Keys.Contains(fileInfo.Key))
  96. {
  97. var fileValue = dic[fileInfo.Key];
  98. fileValue.Type = UpgradeOperateType.Add;
  99. if (fileValue.Size == fileInfo.Value.Size && fileValue.ProductVersion == fileInfo.Value.ProductVersion)
  100. {
  101. dic.Remove(fileInfo.Key);
  102. }
  103. }
  104. else
  105. {
  106. fileInfo.Value.Type = UpgradeOperateType.Remove;
  107. dic.Add(fileInfo.Key, fileInfo.Value);
  108. }
  109. }
  110. return dic;
  111. }
  112. /// <summary>
  113. /// 比对不同文件夹生成的文件版本列表,生成更新文件列表
  114. /// </summary>
  115. /// <param name="json"></param>
  116. /// <param name="diffJson"></param>
  117. /// <returns></returns>
  118. public Dictionary<string, UpgradeFileInfo> DiffReaderJson(string json, string diffJson)
  119. {
  120. var dic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(json);
  121. var diffDic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(diffJson);
  122. foreach (var fileInfo in diffDic)
  123. {
  124. if (dic.Keys.Contains(fileInfo.Key))
  125. {
  126. var fileValue = dic[fileInfo.Key];
  127. fileValue.Type = UpgradeOperateType.Add;
  128. if (fileValue.Size == fileInfo.Value.Size && fileValue.ProductVersion == fileInfo.Value.ProductVersion)
  129. {
  130. dic.Remove(fileInfo.Key);
  131. }
  132. }
  133. else
  134. {
  135. fileInfo.Value.Type = UpgradeOperateType.Remove;
  136. dic.Add(fileInfo.Key, fileInfo.Value);
  137. }
  138. }
  139. return dic;
  140. }
  141. }
  142. }