string persistentDataPath_ = Application.persistentDataPath;
string resourcePath_ = Path.Combine(persistentDataPath_, "151515");
//设置存储位置
string zipPath_ = (resourcePath_ + ".zip").Replace(@"\", "/");
if (Directory.Exists(resourcePath_))
Directory.Delete(resourcePath_, true);
if (File.Exists(zipPath_))
File.Delete(zipPath_);
String filePath = @"您的url地址";//本地路径不行,必须是http或者https或者www才可以
//写入流
FileStream stream = File.Open(zipPath_, FileMode.OpenOrCreate);
//创建httpwebrequest请求
HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create(filePath);
downloadRequest.Method = "GET";
using (HttpTools downloader_ = new HttpTools(downloadRequest, null, stream))
{
yield return downloader_;
bool updateResult = false;
//清空数据流关闭
stream.Flush();
stream.Close();
bool decompressFinish_ = false;
if (downloader_.Exception == null)
{
// 解压资源包
AsyncExtra(zipPath_, persistentDataPath_, delegate(bool b)
{
updateResult = b;
decompressFinish_ = true;
});
// 等待压缩完成
while (!decompressFinish_) yield return null;
}
else
{
//ReportManager.Inst.ReportException(this.downloader_.Exception);
decompressFinish_ = true;
}
if (updateResult)
{
LoadPrint("更新完成");
}
else
{
LoadPrint("更新失败");
}
}
//第二部分
/// <summary>
/// 将指定的zip文件解压缩到指定目录
/// </summary>
/// <param name="path_"></param>
/// <param name="targetDir"></param>
void AsyncExtra(string path_, string targetDir, Action<bool> cb)
{
Thread t = new Thread(delegate()
{
try
{
if (!Directory.Exists(targetDir) && !string.IsNullOrEmpty(targetDir))
Directory.CreateDirectory(targetDir);
using (ZipInputStream zipStream = new ZipInputStream(File.OpenRead(path_)))
{
//this.state_ = ResourceUpdateState.Decompression;
ZipEntry entry;
while ((entry = zipStream.GetNextEntry()) != null)
ExtraZipEntry(zipStream, entry, targetDir);
}
cb(true);
}
catch (Exception e)
{
//ReportManager.Inst.ReportException(e);
cb(false);
}
});
t.Start();
}
//第三部分
void ExtraZipEntry(ZipInputStream zipStream, ZipEntry entry, string outputDir)
{
int bufferSize_ = 8192;
byte[] buffer = new byte[bufferSize_];
int readSize_ = 0;
string directroyName = Path.GetDirectoryName(entry.Name);
string fileName = Path.GetFileName(entry.Name);
string absDirectory = Path.Combine(outputDir, directroyName);
string absFile = Path.Combine(absDirectory, fileName);
Debug.Log("解压信息:" + directroyName + " " + fileName + " " + absDirectory + " " + absFile);
if (!string.IsNullOrEmpty(directroyName))
Directory.CreateDirectory(absDirectory);
int size = (int)entry.Size;
using (FileStream fileStream = File.Create(absFile))
{
while (readSize_ < size)
{
int rd = zipStream.Read(buffer, 0, Math.Min(size - readSize_, bufferSize_));
fileStream.Write(buffer, 0, rd);
readSize_ += rd;
}
}
}