修改main
This commit is contained in:
@@ -2,6 +2,6 @@
|
||||
{
|
||||
public class WebRequestTools
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -11,33 +11,19 @@ namespace Stary.Evo
|
||||
public class ZipTool
|
||||
{
|
||||
// 新增:带进度回调的下载解压方法
|
||||
public static async UniTask<string[]> DownloadAndUnzipAsync(string url, string extractPath,
|
||||
public static async UniTask DownloadAndUnzipAsync(string fildId, string extractPath,
|
||||
Action<float> downloadProgress = null, Action<float> unzipProgress = null)
|
||||
{
|
||||
string tempPath = Path.Combine(Application.persistentDataPath, "temp.zip");
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 下载ZIP文件
|
||||
using( UnityWebRequest request =WebRequester(url))
|
||||
{
|
||||
request.downloadHandler = new DownloadHandlerFile(tempPath);
|
||||
var operation = request.SendWebRequest();
|
||||
|
||||
while (!operation.isDone)
|
||||
{
|
||||
downloadProgress?.Invoke(request.downloadProgress);
|
||||
await UniTask.Yield();
|
||||
}
|
||||
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
throw new Exception($"下载失败:{request.error}");
|
||||
}
|
||||
}
|
||||
string url = $"{AppConfig.IpConfig}/FileLoad/Download/{fildId}";
|
||||
await WebRequestSystem.GetFile(url, tempPath,downloadProgress);
|
||||
|
||||
// 解压下载的文件
|
||||
return await UnzipAsync(tempPath, extractPath, unzipProgress);
|
||||
await UnzipAsync(tempPath, extractPath, unzipProgress);
|
||||
File.Delete(tempPath);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -50,40 +36,37 @@ namespace Stary.Evo
|
||||
}
|
||||
|
||||
// 修改:添加进度回调参数
|
||||
public static async UniTask<string[]> UnzipAsync(string zipPath, string extractPath,
|
||||
public static async UniTask UnzipAsync(string zipPath, string extractPath,
|
||||
Action<float> progressCallback = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<string> pathList = new List<string>();
|
||||
|
||||
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
|
||||
{
|
||||
float totalEntries = archive.Entries.Count;
|
||||
float processed = 0;
|
||||
|
||||
|
||||
foreach (ZipArchiveEntry entry in archive.Entries)
|
||||
{
|
||||
string filePath = Path.Combine(extractPath, entry.FullName);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
||||
|
||||
|
||||
if (!string.IsNullOrEmpty(entry.Name))
|
||||
{
|
||||
using (Stream inputStream = entry.Open())
|
||||
using (FileStream outputStream = File.Create(filePath))
|
||||
{
|
||||
await inputStream.CopyToAsync(outputStream);
|
||||
pathList.Add(outputStream.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
processed++;
|
||||
float progress = processed / totalEntries;
|
||||
progressCallback?.Invoke(progress);
|
||||
Debug.Log($"解压进度: {progress:P0}");
|
||||
}
|
||||
}
|
||||
Debug.Log($"解压完成: {extractPath}");
|
||||
return pathList.ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -91,6 +74,7 @@ namespace Stary.Evo
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义网络请求器需要登录
|
||||
/// </summary>
|
||||
@@ -99,7 +83,7 @@ namespace Stary.Evo
|
||||
private static UnityWebRequest WebRequester(string url)
|
||||
{
|
||||
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
|
||||
var authorization = GetAuthorization(AppConfig.USERNAME, AppConfig.PASSWORD);
|
||||
var authorization = GetAuthorization(AppConfig.UserName, AppConfig.PassWord);
|
||||
request.SetRequestHeader("AUTHORIZATION", authorization);
|
||||
return request;
|
||||
}
|
||||
@@ -111,6 +95,4 @@ namespace Stary.Evo
|
||||
return "Basic " + System.Convert.ToBase64String(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user