CheckFileThran.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading;
  8. using hirdParty.DownloadSystem;
  9. using ThirdParty.DownloadSystem;
  10. using UnityEngine;
  11. public class CheckFileThran
  12. {
  13. private Thread _thread;
  14. private string rootPath;
  15. public List<MD5FileInfo> shiBaiFile = new List<MD5FileInfo>();
  16. public int count;
  17. public bool isEnd;
  18. public CheckFilePool CheckFilePool;
  19. public System.Action OnClick;
  20. public void Start(string dPath, CheckFilePool CheckFilePool)
  21. {
  22. this.CheckFilePool = CheckFilePool;
  23. this.rootPath = dPath;
  24. _thread = new Thread(UpdateThread);
  25. _thread.Start();
  26. shiBaiFile.Clear();
  27. }
  28. public void Close()
  29. {
  30. _thread = null;
  31. OnClick = null;
  32. }
  33. private void UpdateThread()
  34. {
  35. while (_thread != null)
  36. {
  37. // for (int i = 0; i < md5FileInfos.Count; i++)
  38. {
  39. if (_thread == null)
  40. {
  41. break;
  42. }
  43. try
  44. {
  45. MD5FileInfo bdMd5File = null;
  46. lock (CheckFilePool.CheckFile)
  47. {
  48. if (CheckFilePool.CheckFile.Count > 0)
  49. {
  50. bdMd5File = CheckFilePool.CheckFile[0];
  51. CheckFilePool.CheckFile.Remove(bdMd5File);
  52. }
  53. }
  54. if (bdMd5File == null)
  55. {
  56. break;
  57. }
  58. if (bdMd5File.size <= 0)
  59. {
  60. continue;
  61. }
  62. if (bdMd5File.fileName.Contains("MD5"))
  63. {
  64. continue;
  65. }
  66. count++;
  67. string dPath = rootPath + bdMd5File.fileName;
  68. if (!File.Exists(dPath))
  69. {
  70. // Debug.Log("文件不纯在"+ dPath);
  71. shiBaiFile.Add(bdMd5File);
  72. }
  73. else
  74. {
  75. string md5 = FileMD5(dPath);
  76. if (!bdMd5File.md5.Equals(md5))
  77. {
  78. Debug.Log("MD5校验失败" + dPath);
  79. shiBaiFile.Add(bdMd5File);
  80. }
  81. }
  82. Thread.Sleep(1);
  83. }
  84. catch (Exception e)
  85. {
  86. Debug.LogError("MD5校验失败" + e);
  87. }
  88. }
  89. }
  90. isEnd = true;
  91. OnClick?.Invoke();
  92. }
  93. public static string FileMD5(string filePath)
  94. {
  95. byte[] retVal;
  96. using (FileStream file = new FileStream(filePath, FileMode.Open))
  97. {
  98. MD5 md5 = new MD5CryptoServiceProvider();
  99. retVal = md5.ComputeHash(file);
  100. }
  101. return ByteArrayToString(retVal);
  102. }
  103. public static string ByteArrayToString(byte[] ba)
  104. {
  105. StringBuilder hex = new StringBuilder(ba.Length * 2);
  106. foreach (byte b in ba)
  107. {
  108. hex.AppendFormat("{0:x2}", b);
  109. }
  110. return hex.ToString();
  111. }
  112. public string GetMD5HashFromFile(byte[] data)
  113. {
  114. try
  115. {
  116. // FileStream file = new FileStream(filename,FileMode.Open);
  117. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  118. byte[] retVal = md5.ComputeHash(data);
  119. // StringBuilder sb = new StringBuilder();
  120. StringBuilder hex = new StringBuilder(retVal.Length * 2);
  121. foreach (byte b in retVal)
  122. {
  123. hex.AppendFormat("{0:x2}", b);
  124. }
  125. return retVal.ToString();
  126. }
  127. catch (System.Exception ex)
  128. {
  129. Debug.Log("错误信息:" + ex);
  130. return null;
  131. }
  132. }
  133. }