using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; namespace Stary.Evo { public class ZipTool { // 新增:带进度回调的下载解压方法 public static async UniTask DownloadAndUnzipAsync(string fildId, string extractPath, Action downloadProgress = null, Action unzipProgress = null) { string tempPath = Path.Combine(Application.persistentDataPath, "temp.zip"); try { string url = $"{AppConfig.IpConfig}/FileLoad/Download/{fildId}"; await WebRequestSystem.GetFile(url, tempPath,downloadProgress); // 解压下载的文件 await UnzipAsync(tempPath, extractPath, unzipProgress); File.Delete(tempPath); } finally { // 清理临时文件 if (File.Exists(tempPath)) { File.Delete(tempPath); } } } // 修改:添加进度回调参数 public static async UniTask UnzipAsync(string zipPath, string extractPath, Action progressCallback = null) { try { 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); } } processed++; float progress = processed / totalEntries; progressCallback?.Invoke(progress); } } Debug.Log($"解压完成: {extractPath}"); } catch (Exception ex) { Debug.LogError($"解压失败: {ex.Message}"); throw; } } /// /// 自定义网络请求器需要登录 /// /// /// private static UnityWebRequest WebRequester(string url) { var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET); var authorization = GetAuthorization(AppConfig.UserName, AppConfig.PassWord); request.SetRequestHeader("AUTHORIZATION", authorization); return request; } private static string GetAuthorization(string userName, string password) { string auth = userName + ":" + password; var bytes = System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth); return "Basic " + System.Convert.ToBase64String(bytes); } } }