YooUtility.cs 8.2 KB

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