FileHelper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. 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. if (i == size - 1 && !isSign)
  47. {
  48. endindex = i;
  49. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  50. ret.Add(it);
  51. }
  52. }
  53. else if (i == size - 1)
  54. {
  55. endindex = i;
  56. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  57. ret.Add(it);
  58. }
  59. }
  60. return ret.ToArray();
  61. }
  62. public static string[] SpiltCsvData(string str)
  63. {
  64. List<string> ret = new List<string>();
  65. int size = str.Length;
  66. int startindex = 0;
  67. int endindex = 0;
  68. bool isSign = false;
  69. string it = null;
  70. for (int i = 0; i < size; i++)
  71. {
  72. if (str[i] == ',' && !isSign)
  73. {
  74. if (i >= 1 && str[i - 1] == ',')
  75. {
  76. it = string.Empty;
  77. ret.Add(it);
  78. startindex = i + 1 < size ? i + 1 : i;
  79. if (i == size - 1)
  80. {
  81. it = string.Empty;
  82. ret.Add(it);
  83. }
  84. }
  85. else
  86. {
  87. endindex = i - 1 >= 0 ? i - 1 : 0;
  88. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  89. ret.Add(it);
  90. startindex = i + 1 < size ? i + 1 : i;
  91. }
  92. }
  93. else if (str[i] == '"' && ((i >= 1 && str[i - 1] != '\\') || i == 0))
  94. {
  95. isSign = !isSign;
  96. if (i == size - 1 && !isSign)
  97. {
  98. endindex = i;
  99. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  100. ret.Add(it);
  101. }
  102. }
  103. else if (i == size - 1)
  104. {
  105. endindex = i;
  106. it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"');
  107. ret.Add(it);
  108. }
  109. }
  110. return ret.ToArray();
  111. }
  112. public static string RemoveHeadandTailChar(string str, char ch)
  113. {
  114. if (str.Length < 2)
  115. return str;
  116. string ret = str;
  117. if (str.IndexOf(ch) == 0 && str.LastIndexOf(ch) == str.Length - 1)
  118. {
  119. ret = str.Substring(1, str.Length - 2);
  120. }
  121. return ret;
  122. }
  123. public static string AddHeadandTailChar(string str, char ch)
  124. {
  125. string ret = string.Empty;
  126. ret = ch + str + ch;
  127. return ret;
  128. }
  129. public static string GB2312ToUTF8(string str)
  130. {
  131. try
  132. {
  133. Encoding uft8 = Encoding.GetEncoding(65001);
  134. Encoding gb2312 = Encoding.GetEncoding("gb2312");
  135. byte[] temp = gb2312.GetBytes(str);
  136. byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
  137. string result = uft8.GetString(temp1);
  138. return result;
  139. }
  140. catch
  141. {
  142. return null;
  143. }
  144. }
  145. public static void CreateDir(string path)
  146. {
  147. if (!Directory.Exists(path))
  148. {
  149. Directory.CreateDirectory(path);
  150. }
  151. }
  152. public static void DeleteFile(string path)
  153. {
  154. if (File.Exists(path))
  155. {
  156. File.Delete(path);
  157. // Debug.Log($"删除 [{path}]");
  158. }
  159. }
  160. //写文件
  161. public static void WirteToFile(string filePath, byte[] data)
  162. {
  163. if (data == null || data.Length <= 0) return;
  164. //Debug.Log($"保存到路径【{filePath}】 【长度 = {data.Length}】");
  165. CreateDir(PathRemoveBack(filePath));
  166. FileStream fs = new FileStream(filePath, FileMode.Create);
  167. fs.Write(data, 0, data.Length);
  168. fs.Close();
  169. }
  170. public static void WirteStringToFile(string filePath, string data)
  171. {
  172. byte[] bytes = Encoding.UTF8.GetBytes(data);
  173. WirteToFile(filePath, bytes);
  174. }
  175. public static async void WriteToFileAsync(string filePath, byte[] data, Action callback = null)
  176. {
  177. CreateDir(PathRemoveBack(filePath));
  178. FileStream fs = new FileStream(filePath, FileMode.Create);
  179. await fs.WriteAsync(data, 0, data.Length);
  180. fs.Close();
  181. callback?.Invoke();
  182. }
  183. public static void WirteStringToFileAsync(string filePath, string data, Action callback = null)
  184. {
  185. byte[] bytes = Encoding.UTF8.GetBytes(data);
  186. WriteToFileAsync(filePath, bytes, callback);
  187. }
  188. public static string GetName(string path)
  189. {
  190. string name = null;
  191. string[] sts = path.Split('/');
  192. if (sts != null && sts.Length >= 0)
  193. name = sts[sts.Length - 1].Split('.')[0];
  194. return name;
  195. }
  196. public static string GetFullName(string path)
  197. {
  198. string name = null;
  199. string[] sts = path.Split('/', '\\');
  200. if (sts != null && sts.Length >= 0)
  201. name = sts[sts.Length - 1];
  202. return name;
  203. }
  204. public static string PathRemoveBack(string path)
  205. {
  206. int id = path.LastIndexOfAny(new char[] { '/', '\\' });
  207. return path.Substring(0, id);
  208. }
  209. public static string GetMD5(string path)
  210. {
  211. string md5str = string.Empty;
  212. using (FileStream file = new FileStream(path, FileMode.Open))
  213. {
  214. MD5 md5 = new MD5CryptoServiceProvider();
  215. byte[] md5bytes = md5.ComputeHash(file);
  216. file.Close();
  217. StringBuilder sb = new StringBuilder();
  218. for (int i = 0; i < md5bytes.Length; i++)
  219. {
  220. sb.Append(md5bytes[i].ToString("x2"));
  221. }
  222. md5str = sb.ToString();
  223. }
  224. return md5str;
  225. }
  226. public static string GetMD5(byte[] data)
  227. {
  228. string md5str = string.Empty;
  229. MD5 md5 = new MD5CryptoServiceProvider();
  230. byte[] md5bytes = md5.ComputeHash(data);
  231. StringBuilder sb = new StringBuilder();
  232. for (int i = 0; i < md5bytes.Length; i++)
  233. {
  234. sb.Append(md5bytes[i].ToString("x2"));
  235. }
  236. md5str = sb.ToString();
  237. return md5str;
  238. }
  239. public static string[] GetAllFileNmae(string path, string exclude, bool isfullName = false,bool isGetSubPath = true,string perName = "")
  240. {
  241. List<string> paths = new List<string>();
  242. string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/";
  243. if (Directory.Exists(path))
  244. {
  245. DirectoryInfo dir = new DirectoryInfo(path);
  246. FileInfo[] fileInfos = dir.GetFiles();
  247. foreach (var item in fileInfos)
  248. {
  249. if (exclude != null && item.Name.EndsWith(exclude)) continue;
  250. if (!isfullName)
  251. {
  252. paths.Add(pername + item.Name);
  253. }
  254. else
  255. paths.Add(item.FullName);
  256. }
  257. DirectoryInfo[] directoryInfos = dir.GetDirectories();
  258. if (isGetSubPath)
  259. {
  260. if (directoryInfos != null && directoryInfos.Length > 0)
  261. {
  262. foreach (var item in directoryInfos)
  263. {
  264. string[] names = GetAllFileNmae(item.FullName, exclude, isfullName,isGetSubPath, pername + item.Name);
  265. if (names != null && names.Length > 0)
  266. {
  267. paths.AddRange(names);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. return paths.ToArray();
  274. }
  275. public static string[] GetAllFileNmae_Only(string path, string include, bool isfullName = false, bool isGetSubPath = true, string perName = "")
  276. {
  277. List<string> paths = new List<string>();
  278. string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/";
  279. if (Directory.Exists(path))
  280. {
  281. DirectoryInfo dir = new DirectoryInfo(path);
  282. FileInfo[] fileInfos = dir.GetFiles();
  283. foreach (var item in fileInfos)
  284. {
  285. if (include != null && !item.Name.EndsWith(include)) continue;
  286. if (!isfullName)
  287. {
  288. paths.Add(pername + item.Name);
  289. }
  290. else
  291. paths.Add(item.FullName);
  292. }
  293. DirectoryInfo[] directoryInfos = dir.GetDirectories();
  294. if (isGetSubPath)
  295. {
  296. if (directoryInfos != null && directoryInfos.Length > 0)
  297. {
  298. foreach (var item in directoryInfos)
  299. {
  300. string[] names = GetAllFileNmae_Only(item.FullName, include, isfullName, isGetSubPath, pername + item.Name);
  301. if (names != null && names.Length > 0)
  302. {
  303. paths.AddRange(names);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return paths.ToArray();
  310. }
  311. public static void CopyDir(string dir, string outDir, string exclude)
  312. {
  313. string[] files1 = GetAllFileNmae(dir, exclude);
  314. if (dir.LastIndexOf('/') != dir.Length - 1)
  315. {
  316. dir += "/";
  317. }
  318. foreach (var item in files1)
  319. {
  320. //Debug.Log("===============File : " + item);
  321. byte[] fdatas = File.ReadAllBytes(dir + item);
  322. WirteToFile($"{outDir}/{item}", fdatas);
  323. }
  324. }
  325. public static void CopyDir_Only(string dir, string outDir, string include)
  326. {
  327. string[] files1 = GetAllFileNmae_Only(dir, include);
  328. if (dir.LastIndexOf('/') != dir.Length - 1)
  329. {
  330. dir += "/";
  331. }
  332. foreach (var item in files1)
  333. {
  334. //Debug.Log("===============File : " + item);
  335. byte[] fdatas = File.ReadAllBytes(dir + item);
  336. WirteToFile($"{outDir}/{item}", fdatas);
  337. }
  338. }
  339. public static string GetSubPath(string path, string mainPath, bool isRemoveName = true)
  340. {
  341. string subPath = string.Empty;
  342. List<string> str1 = path.Split('/', '\\').ToList();
  343. List<string> str2 = mainPath.Split('/', '\\').ToList();
  344. if (str1.Count - 1 <= str2.Count)
  345. {
  346. return subPath;
  347. }
  348. string[] rems = { ".", ".." };
  349. RemoveStrings(str2, rems);
  350. RemoveStrings(str1, rems);
  351. if (isRemoveName)
  352. {
  353. string s = str1[str1.Count - 1];
  354. str1.Remove(s);
  355. }
  356. string path1 = Path.Combine(str1.ToArray());
  357. string path2 = Path.Combine(str2.ToArray());
  358. int sindex = path1.IndexOf(path2);
  359. if (sindex != -1)
  360. {
  361. sindex = sindex + path2.Length + 1;
  362. int size = path1.Length - sindex;
  363. if (size > 0)
  364. subPath = path1.Substring(sindex, size);
  365. }
  366. return subPath;
  367. }
  368. public static void RemoveStrings(List<string> strs, string[] rems)
  369. {
  370. List<string> res = new List<string>();
  371. for (int i = 0; i < strs.Count; i++)
  372. {
  373. for (int j = 0; j < rems.Length; j++)
  374. {
  375. if (strs[i].Equals(rems[j]))
  376. {
  377. res.Add(strs[i]);
  378. }
  379. }
  380. }
  381. for (int i = 0; i < res.Count; i++)
  382. {
  383. strs.Remove(res[i]);
  384. }
  385. }
  386. public static string CatStringArray(IEnumerable<string> datas,string catstr)
  387. {
  388. StringBuilder stringBuilder = new StringBuilder();
  389. foreach (var item in datas)
  390. {
  391. stringBuilder.Append(item);
  392. stringBuilder.Append(catstr);
  393. }
  394. return stringBuilder.ToString();
  395. }
  396. public static bool CheckStringIsTrue(string str)
  397. {
  398. return str == "true" || str == "TRUE" || str == "True";
  399. }
  400. //public static string[][] GetCsvDatas(string text)
  401. //{
  402. // using (System.IO.StringReader stringReader = new System.IO.StringReader(text))
  403. // {
  404. // CsvReader csvReader = new CsvReader(stringReader);
  405. // Field field;
  406. // int r = -1;
  407. // List<string[]> datas = new List<string[]>();
  408. // List<string> data = new List<string>();
  409. // while (null != (field = csvReader.ReadField()))
  410. // {
  411. // if (field.RowIndex > r)
  412. // {
  413. // if (data.Count > 0)
  414. // {
  415. // datas.Add(data.ToArray());
  416. // }
  417. // data.Clear();
  418. // r = field.RowIndex;
  419. // }
  420. // data.Add(field.Value);
  421. // }
  422. // if (data.Count > 0)
  423. // {
  424. // datas.Add(data.ToArray());
  425. // }
  426. // return datas.ToArray();
  427. // }
  428. //}
  429. }