using UnityEngine;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using LitJson;
public class C_FileHelper
{
/// <summary>
/// 获取文件
/// </summary>
/// <param name="directoyPath">文件夹路径(1.可以传入绝对路径,例如:Application.dataPath 2.可以传入当前文件夹路径(Directory.GetCurrentDirectory())下的相对路径名,例如Assets/Textures)</param>
/// <param name="getAll">是否递归获取所有文件,true获取所有,false仅获取当前路径</param>
/// <param name="extension">获取指定尾缀文件(无需加.字符 多项用|分割)</param>
/// <param name="excludeExtension">剔除掉指定后缀的文件(无需加.字符 多项用|分割)</param>
/// <param name="excludeDirectory">剔除掉路径中包含指定关键字的文件(多项用|分割)</param>
public static List<string> GetAllFiles(string directoyPath, bool getAll = false, string extension = "", string excludeExtension = "", string excludeDirectory = "")
{
//需要指定的后缀
string[] extensionArr = extension.Split('|');
//需要被排除的后缀
string[] excludeExtensionArr = excludeExtension.Split('|');
//需要被排除的文件夹关键字
string[] excludeDirectoryArr = excludeDirectory.Split('|');
List<string> lstSaveFiles = new List<string>();
//递归获取所有文件后返回
RecursionGetFiles(directoyPath, lstSaveFiles, getAll, extensionArr, excludeExtensionArr, excludeDirectoryArr);
return lstSaveFiles;
}
/// <summary>
/// 递归获取所有文件
/// </summary>
static void RecursionGetFiles(string directoyPath, List<string> lstSaveFiles, bool getAll, string[] extensionArr, string[] excludeExtensionArr, string[] excludeDirectoryArr)
{
for (int i = 0; i < excludeDirectoryArr.Length; i++)
{
//存在排除文件夹 且 路径中包含被排除的词条
if (excludeDirectoryArr[i] != "" && directoyPath.ToLower().Contains(excludeDirectoryArr[i].ToLower()))
{
return;
}
}
//获取当前路径下的所有子文件
string[] allFilePaths = Directory.GetFiles(directoyPath);
for (int i = 0; i < allFilePaths.Length; i++)
{
//检测后缀
bool checkExtension = true;
//检测是否是需要排除的后缀
for (int j = 0; j < excludeExtensionArr.Length; j++)
{
//是排除后缀
if (excludeExtensionArr[j] != "" && Path.GetExtension(allFilePaths[i]) == "." + excludeExtensionArr[j].ToLower())
{
//表示是被排除的后缀
checkExtension = false;
break;
}
}
//表示是被排除的后缀直接跳过无须再检测
if (!checkExtension)
{
continue;
}
//这里表示没有指定后缀那么都满足条件直接加入
if (extensionArr == null || extensionArr.Length == 0)
{
lstSaveFiles.Add(allFilePaths[i]);
}
else//这里表示指定了后缀约束
{
//循环检测是否满足后缀约束
for (int k = 0; k < extensionArr.Length; k++) //检测是否是指定的后缀
{
if (Path.GetExtension(allFilePaths[i]) == "." + extensionArr[k])
{
lstSaveFiles.Add(allFilePaths[i]);
//已经满足条件无须再检测路径
checkExtension = false;
break;
}
}
}
}
if (getAll) //是否递归到最深层,不递归则只获取当前文件夹下
{
//获取当前路径下的其他文件夹,继续递归
string[] strDirectoryArr = Directory.GetDirectories(directoyPath);
for (int i = 0; i < strDirectoryArr.Length; i++)
{
RecursionGetFiles(strDirectoryArr[i], lstSaveFiles, getAll, extensionArr, excludeExtensionArr, excludeDirectoryArr);
}
}
}
/// <summary>
/// 获取所有重复文件
/// </summary>
/// <param name="DirectoyPath"></param>
/// <param name="lstSaveFiles"></param>
/// <param name="extension"></param>
/// <param name="excludeExtension"></param>
/// <returns></returns>
public static Dictionary<string, string> GetRepeatFiles(string DirectoyPath, string extension, string excludeExtension)
{
//获取所有文件
List<string> lstallFiles = GetAllFiles(DirectoyPath, true, extension, excludeExtension, "");
Dictionary<string, string> files = new Dictionary<string, string>();
Dictionary<string, string> dicRepeatFile = new Dictionary<string, string>();
//便历所有文件
for (int i = 0; i < lstallFiles.Count; i++)
{
string fileName = Path.GetFileName(lstallFiles[i]);
//如果当前文件名已经不存在
if (!files.ContainsKey(fileName))
{
//则将其加入到files字典中
files.Add(fileName, lstallFiles[i]);
}
else //否则表示当前文件已经存在了是重复文件
{
dicRepeatFile.Add(files[fileName] + "." + Guid.NewGuid().ToString(), lstallFiles[i]);
}
}
return dicRepeatFile;
}
/// <summary>
/// 获取相对路径 例如: C://Folder1/SubFolder/txt.txt -> C://Folder1 => SubFolder/txt.txt
/// </summary>
/// <param name="allPath">整体路径</param>
/// <param name="relativePathTop">相对路径根</param>
/// <returns></returns>
public static string GetRelativePath(string allPath, string relativePathTop)
{
allPath = Path.GetFullPath(allPath);
relativePathTop = Path.GetFullPath(relativePathTop);
return allPath.Substring(allPath.IndexOf(relativePathTop) + relativePathTop.Length);
}
/// <summary>
/// 查看文件是否存在,不引用File
/// </summary>
/// <param name="filePath">文件完成路径,包括文件本身</param>
/// <returns></returns>
public static bool CheckFileIsExist(string filePath)
{
return File.Exists(filePath);
}
/// <summary>
/// 检测后缀
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="suffix">后缀</param>
/// <returns></returns>
public static bool CheckExtension(string filePath, string extension)
{
return Path.GetExtension(filePath).TrimStart('.') == extension.TrimStart('.');
}
/// <summary>
/// 检测是否是一个存在的文件夹
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static bool CheckDirectoryIsExist(string path)
{
return Directory.Exists(path);
}
/// <summary>
/// 写入到文件
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static void WrittingText2File(string filePath, string content)
{
File.WriteAllText(filePath, content, Encoding.UTF8);
}
/// <summary>
/// 从文件读取内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static string ReadingTextFromFile(string filePath)
{
return File.ReadAllText(filePath, Encoding.UTF8);
}
/// <summary>
/// 将文件路径转换到Unity Assets 的路径
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public static string DealFilePath2Assets(string filePath)
{
string[] fileNameSplits = filePath.Split(new string[] { "Assets" }, StringSplitOptions.RemoveEmptyEntries);
if (fileNameSplits.Length > 1)
{
return "Assets" + fileNameSplits[1];
}
return filePath;
}
#region 读写Application.persistentDataPath
/// <summary>
/// 从PersistentDataPath目录下读取指定的文件内容
/// </summary>
/// <param name="path">路径</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public static string ReadAllText_4_PersistentDataPath(string fileName, Encoding encoding)
{
string res = string.Empty;
string fullPath = Application.persistentDataPath + "/" + fileName;
if (File.Exists(fullPath))
{
res = File.ReadAllText(fullPath);
}
return res;
}
/// <summary>
/// 写入到PersistentDataPath目录下
/// </summary>
/// <param name="content">写入的文本内容</param>
/// <param name="path">路径</param>
/// <param name="encoding">编码方式</param>
public static void WriteAllText_2_PersistentDataPath(string content, string fileName, Encoding encoding)
{
string fullPath = Application.persistentDataPath + "/" + fileName;
File.WriteAllText(fullPath, content, encoding);
}
/// <summary>
/// 将对象T从文本读出来
/// </summary>
/// <typeparam name="T">重要:T是一个可以被LitJson处理的对象</typeparam>
/// <param name="path">路径</param>
/// <param name="encoding">编码方式</param>
/// <returns></returns>
public static T Read_T_4_PersistentDataPath<T>(string path, Encoding encoding)
{
return JsonMapper.ToObject<T>(ReadAllText_4_PersistentDataPath(path, encoding));
}
/// <summary>
/// 将对象T写入到文本
/// </summary>
/// <typeparam name="T">重要:T是一个可以被LitJson处理的对象</typeparam>
/// <param name="content">写入的T对象</param>
/// <param name="path">路径</param>
/// <param name="encoding">编码方式</param>
public static void Write_T_2_PersistentDataPath<T>(T content, string path, Encoding encoding)
{
WriteAllText_2_PersistentDataPath(JsonMapper.ToJson(content), path, encoding);
}
/// <summary>
/// 将T对象序列化写入到文件中
/// </summary>
/// <typeparam name="T">T必须是可被序列化的</typeparam>
/// <param name="content"></param>
/// <param name="path"></param>
public static void Serialization<T>(T content, string fullPath)
{
using (FileStream fs = new FileStream(fullPath, FileMode.OpenOrCreate))
{
BinaryFormatter bfFormatter = new BinaryFormatter();
bfFormatter.Serialize(fs, content);
}
}
/// <summary>
/// 将T对象反序列化从文件中读取
/// </summary>
/// <typeparam name="T">T必须是可被序列化的</typeparam>
/// <param name="content"></param>
/// <param name="path"></param>
/// <returns></returns>
public static T DeSerialization<T>(string fullPath)
{
using (FileStream fs = new FileStream(fullPath, FileMode.OpenOrCreate))
{
BinaryFormatter bfFormatter = new BinaryFormatter();
return (T)bfFormatter.Deserialize(fs);
}
}
#endregion
}
网友评论