FileHelper.cs 11 KB

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