//using AxinLib.IO.CSV; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; public class FileHelper { public static string[] SpiltData(string str) { List ret = new List(); int size = str.Length; int startindex = 0; int endindex = 0; bool isSign = false; string it = null; for (int i = 0; i < size; i++) { if (str[i] == ',' && !isSign) { if (i >= 1 && str[i - 1] == ',') { it = string.Empty; ret.Add(it); startindex = i + 1 < size ? i + 1 : i; if (i == size - 1) { it = string.Empty; ret.Add(it); } } else { endindex = i - 1 >= 0 ? i - 1 : 0; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); startindex = i + 1 < size ? i + 1 : i; } } else if (str[i] == '"' && ((i >= 1 && str[i - 1] == ',') || (i + 1 < size && str[i + 1] == ','))) { isSign = !isSign; if (i == size - 1 && !isSign) { endindex = i; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); } } else if (i == size - 1) { endindex = i; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); } } return ret.ToArray(); } public static string[] SpiltCsvData(string str) { List ret = new List(); int size = str.Length; int startindex = 0; int endindex = 0; bool isSign = false; string it = null; for (int i = 0; i < size; i++) { if (str[i] == ',' && !isSign) { if (i >= 1 && str[i - 1] == ',') { it = string.Empty; ret.Add(it); startindex = i + 1 < size ? i + 1 : i; if (i == size - 1) { it = string.Empty; ret.Add(it); } } else { endindex = i - 1 >= 0 ? i - 1 : 0; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); startindex = i + 1 < size ? i + 1 : i; } } else if (str[i] == '"' && ((i >= 1 && str[i - 1] != '\\') || i == 0)) { isSign = !isSign; if (i == size - 1 && !isSign) { endindex = i; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); } } else if (i == size - 1) { endindex = i; it = RemoveHeadandTailChar(str.Substring(startindex, endindex - startindex + 1), '"'); ret.Add(it); } } return ret.ToArray(); } public static string RemoveHeadandTailChar(string str, char ch) { if (str.Length < 2) return str; string ret = str; if (str.IndexOf(ch) == 0 && str.LastIndexOf(ch) == str.Length - 1) { ret = str.Substring(1, str.Length - 2); } return ret; } public static string AddHeadandTailChar(string str, char ch) { string ret = string.Empty; ret = ch + str + ch; return ret; } public static string GB2312ToUTF8(string str) { try { Encoding uft8 = Encoding.GetEncoding(65001); Encoding gb2312 = Encoding.GetEncoding("gb2312"); byte[] temp = gb2312.GetBytes(str); byte[] temp1 = Encoding.Convert(gb2312, uft8, temp); string result = uft8.GetString(temp1); return result; } catch { return null; } } public static void CreateDir(string path) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } public static void DeleteFile(string path) { if (File.Exists(path)) { File.Delete(path); // Debug.Log($"删除 [{path}]"); } } //写文件 public static void WirteToFile(string filePath, byte[] data) { if (data == null || data.Length <= 0) return; //Debug.Log($"保存到路径【{filePath}】 【长度 = {data.Length}】"); CreateDir(PathRemoveBack(filePath)); FileStream fs = new FileStream(filePath, FileMode.Create); fs.Write(data, 0, data.Length); fs.Close(); } public static void WirteStringToFile(string filePath, string data) { byte[] bytes = Encoding.UTF8.GetBytes(data); WirteToFile(filePath, bytes); } public static async void WriteToFileAsync(string filePath, byte[] data, Action callback = null) { CreateDir(PathRemoveBack(filePath)); FileStream fs = new FileStream(filePath, FileMode.Create); await fs.WriteAsync(data, 0, data.Length); fs.Close(); callback?.Invoke(); } public static void WirteStringToFileAsync(string filePath, string data, Action callback = null) { byte[] bytes = Encoding.UTF8.GetBytes(data); WriteToFileAsync(filePath, bytes, callback); } public static string GetName(string path) { string name = null; string[] sts = path.Split('/'); if (sts != null && sts.Length >= 0) name = sts[sts.Length - 1].Split('.')[0]; return name; } public static string GetFullName(string path) { string name = null; string[] sts = path.Split('/', '\\'); if (sts != null && sts.Length >= 0) name = sts[sts.Length - 1]; return name; } public static string PathRemoveBack(string path) { int id = path.LastIndexOfAny(new char[] { '/', '\\' }); return path.Substring(0, id); } public static string GetMD5(string path) { string md5str = string.Empty; using (FileStream file = new FileStream(path, FileMode.Open)) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5bytes = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < md5bytes.Length; i++) { sb.Append(md5bytes[i].ToString("x2")); } md5str = sb.ToString(); } return md5str; } public static string GetMD5(byte[] data) { string md5str = string.Empty; MD5 md5 = new MD5CryptoServiceProvider(); byte[] md5bytes = md5.ComputeHash(data); StringBuilder sb = new StringBuilder(); for (int i = 0; i < md5bytes.Length; i++) { sb.Append(md5bytes[i].ToString("x2")); } md5str = sb.ToString(); return md5str; } public static string[] GetAllFileNmae(string path, string exclude, bool isfullName = false,bool isGetSubPath = true,string perName = "") { List paths = new List(); string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/"; if (Directory.Exists(path)) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] fileInfos = dir.GetFiles(); foreach (var item in fileInfos) { if (exclude != null && item.Name.EndsWith(exclude)) continue; if (!isfullName) { paths.Add(pername + item.Name); } else paths.Add(item.FullName); } DirectoryInfo[] directoryInfos = dir.GetDirectories(); if (isGetSubPath) { if (directoryInfos != null && directoryInfos.Length > 0) { foreach (var item in directoryInfos) { string[] names = GetAllFileNmae(item.FullName, exclude, isfullName,isGetSubPath, pername + item.Name); if (names != null && names.Length > 0) { paths.AddRange(names); } } } } } return paths.ToArray(); } public static string[] GetAllFileNmae_Only(string path, string include, bool isfullName = false, bool isGetSubPath = true, string perName = "") { List paths = new List(); string pername = string.IsNullOrEmpty(perName) ? "" : $"{perName}/"; if (Directory.Exists(path)) { DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] fileInfos = dir.GetFiles(); foreach (var item in fileInfos) { if (include != null && !item.Name.EndsWith(include)) continue; if (!isfullName) { paths.Add(pername + item.Name); } else paths.Add(item.FullName); } DirectoryInfo[] directoryInfos = dir.GetDirectories(); if (isGetSubPath) { if (directoryInfos != null && directoryInfos.Length > 0) { foreach (var item in directoryInfos) { string[] names = GetAllFileNmae_Only(item.FullName, include, isfullName, isGetSubPath, pername + item.Name); if (names != null && names.Length > 0) { paths.AddRange(names); } } } } } return paths.ToArray(); } public static void CopyDir(string dir, string outDir, string exclude) { string[] files1 = GetAllFileNmae(dir, exclude); if (dir.LastIndexOf('/') != dir.Length - 1) { dir += "/"; } foreach (var item in files1) { //Debug.Log("===============File : " + item); byte[] fdatas = File.ReadAllBytes(dir + item); WirteToFile($"{outDir}/{item}", fdatas); } } public static void CopyDir_Only(string dir, string outDir, string include) { string[] files1 = GetAllFileNmae_Only(dir, include); if (dir.LastIndexOf('/') != dir.Length - 1) { dir += "/"; } foreach (var item in files1) { //Debug.Log("===============File : " + item); byte[] fdatas = File.ReadAllBytes(dir + item); WirteToFile($"{outDir}/{item}", fdatas); } } public static string GetSubPath(string path, string mainPath, bool isRemoveName = true) { string subPath = string.Empty; List str1 = path.Split('/', '\\').ToList(); List str2 = mainPath.Split('/', '\\').ToList(); if (str1.Count - 1 <= str2.Count) { return subPath; } string[] rems = { ".", ".." }; RemoveStrings(str2, rems); RemoveStrings(str1, rems); if (isRemoveName) { string s = str1[str1.Count - 1]; str1.Remove(s); } string path1 = Path.Combine(str1.ToArray()); string path2 = Path.Combine(str2.ToArray()); int sindex = path1.IndexOf(path2); if (sindex != -1) { sindex = sindex + path2.Length + 1; int size = path1.Length - sindex; if (size > 0) subPath = path1.Substring(sindex, size); } return subPath; } public static void RemoveStrings(List strs, string[] rems) { List res = new List(); for (int i = 0; i < strs.Count; i++) { for (int j = 0; j < rems.Length; j++) { if (strs[i].Equals(rems[j])) { res.Add(strs[i]); } } } for (int i = 0; i < res.Count; i++) { strs.Remove(res[i]); } } public static string CatStringArray(IEnumerable datas,string catstr) { StringBuilder stringBuilder = new StringBuilder(); foreach (var item in datas) { stringBuilder.Append(item); stringBuilder.Append(catstr); } return stringBuilder.ToString(); } public static bool CheckStringIsTrue(string str) { return str == "true" || str == "TRUE" || str == "True"; } //public static string[][] GetCsvDatas(string text) //{ // using (System.IO.StringReader stringReader = new System.IO.StringReader(text)) // { // CsvReader csvReader = new CsvReader(stringReader); // Field field; // int r = -1; // List datas = new List(); // List data = new List(); // while (null != (field = csvReader.ReadField())) // { // if (field.RowIndex > r) // { // if (data.Count > 0) // { // datas.Add(data.ToArray()); // } // data.Clear(); // r = field.RowIndex; // } // data.Add(field.Value); // } // if (data.Count > 0) // { // datas.Add(data.ToArray()); // } // return datas.ToArray(); // } //} }