TestCodeStructure/Assets/Scripts/Utils/FileTool.cs

102 lines
2.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class FileTool
{
public static void CreateFile(string fileDict, string fileName)
{
if (!File.Exists(fileDict))
{
Directory.CreateDirectory(fileDict);
}
FileInfo file = new FileInfo($"{fileDict}/{fileName}");
file.Create().Dispose();
}
public static void DeleteAllFile(string srcPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(srcPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
}
}
catch (Exception e)
{
Debug.Log($"delete {srcPath} error");
}
}
public static void CreatePath(string path)
{
FileInfo fi = new FileInfo(path);
DirectoryInfo dir = fi.Directory;
if (!dir.Exists)
{
dir.Create();
}
}
/// <summary>
/// 加密/解密
/// </summary>
/// <param name="targetData">文件流</param>
// public static void Encypt(ref byte[] targetData)
// {
// //加密与key异或解密的时候同样如此
// int dataLength = targetData.Length;
// for (int i = 0; i < dataLength; ++i)
// {
// targetData[i] = (byte) (targetData[i] ^ ApplicationConst.Encry_Key);
// }
// }
/// <summary>
/// 从本地文件中解密并加载AB包
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
// public static async Task<AssetBundle> LoadAssetBundleFromBytes(string path)
// {
// // Log.Debug($"path : {path}");
// byte[] content;
// #if UNITY_ANDROID && !UNITY_EDITOR
// UnityWebRequest getFile = UnityWebRequest.Get(path);
// UnityWebRequestAsyncOperation req = getFile.SendWebRequest();
// while (!req.isDone)
// {
// await Task.Yield();
// }
// content = getFile.downloadHandler.data;
// #else
// content = File.ReadAllBytes(path);
// #endif
// Encypt(ref content);
// var load = AssetBundle.LoadFromMemoryAsync(content);
// do
// {
// await Task.Yield();
// } while (!load.isDone);
// return load.assetBundle;
// }
}