123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using UnityEngine.Networking;
- /// <summary>
- /// 文件帮助类
- /// </summary>
- public static class FileHelper
- {
- #region 私有属性
- /// <summary>
- /// 缓存/缓冲区
- /// </summary>
- private static byte[] m_CacheBuffer = new byte[4096];
- #endregion
-
- #region 公有方法
-
- /// <summary>
- /// 获取所有数据
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static string GetAllDataInfo(string _filePath)
- {
- if (string.IsNullOrEmpty(_filePath))
- {
- return String.Empty;
- }
- return File.ReadAllText(_filePath);
- }
-
- /// <summary>
- /// 从StreamingAssets文件夹下获取数据信息(IO模式)
- /// </summary>
- /// <param name="_path">路径集合,不需要填写streamingAssetsPath</param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 将对应文件转换为二进制数组
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- public static byte[] GetAllBytes(string _filePath)
- {
- return File.ReadAllBytes(_filePath);
- }
- /// <summary>
- /// 获取指定文件夹下所有指定类型的文件
- /// </summary>
- /// <param name="_searchPattern">文件后缀,带点号</param>
- /// <param name="_paths"></param>
- /// <returns></returns>
- public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, params string[] _paths)
- {
- string folderPath = PathHelper.CombineFilePath(_paths).Replace('/', '\\');
- return GetAllFileInfo(_searchPattern, folderPath);
- }
-
- /// <summary>
- /// 获取指定文件夹下所有指定类型的文件
- /// </summary>
- /// <param name="_searchPattern">文件后缀,带点号</param>
- /// <param name="_folderPath"></param>
- /// <returns></returns>
- public static FileInfo[] GetFileOfSpecifiedType(string _searchPattern, string _folderPath)
- {
- return GetAllFileInfo(_searchPattern, _folderPath);
- }
- /// <summary>
- /// 保存文本文件到本地
- /// </summary>
- /// <param name="_targetContent"></param>
- /// <param name="_paths"></param>
- 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);
- }
- /// <summary>
- /// 写入Json数据
- /// </summary>
- /// <param name="_t"></param>
- /// <param name="_paths"></param>
- /// <typeparam name="T"></typeparam>
- public static void WriteJsonData<T>(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);
- }
- }
- /// <summary>
- /// 拷贝文件到对应位置
- /// </summary>
- /// <param name="_sourceFilePath">源文件路径</param>
- /// <param name="_destinationPath">目标文件夹路径</param>
- /// <returns>返回拷贝文件的名称</returns>
- 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;
- }
-
- /// <summary>
- /// 通过协程的形式上传文件
- /// </summary>
- /// <param name="_sourceFilePath"></param>
- /// <param name="_destinationPath"></param>
- /// <param name="_action">返回音频文件名</param>
- /// <param name="_progressBar"></param>
- /// <returns></returns>
- public static IEnumerator UploadFileCoroutine(string _sourceFilePath, string _destinationPath,Action<string> _action,Action<float> _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;
- }
- }
- }
- }
-
- /// <summary>
- /// 删除目标文件夹下与传入文件名相同的文件
- /// </summary>
- /// <param name="_sourceFilePath">源文件路径</param>
- /// <param name="_folderPath">目标文件夹</param>
- 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;
- }
- }
- }
-
- /// <summary>
- /// 删除文件
- /// </summary>
- /// <param name="_folderPath"></param>
- /// <param name="_fileName">带后缀文件名</param>
- 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 私有方法
- /// <summary>
- /// 本地是否存在指定文件
- /// </summary>
- /// <param name="_filePath"></param>
- /// <returns></returns>
- private static bool LoaclIsExistFile(string _filePath)
- {
- return File.Exists(_filePath);
- }
- /// <summary>
- /// 删除本地指定文件
- /// </summary>
- /// <param name="_filePath"></param>
- private static void DeleteLocalFile(string _filePath)
- {
- if (LoaclIsExistFile(_filePath))
- {
- File.Delete(_filePath);
- }
- }
-
- /// <summary>
- /// 获取指定文件夹下的所有文件信息
- /// </summary>
- /// <param name="_searchPattern"></param>
- /// <param name="_folderPath"></param>
- /// <returns></returns>
- private static FileInfo[] GetAllFileInfo(string _searchPattern, string _folderPath)
- {
- // 获取指定文件夹信息
- DirectoryInfo tmpDirectoryInfo = new DirectoryInfo(_folderPath);
- // 获取文件夹下所有的文件信息
- FileInfo[] tmpFileInfos = tmpDirectoryInfo.GetFiles("*" + _searchPattern, SearchOption.AllDirectories);
- return tmpFileInfos;
- }
- /// <summary>
- /// 获取构建目标文件完整路径
- /// </summary>
- /// <param name="_sourceFilePath"></param>
- /// <param name="_destinationPath"></param>
- /// <returns>返回源文件文件名和构建后的目标我呢见完整路径</returns>
- private static string GetDestinationFilePath(string _sourceFileName, string _destinationPath)
- {
- // 构建目标文件完整路径
- string destinationFilePath = PathHelper.CombineFilePath(_destinationPath, _sourceFileName);
- return PathHelper.CombineFilePath(_destinationPath, _sourceFileName);
- }
- /// <summary>
- /// 获取源文件文件名
- /// </summary>
- /// <param name="_sourceFilePath"></param>
- /// <returns></returns>
- private static string GetSourceFileName(string _sourceFilePath)
- {
- return Path.GetFileName(_sourceFilePath);
- }
- #endregion
- }
|