FilesHelper.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Mime;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. //----------------------------------------------------------------
  10. //Module Name: $safeprojectname$
  11. //Purpose:
  12. //CopyRight: Muchinfo
  13. //History:
  14. //----------------------------------------------------------------
  15. //DateTime 2015/12/30 9:12:33
  16. //Author
  17. //Description Create
  18. //----------------------------------------------------------------
  19. using System.Xml.Serialization;
  20. namespace Muchinfo.Client.Update.UpdateListFactory
  21. {
  22. public class FilesHelper
  23. {
  24. private const string CompassPath = "MuchinfoUpdate";
  25. public static void SearchfileCompress(UpdateInfo updateFile ,string directoryPath)
  26. {
  27. var dirInfo = new DirectoryInfo(directoryPath);
  28. var files = dirInfo.GetFiles().Where((item)=>item.Name!= AppDomain.CurrentDomain.FriendlyName);
  29. foreach (var file in files)
  30. {
  31. string path = file.Name;
  32. var md5Code = GetFileMD5Code(file);
  33. if (updateFile.IsCompress)
  34. {
  35. Console.WriteLine("正在压缩:" + file.FullName);
  36. using (FileStream fStream = new FileStream(file.FullName, FileMode.Open))
  37. {
  38. var fileDirectoy = directoryPath + "\\" + CompassPath + "\\";
  39. if (!Directory.Exists(fileDirectoy))
  40. {
  41. Directory.CreateDirectory(fileDirectoy);
  42. }
  43. ZipCompessHelper.CreateCompressFile(fStream,
  44. fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz");
  45. fStream.Close();
  46. }
  47. }
  48. var updatefile = new UpdateFile(path, file.Extension, md5Code);
  49. if (updateFile.UpdateFiles == null)
  50. {
  51. updateFile.UpdateFiles = new List<UpdateFile>();
  52. }
  53. updateFile.UpdateFiles.Add(updatefile);
  54. }
  55. var directories = dirInfo.GetDirectories();
  56. var directoryList= directories.Where((dir) => dir.Name != CompassPath); //删除压缩的
  57. foreach (var directory in directoryList)
  58. {
  59. EnumeratorDirectory(updateFile, directory, string.Empty, directoryPath);
  60. }
  61. }
  62. /// <summary>
  63. /// 递归读取文件
  64. /// </summary>
  65. /// <param name="updateFile"></param>
  66. /// <param name="directory"></param>
  67. /// <param name="parentDir"></param>
  68. /// <param name="baseCompessdir"></param>
  69. public static void EnumeratorDirectory(UpdateInfo updateFile, DirectoryInfo directory,string parentDir,string baseCompessdir)
  70. {
  71. var files = directory.GetFiles();
  72. string dirpath = parentDir + "\\" + directory.Name;
  73. foreach (var file in files)
  74. {
  75. string path = dirpath+"\\" + file.Name;
  76. if (updateFile.IsCompress)
  77. {
  78. Console.WriteLine("正在压缩:" + file.FullName);
  79. using (FileStream fStream = new FileStream(file.FullName, FileMode.Open))
  80. {
  81. var fileDirectoy = baseCompessdir + "\\" + CompassPath + "\\" + dirpath + "\\";
  82. if (!Directory.Exists(fileDirectoy))
  83. {
  84. Directory.CreateDirectory(fileDirectoy);
  85. }
  86. ZipCompessHelper.CreateCompressFile(fStream,
  87. fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz");
  88. fStream.Close();
  89. }
  90. }
  91. var md5Code = GetFileMD5Code(file);
  92. var updatefile = new UpdateFile(path, file.Extension, md5Code);
  93. if (updateFile.UpdateFiles == null)
  94. {
  95. updateFile.UpdateFiles = new List<UpdateFile>();
  96. }
  97. updateFile.UpdateFiles.Add(updatefile);
  98. }
  99. var directories = directory.GetDirectories();
  100. foreach (var dirInfo in directories)
  101. {
  102. EnumeratorDirectory(updateFile, dirInfo, dirpath, baseCompessdir);
  103. }
  104. }
  105. /// <summary>
  106. /// 只做当前文件夹下所有文件夹更新
  107. /// </summary>
  108. /// <param name="updateFile"></param>
  109. /// <param name="directoryPath"></param>
  110. public static void CurrentDirectory(UpdateInfo updateFile, string directoryPath)
  111. {
  112. var dirInfo = new DirectoryInfo(directoryPath);
  113. var directoryList = dirInfo.GetDirectories();
  114. foreach (var directory in directoryList)
  115. {
  116. var files = directory.GetFiles();
  117. foreach (var file in files)
  118. {
  119. string path = directory.Name+ "\\" + file.Name;
  120. var md5Code = GetFileMD5Code(file);
  121. if (updateFile.IsCompress)
  122. {
  123. Console.WriteLine("正在压缩:" + file.FullName);
  124. using (FileStream fStream = new FileStream(file.FullName, FileMode.Open))
  125. {
  126. var fileDirectoy = directoryPath + "\\" + CompassPath + "\\" + directory.Name+"\\";
  127. if (!Directory.Exists(fileDirectoy))
  128. {
  129. Directory.CreateDirectory(fileDirectoy);
  130. }
  131. ZipCompessHelper.CreateCompressFile(fStream,
  132. fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz");
  133. fStream.Close();
  134. }
  135. }
  136. var updatefile = new UpdateFile(path, file.Extension, md5Code);
  137. // updatefile.Directory = directory.Name;
  138. if (updateFile.UpdateFiles == null)
  139. {
  140. updateFile.UpdateFiles = new List<UpdateFile>();
  141. }
  142. updateFile.UpdateFiles.Add(updatefile);
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// 计算文件MD5
  148. /// </summary>
  149. /// <param name="fileInfo"></param>
  150. /// <returns></returns>
  151. public static string GetFileMD5Code(FileInfo fileInfo)
  152. {
  153. using (FileStream fs= File.OpenRead(fileInfo.FullName))
  154. {
  155. System.Security.Cryptography.MD5 md5=new MD5CryptoServiceProvider();
  156. byte[] retval = md5.ComputeHash(fs);
  157. fs.Close();
  158. var sbBuilder=new StringBuilder();
  159. for (int i = 0; i < retval.Length; i++)
  160. {
  161. sbBuilder.Append(retval[i].ToString("x2"));
  162. }
  163. return sbBuilder.ToString().ToUpper();
  164. }
  165. }
  166. /// <summary>
  167. /// 生成更新文件的XML
  168. /// </summary>
  169. /// <param name="updateInfo">更新配置</param>
  170. /// <param name="filePath">保存路径</param>
  171. public static void SaveUpdateXml(UpdateInfo updateInfo, string filePath)
  172. {
  173. Type[] types = new Type[] { typeof(UpdateFile) };
  174. var xs = new XmlSerializer(updateInfo.GetType(), types);
  175. using (var stream = new FileStream(filePath, FileMode.Create))
  176. {
  177. xs.Serialize(stream, updateInfo);
  178. stream.Close();
  179. }
  180. }
  181. }
  182. }