UnityWWW下載壓縮包並解壓到桌面資料夾中
阿新 • • 發佈:2018-12-27
https://download.csdn.net/download/qq_30928175/10877644工程下載
using ICSharpCode.SharpZipLib.Zip; using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; public class ZipText : MonoBehaviour { public Text text; private string url = "https://www.boxonline.com.cn/xiaopeng/321.zip"; private bool xiazaichenggong = false; private static string xiazai = ""; // Use this for initialization void Start() { xiazaichenggong = false; zhuomianlujing(); StartCoroutine(Wait_LoadDown("UnityWWW下載壓縮包並壓縮到桌面", url)); } // Update is called once per frame void Update() { if (xiazaichenggong) { if (xiazai == "") { text.text = "正在下載資源"; } else { text.text = "下載完成"; xiazaichenggong = false; } } } #region 獲取桌面路徑 //桌面路徑 private static string desktopPath; //獲取桌面路徑 private void zhuomianlujing() { RegistryKey folders; folders = OpenRegistryPath(Registry.CurrentUser, @"/software/microsoft/windows/currentversion/explorer/shell folders"); // Windows使用者桌面路徑 desktopPath = folders.GetValue("Desktop").ToString(); } private RegistryKey OpenRegistryPath(RegistryKey root, string s) { s = s.Remove(0, 1) + @"/"; while (s.IndexOf(@"/") != -1) { root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/"))); s = s.Remove(0, s.IndexOf(@"/") + 1); } return root; } #endregion /// <summary> /// 下載壓縮包 /// </summary> /// <param name="ZipID"></param> /// <param name="url"></param> /// <returns></returns> IEnumerator Wait_LoadDown(string ZipID, string url) { xiazaichenggong = true; WWW www = new WWW(url); yield return www; while (!www.isDone) { Debug.Log("正在下載"); } if (www.isDone) { if (www.error == null) { string dir = desktopPath; //Debug.Log(dir); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); yield return new WaitForEndOfFrame(); //直接使用 將byte轉換為Stream,省去先儲存到本地在解壓的過程 SaveZip(ZipID, url, www.bytes, null); } else { //Debug.Log(www.error); } } } /// <summary> /// 解壓功能(下載後直接解壓壓縮檔案到指定目錄) /// </summary> /// <param name="wwwStream">www下載轉換而來的Stream</param> /// <param name="zipedFolder">指定解壓目標目錄(每一個Obj對應一個Folder)</param> /// <param name="password">密碼</param> /// <returns>解壓結果</returns> public static bool SaveZip(string ZipID, string url, byte[] ZipByte, string password) { bool result = true; FileStream fs = null; ZipInputStream zipStream = null; ZipEntry ent = null; string fileName; ZipID = desktopPath + "/" + ZipID; xiazai = ZipID; if (!Directory.Exists(ZipID)) { Directory.CreateDirectory(ZipID); } try { //直接使用 將byte轉換為Stream,省去先儲存到本地在解壓的過程 Stream stream = new MemoryStream(ZipByte); zipStream = new ZipInputStream(stream); if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } while ((ent = zipStream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(ent.Name)) { fileName = Path.Combine(ZipID, ent.Name); #region Android fileName = fileName.Replace('\\', '/'); if (fileName.EndsWith("/")) { Directory.CreateDirectory(fileName); continue; } #endregion fs = File.Create(fileName); int size = 2048; byte[] data = new byte[size]; while (true) { size = zipStream.Read(data, 0, data.Length); if (size > 0) { //fs.Write(data, 0, data.Length); fs.Write(data, 0, size);//解決讀取不完整情況 } else break; } } } } catch (Exception e) { Debug.Log(e.ToString()); result = false; } finally { if (fs != null) { fs.Close(); fs.Dispose(); } if (zipStream != null) { zipStream.Close(); zipStream.Dispose(); } if (ent != null) { ent = null; } GC.Collect(); GC.Collect(1); } return result; }