using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
using System.Text;
///
/// 【此文件如果出错,请检查playerSetting是否为Android平台】
/// 使用Application.persistentDataPath方式来创建文件,读写Xml文件.
/// 注Application.persistentDataPath末尾没有“/”符号
///
public class AssetSystem_AndroidFileHelper : MonoSingleton
{
///
/// 动态创建文件夹.
///
/// The folder.
/// 文件创建目录.
/// 文件夹名(不带符号).
public string CreateFolder(string path, string FolderName)
{
string FolderPath = path + FolderName;
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
return FolderPath;
}
///
/// 创建文件[追加内容].
///
/// 完整文件夹路径.
/// 文件的名称.
/// 写入的内容.
public void CreateFile(string path, string name, string info)
{
//文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path + name);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.CreateText();
}
else
{
//如果此文件存在则打开
sw =t.AppendText();
}
//以行的形式写入信息
sw.WriteLine(info);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
///
/// 重写指定文件
///
///
///
///
public void ReWriteFileByString(string name, string info)
{
//AssetSystem_Log.Ins.Log("重写文件:" + name +"/"+info);
StringBuilder versions = new StringBuilder(info);
byte[] data = Encoding.UTF8.GetBytes(versions.ToString());
LogHelper.Log("重写文件:" + AssetConfig_GlobalSetting.strVersionFileName + data);
ReWriteFileByByte(AssetConfig_GlobalSetting.strVersionFileName, data);
}
///
/// 重写指定文件
///
///
///
public void ReWriteFileByByte(string fileName, byte[] data)
{
LogHelper.Log("重写文件:" + AssetConfig_GlobalSetting.strLocalSavePath + fileName);
DirectoryInfo myDirectoryInfo = new DirectoryInfo(AssetConfig_GlobalSetting.strLocalSavePath + fileName);
if (myDirectoryInfo.Exists)
{
LogHelper.Log("找到指定文件!");
}
else
{
Directory.CreateDirectory(AssetConfig_GlobalSetting.strLocalSavePath);
LogHelper.Log("创建指定文件夹Res");
}
// AssetSystem_Log.Ins.Log("重写文件:" +AssetConfig_GlobalSetting.strLocalSavePath+ fileName);
FileStream stream = new FileStream(AssetConfig_GlobalSetting.strLocalSavePath + fileName, FileMode.Create);
stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();
}
///
/// 读取文件.
///
/// The file.
/// 完整文件夹路径.
/// 读取文件的名称.
public ArrayList LoadFile2(string path, string name)
{
//使用流的形式读取
StreamReader sr = null;
try
{
sr = File.OpenText(path + name);
}
catch (Exception e)
{
//路径与名称未找到文件则直接返回空
LogHelper.LogError("路径:" + path + "文件名:" + name + ",路径与名称未找到文件则直接返回空");
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的读取
//将每一行的内容存入数组链表容器中
arrlist.Add(line);
}
//关闭流
sr.Close();
//销毁流
sr.Dispose();
//将数组链表容器返回
return arrlist;
}
///
/// StreamReader读取文件
///
///
public IEnumerator LoadFile(string inFile, string outFile )
{
//float percent = 0;
float progress;
try
{
System.IO.Stream st = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
System.IO.Stream so = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
long totalBytes = st.Length;
long totalDownloadedByte = 0;
byte[] by = new byte[10];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
progress = (float)totalDownloadedByte / (float)totalBytes * 100;
//LogHelper.LogError(progress);
//"当前补丁下载进度" + percent.ToString() + "%";
AssetUI_ShowLog.Instance.logString = "当前补丁下载进度" + progress.ToString() + "%";
}
so.Close();
st.Close();
}
catch(Exception e)
{
LogHelper.LogError(e.Message);
}
yield return null;
//return percent;
}
//写入模型到本地
IEnumerator loadassetbundle(string url)
{
WWW w = new WWW(url);
yield return w;
if (w.isDone)
{
byte[] model = w.bytes;
int length = model.Length;
//写入模型到本地
CreateassetbundleFile(Application.persistentDataPath, "Model.assetbundle", model, length);
}
}
///
/// 获取文件下所有文件大小
///
///
///
public int GetAllFileSize(string filePath)
{
int sum = 0;
if (!Directory.Exists(filePath))
{
return 0;
}
DirectoryInfo dti = new DirectoryInfo(filePath);
FileInfo[] fi = dti.GetFiles();
foreach (FileInfo f in fi)
{
if (f.Extension == ".zip")
{
sum += Convert.ToInt32(f.Length);
}
}
return sum / 1024 / 1024;
}
///
/// 获取指定文件大小
///
///
///
///
public int GetFileSize(string FilePath, string FileName)
{
int sum = 0;
if (!Directory.Exists(FilePath))
{
return 0;
}
else
{
FileInfo Files = new FileInfo(@FilePath + FileName);
sum += Convert.ToInt32(Files.Length / 1024);
}
return sum;
}
///
///
///
///
///
///
///
void CreateassetbundleFile(string path, string name, byte[] info, int length)
{
//文件流信息
//StreamWriter sw;
Stream sw;
FileInfo t = new FileInfo(path + "//" + name);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.Create();
}
else
{
//如果此文件存在则打开
//sw = t.Append();
return;
}
//以行的形式写入信息
sw.Write(info, 0, length);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
//读取本地AssetBundle文件
IEnumerator LoadAssetbundleFromLocal(string path, string name)
{
//print("file:///" + path + "/" + name);
WWW w = new WWW("file:///" + path + "/" + name);
yield return w;
if (w.isDone)
{
Instantiate(w.assetBundle.LoadAsset(name));
}
}
///
/// 删除文件.
///
/// 删除完整文件夹路径.
/// 删除文件的名称.
public void DeleteFile(string path, string name)
{
File.Delete(path + name);
}
///
/// 删除文件
///
///
///
///
public bool DeleteFiles(string path, string filesName)
{
bool isDelete = false;
try
{
if (Directory.Exists(path))
{
if (File.Exists(path + "\\" + filesName))
{
File.Delete(path + "\\" + filesName);
isDelete = true;
}
}
}
catch
{
return isDelete;
}
return isDelete;
}
}