| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- public class FileHelper
- {
- public static string[] SpiltData(string str)
- {
- List<string> ret = new List<string>();
- 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;
- }
- 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<string> paths = new List<string>();
- 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<string> paths = new List<string>();
- 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<string> str1 = path.Split('/', '\\').ToList();
- List<string> 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<string> strs, string[] rems)
- {
- List<string> res = new List<string>();
- 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]);
- }
- }
- }
|