YooUtility.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace YooAsset
  6. {
  7. /// <summary>
  8. /// 路径工具类
  9. /// </summary>
  10. internal static class PathUtility
  11. {
  12. /// <summary>
  13. /// 路径归一化
  14. /// 注意:替换为Linux路径格式
  15. /// </summary>
  16. public static string RegularPath(string path)
  17. {
  18. return path.Replace('\\', '/').Replace("\\", "/");
  19. }
  20. /// <summary>
  21. /// 移除路径里的后缀名
  22. /// </summary>
  23. public static string RemoveExtension(string str)
  24. {
  25. if (string.IsNullOrEmpty(str))
  26. return str;
  27. int index = str.LastIndexOf('.');
  28. if (index == -1)
  29. return str;
  30. else
  31. return str.Remove(index); //"assets/config/test.unity3d" --> "assets/config/test"
  32. }
  33. /// <summary>
  34. /// 合并路径
  35. /// </summary>
  36. public static string Combine(string path1, string path2)
  37. {
  38. return StringUtility.Format("{0}/{1}", path1, path2);
  39. }
  40. /// <summary>
  41. /// 合并路径
  42. /// </summary>
  43. public static string Combine(string path1, string path2, string path3)
  44. {
  45. return StringUtility.Format("{0}/{1}/{2}", path1, path2, path3);
  46. }
  47. /// <summary>
  48. /// 合并路径
  49. /// </summary>
  50. public static string Combine(string path1, string path2, string path3, string path4)
  51. {
  52. return StringUtility.Format("{0}/{1}/{2}/{3}", path1, path2, path3, path4);
  53. }
  54. }
  55. /// <summary>
  56. /// 字符串工具类
  57. /// </summary>
  58. internal static class StringUtility
  59. {
  60. [ThreadStatic]
  61. private static StringBuilder _cacheBuilder = new StringBuilder(2048);
  62. public static string Format(string format, object arg0)
  63. {
  64. if (string.IsNullOrEmpty(format))
  65. throw new ArgumentNullException();
  66. _cacheBuilder.Length = 0;
  67. _cacheBuilder.AppendFormat(format, arg0);
  68. return _cacheBuilder.ToString();
  69. }
  70. public static string Format(string format, object arg0, object arg1)
  71. {
  72. if (string.IsNullOrEmpty(format))
  73. throw new ArgumentNullException();
  74. _cacheBuilder.Length = 0;
  75. _cacheBuilder.AppendFormat(format, arg0, arg1);
  76. return _cacheBuilder.ToString();
  77. }
  78. public static string Format(string format, object arg0, object arg1, object arg2)
  79. {
  80. if (string.IsNullOrEmpty(format))
  81. throw new ArgumentNullException();
  82. _cacheBuilder.Length = 0;
  83. _cacheBuilder.AppendFormat(format, arg0, arg1, arg2);
  84. return _cacheBuilder.ToString();
  85. }
  86. public static string Format(string format, params object[] args)
  87. {
  88. if (string.IsNullOrEmpty(format))
  89. throw new ArgumentNullException();
  90. if (args == null)
  91. throw new ArgumentNullException();
  92. _cacheBuilder.Length = 0;
  93. _cacheBuilder.AppendFormat(format, args);
  94. return _cacheBuilder.ToString();
  95. }
  96. }
  97. /// <summary>
  98. /// 文件工具类
  99. /// </summary>
  100. internal static class FileUtility
  101. {
  102. /// <summary>
  103. /// 读取文件的文本数据
  104. /// </summary>
  105. public static string ReadAllText(string filePath)
  106. {
  107. if (File.Exists(filePath) == false)
  108. return null;
  109. return File.ReadAllText(filePath, Encoding.UTF8);
  110. }
  111. /// <summary>
  112. /// 读取文件的字节数据
  113. /// </summary>
  114. public static byte[] ReadAllBytes(string filePath)
  115. {
  116. if (File.Exists(filePath) == false)
  117. return null;
  118. return File.ReadAllBytes(filePath);
  119. }
  120. /// <summary>
  121. /// 写入文本数据(会覆盖指定路径的文件)
  122. /// </summary>
  123. public static void WriteAllText(string filePath, string content)
  124. {
  125. // 创建文件夹路径
  126. CreateFileDirectory(filePath);
  127. byte[] bytes = Encoding.UTF8.GetBytes(content);
  128. File.WriteAllBytes(filePath, bytes); //避免写入BOM标记
  129. }
  130. /// <summary>
  131. /// 写入字节数据(会覆盖指定路径的文件)
  132. /// </summary>
  133. public static void WriteAllBytes(string filePath, byte[] data)
  134. {
  135. // 创建文件夹路径
  136. CreateFileDirectory(filePath);
  137. File.WriteAllBytes(filePath, data);
  138. }
  139. /// <summary>
  140. /// 创建文件的文件夹路径
  141. /// </summary>
  142. public static void CreateFileDirectory(string filePath)
  143. {
  144. // 获取文件的文件夹路径
  145. string directory = Path.GetDirectoryName(filePath);
  146. CreateDirectory(directory);
  147. }
  148. /// <summary>
  149. /// 创建文件夹路径
  150. /// </summary>
  151. public static void CreateDirectory(string directory)
  152. {
  153. // If the directory doesn't exist, create it.
  154. if (Directory.Exists(directory) == false)
  155. Directory.CreateDirectory(directory);
  156. }
  157. /// <summary>
  158. /// 获取文件大小(字节数)
  159. /// </summary>
  160. public static long GetFileSize(string filePath)
  161. {
  162. FileInfo fileInfo = new FileInfo(filePath);
  163. return fileInfo.Length;
  164. }
  165. }
  166. /// <summary>
  167. /// 哈希工具类
  168. /// </summary>
  169. public static class HashUtility
  170. {
  171. private static string ToString(byte[] hashBytes)
  172. {
  173. string result = BitConverter.ToString(hashBytes);
  174. result = result.Replace("-", "");
  175. return result.ToLower();
  176. }
  177. #region SHA1
  178. /// <summary>
  179. /// 获取字符串的Hash值
  180. /// </summary>
  181. public static string StringSHA1(string str)
  182. {
  183. byte[] buffer = Encoding.UTF8.GetBytes(str);
  184. return BytesSHA1(buffer);
  185. }
  186. /// <summary>
  187. /// 获取文件的Hash值
  188. /// </summary>
  189. public static string FileSHA1(string filePath)
  190. {
  191. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  192. {
  193. return StreamSHA1(fs);
  194. }
  195. }
  196. /// <summary>
  197. /// 获取文件的Hash值
  198. /// </summary>
  199. public static string FileSHA1Safely(string filePath)
  200. {
  201. try
  202. {
  203. return FileSHA1(filePath);
  204. }
  205. catch (Exception e)
  206. {
  207. YooLogger.Exception(e);
  208. return string.Empty;
  209. }
  210. }
  211. /// <summary>
  212. /// 获取数据流的Hash值
  213. /// </summary>
  214. public static string StreamSHA1(Stream stream)
  215. {
  216. // 说明:创建的是SHA1类的实例,生成的是160位的散列码
  217. HashAlgorithm hash = HashAlgorithm.Create();
  218. byte[] hashBytes = hash.ComputeHash(stream);
  219. return ToString(hashBytes);
  220. }
  221. /// <summary>
  222. /// 获取字节数组的Hash值
  223. /// </summary>
  224. public static string BytesSHA1(byte[] buffer)
  225. {
  226. // 说明:创建的是SHA1类的实例,生成的是160位的散列码
  227. HashAlgorithm hash = HashAlgorithm.Create();
  228. byte[] hashBytes = hash.ComputeHash(buffer);
  229. return ToString(hashBytes);
  230. }
  231. #endregion
  232. #region MD5
  233. /// <summary>
  234. /// 获取字符串的MD5
  235. /// </summary>
  236. public static string StringMD5(string str)
  237. {
  238. byte[] buffer = Encoding.UTF8.GetBytes(str);
  239. return BytesMD5(buffer);
  240. }
  241. /// <summary>
  242. /// 获取文件的MD5
  243. /// </summary>
  244. public static string FileMD5(string filePath)
  245. {
  246. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  247. {
  248. return StreamMD5(fs);
  249. }
  250. }
  251. /// <summary>
  252. /// 获取文件的MD5
  253. /// </summary>
  254. public static string FileMD5Safely(string filePath)
  255. {
  256. try
  257. {
  258. return FileMD5(filePath);
  259. }
  260. catch (Exception e)
  261. {
  262. YooLogger.Exception(e);
  263. return string.Empty;
  264. }
  265. }
  266. /// <summary>
  267. /// 获取数据流的MD5
  268. /// </summary>
  269. public static string StreamMD5(Stream stream)
  270. {
  271. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  272. byte[] hashBytes = provider.ComputeHash(stream);
  273. return ToString(hashBytes);
  274. }
  275. /// <summary>
  276. /// 获取字节数组的MD5
  277. /// </summary>
  278. public static string BytesMD5(byte[] buffer)
  279. {
  280. MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
  281. byte[] hashBytes = provider.ComputeHash(buffer);
  282. return ToString(hashBytes);
  283. }
  284. #endregion
  285. #region CRC32
  286. /// <summary>
  287. /// 获取字符串的CRC32
  288. /// </summary>
  289. public static string StringCRC32(string str)
  290. {
  291. byte[] buffer = Encoding.UTF8.GetBytes(str);
  292. return BytesCRC32(buffer);
  293. }
  294. /// <summary>
  295. /// 获取文件的CRC32
  296. /// </summary>
  297. public static string FileCRC32(string filePath)
  298. {
  299. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
  300. {
  301. return StreamCRC32(fs);
  302. }
  303. }
  304. /// <summary>
  305. /// 获取文件的CRC32
  306. /// </summary>
  307. public static string FileCRC32Safely(string filePath)
  308. {
  309. try
  310. {
  311. return FileCRC32(filePath);
  312. }
  313. catch (Exception e)
  314. {
  315. YooLogger.Exception(e);
  316. return string.Empty;
  317. }
  318. }
  319. /// <summary>
  320. /// 获取数据流的CRC32
  321. /// </summary>
  322. public static string StreamCRC32(Stream stream)
  323. {
  324. CRC32Algorithm hash = new CRC32Algorithm();
  325. byte[] hashBytes = hash.ComputeHash(stream);
  326. return ToString(hashBytes);
  327. }
  328. /// <summary>
  329. /// 获取字节数组的CRC32
  330. /// </summary>
  331. public static string BytesCRC32(byte[] buffer)
  332. {
  333. CRC32Algorithm hash = new CRC32Algorithm();
  334. byte[] hashBytes = hash.ComputeHash(buffer);
  335. return ToString(hashBytes);
  336. }
  337. #endregion
  338. }
  339. }