private void OnGUI() { if (GUI.Button(new Rect(100, 100, 100, 50), "测试压缩")) { List <string> _strList = new List <string>(strs); for (int i = 0; i < strs.Length; i++) { _strList[i] = Application.dataPath + "/from/" + strs[i]; //Debug.Log(_strList[i]); } ZipUtility.Zip(_strList.ToArray(), Application.dataPath + "/to/test.zip"); Debug.Log("测试压缩 OK,保存路径->" + Application.dataPath + "/to/test.zip"); } if (GUI.Button(new Rect(300, 100, 100, 50), "测试解压缩")) { ZipUtility.UnzipFile(Application.dataPath + "/to/test.zip", Application.dataPath + "/from"); Debug.Log("测试解压缩 OK,保存路径->" + Application.dataPath + "/from"); } }
// 解压脚本 IEnumerator UnZip_lua() { launcherUI.setTip(Launcher_str.tipTextUnzipLua); if (Directory.Exists(dst_lua_zip)) { DirectoryInfo dir = new DirectoryInfo(dst_lua_zip); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); foreach (FileSystemInfo i in fileinfo) { string aFirstName = i.Name.Substring(0, i.Name.LastIndexOf(".")); //文件名 ZipUtility.UnzipFile(i.FullName, dst_lua); yield return(new WaitForSeconds(0.01f)); } // 解压完毕,删除目录,避免累积 Directory.Delete(dst_lua_zip, true); } // 全部热更完成 加载主场景 TryEnterGeme(); // TODO:request_server_list }
//开始解压 public void StartUnZip() { ZipUtility.UnzipFile(zipPath, unZiPath, null, new MyUnZipCallback()); }
//这个是热更新打包系统支持的更新,由每一次新打包与上次打包的文件对比组成的 //这个热更新系统不好维护,仅支持一些资源文件的更新等 IEnumerator CheckNeedUpdate() { //仅在局域网下可用 if (Application.internetReachability == NetworkReachability.NotReachable) { //不可用 GameStart(); yield break; } else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork && GameStateMgr.gameStatus.OnlyWifi) { //3G-4G流量套餐 //别更新 GameStart(); yield break; } else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork || !GameStateMgr.gameStatus.OnlyWifi) { //Debug.LogError("download:" + string.Format(strVFile, strHost, Main.port, strProjectUrl, strPlatform, strVFileName)); UnityWebRequest vFile = new UnityWebRequest(); vFile.url = string.Format(strFile, strHost, Main.port, strProjectUrl, strVFileName); vFile.timeout = 5; DownloadHandlerBuffer dH = new DownloadHandlerBuffer(); vFile.downloadHandler = dH; yield return(vFile.Send()); if (vFile.isError || vFile.responseCode != 200) { Debug.LogError(string.Format("update version file:{0} error:{1} or responseCode:{2}", vFile.url, vFile.error, vFile.responseCode)); vFile.Dispose(); GameStart(); yield break; } if (vFile.downloadHandler.data != null && vFile.downloadedBytes != 0) { if (File.Exists(ResMng.GetResPath() + "/" + strVFileName)) { File.Delete(ResMng.GetResPath() + "/" + strVFileName); } if (File.Exists(ResMng.GetResPath() + "/" + strVerFile)) { File.Delete(ResMng.GetResPath() + "/" + strVerFile); } Debug.Log(ResMng.GetResPath() + "/" + strVFileName); File.WriteAllBytes(ResMng.GetResPath() + "/" + strVFileName, vFile.downloadHandler.data); ZipUtility.UnzipFile(ResMng.GetResPath() + "/" + strVFileName, ResMng.GetResPath() + "/"); vFile.Dispose(); } else { GameStart(); yield break; } List <VersionItem> v = ReadVersionJson(File.ReadAllText(ResMng.GetResPath() + "/" + strVerFile)); //从版本信息下载指定压缩包 for (int i = 0; i < v.Count; i++) { if (v[i].strVersion == AppInfo.AppVersion() && v[i].strVersionMax != v[i].strVersion) { UpdateHelper.ApplyVersion(v[i], this); yield break; } } GameStart(); yield break; } else { Log.WriteError("Application.internetReachability != NetworkReachability.ReachableViaLocalAreaNetwork"); GameStart(); yield break; } }
// 解压文件 IEnumerator Unpack() { launcherUI.setTip(tipTextUnzipAb); Directory.CreateDirectory(dst); Directory.CreateDirectory(dst_ab); Directory.CreateDirectory(dst_ab_zip); /* * 放入streamingAssets中的资源: * zip_ab 存放ab的文件夹 * zip_list.json * android资源没法遍历 * android需要先拷贝,再解压 */ if (Application.platform == RuntimePlatform.Android) { // 读取资源列表 string path = src + "/zip_list.json"; WWW www = new WWW(path); yield return(www); // 拷贝ab压缩资源到手机 bool success = true; Dictionary <string, object> resList = (Dictionary <string, object>)BestHTTP.JSON.Json.Decode(www.text.Trim(), ref success); int index = 0; int total = resList.Count; foreach (var item in resList) { index++; launcherUI.setProgress(total, index); WWW www_zip = new WWW(src + "/zip_ab/" + item.Key + ".zip"); yield return(www_zip); if (www_zip.isDone) { File.WriteAllBytes(dst_ab_zip + "/" + item.Key + ".zip", www_zip.bytes); } } launcherUI.setTip(tipTextCopy); // 解压资源 DirectoryInfo dir = new DirectoryInfo(dst_ab_zip); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 index = 0; total = fileinfo.Length; foreach (FileSystemInfo i in fileinfo) { string aFirstName = i.Name.Substring(0, i.Name.LastIndexOf(".")); //文件名 ZipUtility.UnzipFile(i.FullName, dst_ab + "/"); index++; launcherUI.setProgress(total, index); yield return(new WaitForSeconds(0.01f)); } } else if (Application.platform == RuntimePlatform.IPhonePlayer) { string path = Path.Combine(src, "zip_ab/"); DirectoryInfo dir = new DirectoryInfo(path); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 int index = 0; int total = fileinfo.Length; foreach (FileSystemInfo i in fileinfo) { string aFirstName = i.Name.Substring(0, i.Name.LastIndexOf(".")); //文件名 ZipUtility.UnzipFile(i.FullName, dst_ab + "/"); index++; launcherUI.setProgress(total, index); yield return(new WaitForSeconds(0.01f)); } } else { Debug.Log("Unpack is not supporting this platform"); } yield return(new WaitForSeconds(0.01f)); PlayerPrefs.SetString("local_ab_pack_ver", ab_pack_ver); // 删除拷贝到手机的压缩文件 if (Directory.Exists(dst_ab_zip)) { Directory.Delete(dst_ab_zip, true); } request_md5_file(); }
// 解压文件 IEnumerator Unpack() { launcherUI.setTip(Launcher_str.tipTextUnzipAb); /* * 放入streamingAssets中的资源: * zip_ab 存放ab的文件夹 * zip_list.json * android资源没法遍历 * android需要先拷贝,再解压 */ if (Application.platform == RuntimePlatform.Android) { // 读取资源列表 string path = src + "/zip_list.json"; UnityWebRequest request = UnityWebRequest.Get(path); yield return(request.SendWebRequest()); // 拷贝ab压缩资源到手机 bool success = true; Dictionary <string, object> resList = (Dictionary <string, object>)BestHTTP.JSON.Json.Decode(request.downloadHandler.text.Trim(), ref success); int index = 0; int total = resList.Count; foreach (var item in resList) { index++; launcherUI.setProgress(total, index, 1, true); var uri = new System.Uri(Path.Combine(src + "/zip_ab", item.Key + ".zip")); string zip_path = uri.AbsoluteUri; UnityWebRequest www_zip = UnityWebRequest.Get(zip_path); yield return(www_zip.SendWebRequest()); if (www_zip.isDone) { File.WriteAllBytes(dst_ab_zip + "/" + item.Key + ".zip", www_zip.downloadHandler.data); } } launcherUI.setTip(Launcher_str.tipTextCopy); // 解压资源 DirectoryInfo dir = new DirectoryInfo(dst_ab_zip); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 index = 0; total = fileinfo.Length; foreach (FileSystemInfo i in fileinfo) { string aFirstName = i.Name.Substring(0, i.Name.LastIndexOf(".")); //文件名 ZipUtility.UnzipFile(i.FullName, dst_ab + "/"); index++; launcherUI.setProgress(total, index, 1, true); yield return(new WaitForSeconds(0.01f)); } // var lua_uri = new System.Uri(Path.Combine(src + "/zip_lua", "src_encrypt.zip")); string lua_zip_path = lua_uri.AbsoluteUri; UnityWebRequest lua_www_zip = UnityWebRequest.Get(lua_zip_path); yield return(lua_www_zip.SendWebRequest()); if (lua_www_zip.isDone) { File.WriteAllBytes(dst_lua_zip + "/src_encrypt.zip", lua_www_zip.downloadHandler.data); ZipUtility.UnzipFile(dst_lua_zip + "/src_encrypt.zip", dst_lua); File.Delete(dst_lua_zip + "/src_encrypt.zip"); } } else if (Application.platform == RuntimePlatform.IPhonePlayer) { string path = Path.Combine(src, "zip_ab/"); DirectoryInfo dir = new DirectoryInfo(path); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录 int index = 0; int total = fileinfo.Length; foreach (FileSystemInfo i in fileinfo) { string aFirstName = i.Name.Substring(0, i.Name.LastIndexOf(".")); //文件名 ZipUtility.UnzipFile(i.FullName, dst_ab + "/"); index++; launcherUI.setProgress(total, index, 1, true); yield return(new WaitForSeconds(0.01f)); } } else { Debug.Log("Unpack is not supporting this platform"); } yield return(new WaitForSeconds(0.01f)); Debug.Log("ab_pack_ver = " + ab_pack_ver); PlayerPrefs.SetString("local_ab_pack_ver", ab_pack_ver); // 删除拷贝到手机的压缩文件 if (Directory.Exists(dst_ab_zip)) { Directory.Delete(dst_ab_zip, true); } request_md5_file(); }