FileHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using AxinLib.IO.CSV;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using UnityEngine;
  10. public class FileHelper
  11. {
  12. public static string[] SpiltData(string str)
  13. {
  14. List<string> ret = new List<string>();
  15. int size = str.Length;
  16. int startindex = 0;
  17. int endindex = 0;
  18. bool isSign = false;
  19. string it = null;
  20. for (int i = 0; i < size; i++)
  21. {
  22. if (str[i] == ',' && !isSign)
  23. {
  24. if (i >= 1 && str[i - 1] == ',')
  25. {
  26. it = string.Empty;
  27. ret.Add(it);
  28. startindex = i + 1 < size ? i + 1 : i;
  29. if (i == size - 1)
  30. {
  31. it = string.Empty;
  32. ret.Add(it);
  33. }
  34. }
  35. else
  36. {
  37. endindex = i - 1 >= 0 ? i - 1 : 0;
  38. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  39. ret.Add(it);
  40. startindex = i + 1 < size ? i + 1 : i;
  41. }
  42. }
  43. else if (str[i] == '"' && ((i >= 1 && str[i - 1] == ',')
  44. || (i + 1 < size && str[i + 1] == ',')))
  45. {
  46. isSign = !isSign;
  47. }
  48. else if (i == size - 1)
  49. {
  50. endindex = i;
  51. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  52. ret.Add(it);
  53. }
  54. }
  55. return ret.ToArray();
  56. }
  57. public static string RemoveHeadandTailChar(string str, char ch)
  58. {
  59. if (str.Length < 2)
  60. return str;
  61. string ret = str;
  62. if (str.IndexOf(ch) == 0 && str.LastIndexOf(ch) == str.Length - 1)
  63. {
  64. ret = str.Substring(1, str.Length - 2);
  65. }
  66. return ret;
  67. }
  68. public static string AddHeadandTailChar(string str, char ch)
  69. {
  70. string ret = string.Empty;
  71. ret = ch + str + ch;
  72. return ret;
  73. }
  74. public static string GB2312ToUTF8(string str)
  75. {
  76. try
  77. {
  78. Encoding uft8 = Encoding.GetEncoding(65001);
  79. Encoding gb2312 = Encoding.GetEncoding("gb2312");
  80. byte[] temp = gb2312.GetBytes(str);
  81. byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
  82. string result = uft8.GetString(temp1);
  83. return result;
  84. }
  85. catch
  86. {
  87. return null;
  88. }
  89. }
  90. public static void CreateDir(string path)
  91. {
  92. if (!Directory.Exists(path))
  93. {
  94. Directory.CreateDirectory(path);
  95. }
  96. }
  97. public static void DeleteFile(string path)
  98. {
  99. if (File.Exists(path))
  100. {
  101. File.Delete(path);
  102. Debug.Log($"删除 [{path}]");
  103. }
  104. }
  105. //写文件
  106. public static void WirteToFile(string filePath, byte[] data)
  107. {
  108. if (data == null || data.Length <= 0) return;
  109. Debug.Log($"保存到路径【{filePath}】 【长度 = {data.Length}】");
  110. CreateDir(PathRemoveBack(filePath));
  111. FileStream fs = new FileStream(filePath, FileMode.Create);
  112. fs.Write(data, 0, data.Length);
  113. fs.Close();
  114. }
  115. public static void WirteStringToFile(string filePath, string data)
  116. {
  117. byte[] bytes = Encoding.UTF8.GetBytes(data);
  118. WirteToFile(filePath, bytes);
  119. }
  120. public static async void WriteToFileAsync(string filePath, byte[] data, Action callback = null)
  121. {
  122. CreateDir(PathRemoveBack(filePath));
  123. FileStream fs = new FileStream(filePath, FileMode.Create);
  124. await fs.WriteAsync(data, 0, data.Length);
  125. fs.Close();
  126. callback?.Invoke();
  127. }
  128. public static void WirteStringToFileAsync(string filePath, string data, Action callback = null)
  129. {
  130. byte[] bytes = Encoding.UTF8.GetBytes(data);
  131. WriteToFileAsync(filePath, bytes, callback);
  132. }
  133. public static string GetName(string path)
  134. {
  135. string name = null;
  136. string[] sts = path.Split('/');
  137. if (sts != null && sts.Length >= 0)
  138. name = sts[sts.Length - 1].Split('.')[0];
  139. return name;
  140. }
  141. public static string GetFullName(string path)
  142. {
  143. string name = null;
  144. string[] sts = path.Split('/', '\\');
  145. if (sts != null && sts.Length >= 0)
  146. name = sts[sts.Length - 1];
  147. return name;
  148. }
  149. public static string PathRemoveBack(string path)
  150. {
  151. int id = path.LastIndexOfAny(new char[] { '/', '\\' });
  152. return path.Substring(0, id);
  153. }
  154. public static string GetMD5(string path)
  155. {
  156. string md5str = string.Empty;
  157. using (FileStream file = new FileStream(path, FileMode.Open))
  158. {
  159. MD5 md5 = new MD5CryptoServiceProvider();
  160. byte[] md5bytes = md5.ComputeHash(file);
  161. file.Close();
  162. StringBuilder sb = new StringBuilder();
  163. for (int i = 0; i < md5bytes.Length; i++)
  164. {
  165. sb.Append(md5bytes[i].ToString("x2"));
  166. }
  167. md5str = sb.ToString();
  168. }
  169. return md5str;
  170. }
  171. public static string GetMD5(byte[] data)
  172. {
  173. string md5str = string.Empty;
  174. MD5 md5 = new MD5CryptoServiceProvider();
  175. byte[] md5bytes = md5.ComputeHash(data);
  176. StringBuilder sb = new StringBuilder();
  177. for (int i = 0; i < md5bytes.Length; i++)
  178. {
  179. sb.Append(md5bytes[i].ToString("x2"));
  180. }
  181. md5str = sb.ToString();
  182. return md5str;
  183. }
  184. public static string[] GetAllFileNmae(string path, string exclude, bool isfullName = false,bool isGetSubPath = true,string perName = "")
  185. {
  186. List<string> paths = new List<string>();
  187. string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/";
  188. if (Directory.Exists(path))
  189. {
  190. DirectoryInfo dir = new DirectoryInfo(path);
  191. FileInfo[] fileInfos = dir.GetFiles();
  192. foreach (var item in fileInfos)
  193. {
  194. if (exclude != null && item.Name.EndsWith(exclude)) continue;
  195. if (!isfullName)
  196. {
  197. paths.Add(pername + item.Name);
  198. }
  199. else
  200. paths.Add(item.FullName);
  201. }
  202. DirectoryInfo[] directoryInfos = dir.GetDirectories();
  203. if (isGetSubPath)
  204. {
  205. if (directoryInfos != null && directoryInfos.Length > 0)
  206. {
  207. foreach (var item in directoryInfos)
  208. {
  209. string[] names = GetAllFileNmae(item.FullName, exclude, isfullName,isGetSubPath, pername + item.Name);
  210. if (names != null && names.Length > 0)
  211. {
  212. paths.AddRange(names);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. return paths.ToArray();
  219. }
  220. public static string[] GetAllFileNmae_Only(string path, string include, bool isfullName = false, bool isGetSubPath = true, string perName = "")
  221. {
  222. List<string> paths = new List<string>();
  223. string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/";
  224. if (Directory.Exists(path))
  225. {
  226. DirectoryInfo dir = new DirectoryInfo(path);
  227. FileInfo[] fileInfos = dir.GetFiles();
  228. foreach (var item in fileInfos)
  229. {
  230. if (include != null && !item.Name.EndsWith(include)) continue;
  231. if (!isfullName)
  232. {
  233. paths.Add(pername + item.Name);
  234. }
  235. else
  236. paths.Add(item.FullName);
  237. }
  238. DirectoryInfo[] directoryInfos = dir.GetDirectories();
  239. if (isGetSubPath)
  240. {
  241. if (directoryInfos != null && directoryInfos.Length > 0)
  242. {
  243. foreach (var item in directoryInfos)
  244. {
  245. string[] names = GetAllFileNmae_Only(item.FullName, include, isfullName, isGetSubPath, pername + item.Name);
  246. if (names != null && names.Length > 0)
  247. {
  248. paths.AddRange(names);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. return paths.ToArray();
  255. }
  256. public static void CopyDir(string dir, string outDir, string exclude)
  257. {
  258. string[] files1 = GetAllFileNmae(dir, exclude);
  259. if (dir.LastIndexOf('/') != dir.Length - 1)
  260. {
  261. dir += "/";
  262. }
  263. foreach (var item in files1)
  264. {
  265. Debug.Log("===============File : " + item);
  266. byte[] fdatas = File.ReadAllBytes(dir + item);
  267. WirteToFile($"{outDir}/{item}", fdatas);
  268. }
  269. }
  270. public static void CopyDir_Only(string dir, string outDir, string include)
  271. {
  272. string[] files1 = GetAllFileNmae_Only(dir, include);
  273. if (dir.LastIndexOf('/') != dir.Length - 1)
  274. {
  275. dir += "/";
  276. }
  277. foreach (var item in files1)
  278. {
  279. Debug.Log("===============File : " + item);
  280. byte[] fdatas = File.ReadAllBytes(dir + item);
  281. WirteToFile($"{outDir}/{item}", fdatas);
  282. }
  283. }
  284. public static string GetSubPath(string path, string mainPath, bool isRemoveName = true)
  285. {
  286. string subPath = string.Empty;
  287. List<string> str1 = path.Split('/', '\\').ToList();
  288. List<string> str2 = mainPath.Split('/', '\\').ToList();
  289. if (str1.Count - 1 <= str2.Count)
  290. {
  291. return subPath;
  292. }
  293. string[] rems = { ".", ".." };
  294. RemoveStrings(str2, rems);
  295. RemoveStrings(str1, rems);
  296. if (isRemoveName)
  297. {
  298. string s = str1[str1.Count - 1];
  299. str1.Remove(s);
  300. }
  301. string path1 = Path.Combine(str1.ToArray());
  302. string path2 = Path.Combine(str2.ToArray());
  303. int sindex = path1.IndexOf(path2);
  304. if (sindex != -1)
  305. {
  306. sindex = sindex + path2.Length + 1;
  307. int size = path1.Length - sindex;
  308. if (size > 0)
  309. subPath = path1.Substring(sindex, size);
  310. }
  311. return subPath;
  312. }
  313. public static void RemoveStrings(List<string> strs, string[] rems)
  314. {
  315. List<string> res = new List<string>();
  316. for (int i = 0; i < strs.Count; i++)
  317. {
  318. for (int j = 0; j < rems.Length; j++)
  319. {
  320. if (strs[i].Equals(rems[j]))
  321. {
  322. res.Add(strs[i]);
  323. }
  324. }
  325. }
  326. for (int i = 0; i < res.Count; i++)
  327. {
  328. strs.Remove(res[i]);
  329. }
  330. }
  331. public static string[][] GetCsvDatas(string text)
  332. {
  333. using (System.IO.StringReader stringReader = new System.IO.StringReader(text))
  334. {
  335. CsvReader csvReader = new CsvReader(stringReader);
  336. Field field;
  337. int r = -1;
  338. List<string[]> datas = new List<string[]>();
  339. List<string> data = new List<string>();
  340. while (null != (field = csvReader.ReadField()))
  341. {
  342. if (field.RowIndex > r)
  343. {
  344. if (data.Count > 0)
  345. {
  346. datas.Add(data.ToArray());
  347. }
  348. data.Clear();
  349. r = field.RowIndex;
  350. }
  351. data.Add(field.Value);
  352. }
  353. if (data.Count > 0)
  354. {
  355. datas.Add(data.ToArray());
  356. }
  357. return datas.ToArray();
  358. }
  359. }
  360. }