using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.Networking; /// /// 文件帮助类 /// public static class FileHelper { #region 私有属性 /// /// 缓存/缓冲区 /// private static byte[] m_CacheBuffer = new byte[4096]; #endregion #region 公有方法 /// /// 获取所有数据 /// /// /// public static string GetAllDataInfo(string _filePath) { if (string.IsNullOrEmpty(_filePath)) { return String.Empty; } return File.ReadAllText(_filePath); } /// /// 从StreamingAssets文件夹下获取数据信息(IO模式) /// /// 路径集合,不需要填写streamingAssetsPath /// public static string GetAllDataInfoForStreamingAssets(params string[] _filePath) { if (_filePath.Length <= 0) { return string.Empty; } string result = String.Empty; result = GetAllDataInfo(Application.streamingAssetsPath + "/" + PathHelper.CombineFilePath(_filePath)); return result; } /// /// 将对应文件转换为二进制数组 /// /// /// public static byte[] GetAllBytes(string _filePath) { return File.ReadAllBytes(_filePath); } /// /// 获取指定文件夹下所有指定类型的文件 /// /// 文件后缀,带点号 /// /// public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, params string[] _paths) { string folderPath = PathHelper.CombineFilePath(_paths).Replace('/', '\\'); return GetAllFileInfo(_searchPattern, folderPath); } /// /// 获取指定文件夹下所有指定类型的文件 /// /// 文件后缀,带点号 /// /// public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, string _folderPath) { return GetAllFileInfo(_searchPattern, _folderPath); } /// /// 保存文本文件到本地 /// /// /// public static void SaveTextToFile(string _targetContent, params string[] _paths) { if (string.IsNullOrEmpty(_targetContent) || _paths.Length <= 0) { return; } // 合并路径 string savePath = PathHelper.CombineFilePath(_paths); // 获取文件夹路径 string foldrePath = Path.GetDirectoryName(savePath); // 判断文件夹是否存在 if (!Directory.Exists(foldrePath)) { // 创建文件夹 Directory.CreateDirectory(foldrePath); } // 写入文件 File.WriteAllText(savePath,_targetContent); } /// /// 写入Json数据 /// /// /// /// public static void WriteJsonData(T _t, params string[] _paths) { if (_t == null || _paths.Length <= 0) { return; } // 合并路径 string filePath = PathHelper.CombineFilePath(_paths); if (!File.Exists(filePath)) { return; } string tmpContent = JsonUtility.ToJson(_t).Replace(@"\\r\\n",@"\r\n"); using (StreamWriter streamWriter = new StreamWriter(filePath)) { streamWriter.Write(tmpContent); } } /// /// 拷贝文件到对应位置 /// /// 源文件路径 /// 目标文件夹路径 /// 返回拷贝文件的名称 public static string CopeFileTocorrespondlocation(string _sourceFilePath, string _destinationPath) { if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_destinationPath)) { return String.Empty; } // 调用删除方法 DeleteFilewithSameName(_sourceFilePath, _destinationPath); string tmpFileName = GetSourceFileName(_sourceFilePath); string tmpCompletePath = GetDestinationFilePath(tmpFileName, _destinationPath); // 拷贝文件 File.Copy(_sourceFilePath,tmpCompletePath,true); return tmpFileName; } /// /// 通过协程的形式上传文件 /// /// /// /// 返回音频文件名 /// /// public static IEnumerator UploadFileCoroutine(string _sourceFilePath, string _destinationPath,Action _action,Action _progressBar) { if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_destinationPath)) { yield return null; } DeleteFilewithSameName(_sourceFilePath, _destinationPath); string tmpFileName = GetSourceFileName(_sourceFilePath); string tmpCompletePath = GetDestinationFilePath(tmpFileName, _destinationPath); if (_action != null) { _action.Invoke(tmpFileName); } if (string.IsNullOrEmpty(_sourceFilePath)) { yield return null; } // 文件拷贝操作 using (FileStream sourceFile = File.OpenRead(_sourceFilePath)) { using (FileStream destinationFile=File.Create(tmpCompletePath)) { // 获取源文件大小 long totalSourceLength = sourceFile.Length; // 记录拷贝数值 long bytesCopied = 0; while (bytesCopied < totalSourceLength) { // 读取源文件中的数据到缓冲区 int bytesRead = sourceFile.Read(m_CacheBuffer, 0, m_CacheBuffer.Length); // 写入缓冲区数据到目标文件 destinationFile.Write(m_CacheBuffer, 0, bytesRead); // 修订拷贝长度 bytesCopied += bytesRead; // 计算加载进度 if (_progressBar != null) { _progressBar.Invoke((float)bytesCopied / (float)totalSourceLength); } yield return null; } } } } /// /// 删除目标文件夹下与传入文件名相同的文件 /// /// 源文件路径 /// 目标文件夹 public static void DeleteFilewithSameName(string _sourceFilePath,string _folderPath) { if (string.IsNullOrEmpty(_sourceFilePath) || string.IsNullOrEmpty(_folderPath)) { return; } // 获取源文件名 string sourceFileName = Path.GetFileName(_sourceFilePath); // 获取指定文件夹下所有的文件 FileInfo[] fileInfos = GetFileOfSpecifiedType(sourceFileName.Split('.')[1], _folderPath); // 目标文件夹无数据 if (fileInfos.Length <= 0) { return; } for (int i = 0; i < fileInfos.Length; i++) { // 获取文件名 如果相同,则直接删除 if (sourceFileName.Equals(Path.GetFileName(fileInfos[i].FullName))) { DeleteLocalFile(fileInfos[i].FullName); return; } } } /// /// 删除文件 /// /// /// 带后缀文件名 public static void DeleteFile(string _folderPath,string _fileName) { if (string.IsNullOrEmpty(_folderPath) || string.IsNullOrEmpty(_fileName)) { return; } string filePath = PathHelper.CombineFilePath(PathHelper.m_ReadOnlyPath, _folderPath, _fileName); DeleteLocalFile(filePath); } #endregion #region 私有方法 /// /// 本地是否存在指定文件 /// /// /// private static bool LoaclIsExistFile(string _filePath) { return File.Exists(_filePath); } /// /// 删除本地指定文件 /// /// private static void DeleteLocalFile(string _filePath) { if (LoaclIsExistFile(_filePath)) { File.Delete(_filePath); } } /// /// 获取指定文件夹下的所有文件信息 /// /// /// /// private static FileInfo[] GetAllFileInfo(string _searchPattern, string _folderPath) { // 获取指定文件夹信息 DirectoryInfo tmpDirectoryInfo = new DirectoryInfo(_folderPath); // 获取文件夹下所有的文件信息 FileInfo[] tmpFileInfos = tmpDirectoryInfo.GetFiles("*" + _searchPattern, SearchOption.AllDirectories); return tmpFileInfos; } /// /// 获取构建目标文件完整路径 /// /// /// /// 返回源文件文件名和构建后的目标我呢见完整路径 private static string GetDestinationFilePath(string _sourceFileName, string _destinationPath) { // 构建目标文件完整路径 string destinationFilePath = PathHelper.CombineFilePath(_destinationPath, _sourceFileName); return PathHelper.CombineFilePath(_destinationPath, _sourceFileName); } /// /// 获取源文件文件名 /// /// /// private static string GetSourceFileName(string _sourceFilePath) { return Path.GetFileName(_sourceFilePath); } #endregion }