UpdateHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.AccessControl;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. //----------------------------------------------------------------
  9. //Module Name: $safeprojectname$
  10. //Purpose:
  11. //CopyRight: Muchinfo
  12. //History:
  13. //----------------------------------------------------------------
  14. //DateTime 2016/1/5 11:37:29
  15. //Author
  16. //Description Create
  17. //----------------------------------------------------------------
  18. using System.Xml;
  19. using System.Xml.Serialization;
  20. namespace Muchinfo.MTPClient.Update
  21. {
  22. public class UpdateHelper
  23. {
  24. /// <summary>
  25. /// 获取配置文件路径
  26. /// </summary>
  27. /// <returns></returns>
  28. private string GetConfigPath(string applictionUpdateUrl)
  29. {
  30. string configPath = string.Empty;
  31. if (applictionUpdateUrl == null)
  32. {
  33. return configPath;
  34. }
  35. var typeinfo = typeof (UpdateInfo);
  36. if (applictionUpdateUrl.EndsWith("/"))
  37. {
  38. configPath = string.Format("{0}{1}", applictionUpdateUrl,typeinfo.Name);
  39. }
  40. else
  41. {
  42. configPath = string.Format("{0}/{1}", applictionUpdateUrl, typeinfo.Name);
  43. }
  44. return configPath;
  45. }
  46. /// <summary>
  47. /// 反序列化对象
  48. /// </summary>
  49. /// <param name="stream">数据流</param>
  50. /// <returns>实体对象</returns>
  51. public UpdateInfo DeSerialize(Stream stream)
  52. {
  53. Type[] types = new Type[] { typeof(UpdateFile) };
  54. var xs = new XmlSerializer(typeof(UpdateInfo), types);
  55. return xs.Deserialize(stream) as UpdateInfo;
  56. }
  57. /// <summary>
  58. /// 加载升级配置文件
  59. /// </summary>
  60. /// <param name="updatePath">升级路径</param>
  61. /// <returns></returns>
  62. public UpdateInfo LoadUpdateConfig(string updatePath)
  63. {
  64. var doc = new XmlDocument();
  65. // string loadPath = GetConfigPath(updatePath);
  66. doc.Load(updatePath);
  67. using (var ms=new System.IO.MemoryStream())
  68. {
  69. doc.Save(ms);
  70. ms.Flush();
  71. ms.Position = 0;
  72. return DeSerialize(ms);
  73. }
  74. }
  75. /// <summary>
  76. /// 生成本地文件的MD5
  77. /// </summary>
  78. /// <param name="path">要生成文件Md5的目录</param>
  79. /// <param name="removeDir">要排除的项目</param>
  80. public UpdateInfo CreateUpdateInfoFile(string path,string[] removeDir=null)
  81. {
  82. var updateInfo = new UpdateInfo();
  83. SearchfileCompress(updateInfo,removeDir, path);
  84. return updateInfo;
  85. }
  86. /// <summary>
  87. /// 生成MD5配置文件
  88. /// </summary>
  89. /// <param name="updateFile">配置文件内容</param>
  90. /// <param name="removeDir">排除的文件夹</param>
  91. /// <param name="directoryPath">父目录路径</param>
  92. private void SearchfileCompress(UpdateInfo updateFile,string[] removeDir, string directoryPath)
  93. {
  94. var dirInfo = new DirectoryInfo(directoryPath);
  95. var files = dirInfo.GetFiles(); //.Where((item) => item.Name != AppDomain.CurrentDomain.FriendlyName);
  96. foreach (var file in files)
  97. {
  98. string path = file.Name;
  99. var md5Code = GetFileMD5Code(file);
  100. var updatefile = new UpdateFile(path.ToLower(), file.Extension, md5Code);
  101. if (updateFile.UpdateFiles == null)
  102. {
  103. updateFile.UpdateFiles = new List<UpdateFile>();
  104. }
  105. updateFile.UpdateFiles.Add(updatefile);
  106. }
  107. var directories = dirInfo.GetDirectories();
  108. foreach (var directory in directories)
  109. {
  110. if (removeDir == null || !removeDir.Where((item)=>item.ToLower().Equals( directory.Name.ToLower())).Any())
  111. {
  112. EnumeratorDirectory(updateFile, directory, removeDir, string.Empty, directoryPath);
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 递归生成文件配置文件
  118. /// </summary>
  119. /// <param name="updateFile">配置文件内容</param>
  120. /// <param name="directory">生成配置文件的目录</param>
  121. /// <param name="removeDir">不生成的目录</param>
  122. /// <param name="parentDir">父目录</param>
  123. /// <param name="baseCompessdir">是否压缩</param>
  124. private void EnumeratorDirectory(UpdateInfo updateFile, DirectoryInfo directory,string[] removeDir, string parentDir, string baseCompessdir)
  125. {
  126. var files = directory.GetFiles();
  127. string dirpath = parentDir + "\\" + directory.Name;
  128. foreach (var file in files)
  129. {
  130. string path = dirpath + "\\" + file.Name;
  131. var md5Code = GetFileMD5Code(file);
  132. var updatefile = new UpdateFile(path.ToLower(), file.Extension, md5Code);
  133. if (updateFile.UpdateFiles == null)
  134. {
  135. updateFile.UpdateFiles = new List<UpdateFile>();
  136. }
  137. updateFile.UpdateFiles.Add(updatefile);
  138. }
  139. var directories = directory.GetDirectories();
  140. foreach (var dirInfo in directories)
  141. {
  142. if (removeDir == null || !removeDir.Where((item) => item.ToLower().Equals(directory.Name.ToLower())).Any())
  143. {
  144. EnumeratorDirectory(updateFile, dirInfo, removeDir, dirpath, baseCompessdir);
  145. }
  146. }
  147. }
  148. /// <summary>
  149. /// 计算文件MD5
  150. /// </summary>
  151. /// <param name="fileInfo"></param>
  152. /// <returns></returns>
  153. private string GetFileMD5Code(FileInfo fileInfo)
  154. {
  155. try
  156. {
  157. using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open,FileAccess.Read))
  158. {
  159. System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
  160. byte[] retval = md5.ComputeHash(fs);
  161. fs.Close();
  162. var sbBuilder = new StringBuilder();
  163. for (int i = 0; i < retval.Length; i++)
  164. {
  165. sbBuilder.Append(retval[i].ToString("x2"));
  166. }
  167. return sbBuilder.ToString().ToUpper();
  168. }
  169. }
  170. catch (Exception)
  171. {
  172. return string.Empty;
  173. }
  174. }
  175. /// <summary>
  176. /// 生成更新文件的XML
  177. /// </summary>
  178. /// <param name="updateInfo">更新配置</param>
  179. /// <param name="filePath">保存路径</param>
  180. public void SaveUpdateXml(UpdateInfo updateInfo, string filePath)
  181. {
  182. var xs = new XmlSerializer(updateInfo.GetType());
  183. using (var stream = new FileStream(filePath, FileMode.Create))
  184. {
  185. xs.Serialize(stream, updateInfo);
  186. stream.Close();
  187. }
  188. }
  189. /// <summary>
  190. /// 生成更新文件的XML
  191. /// </summary>
  192. /// <param name="updateInfo">更新配置</param>
  193. /// <param name="filePath">保存路径</param>
  194. public static void SaveStaticUpdateXml(UpdateInfo updateInfo, string filePath)
  195. {
  196. var xs = new XmlSerializer(updateInfo.GetType());
  197. using (var stream = new FileStream(filePath, FileMode.Create))
  198. {
  199. xs.Serialize(stream, updateInfo);
  200. stream.Close();
  201. }
  202. }
  203. }
  204. }