Unity_移动端安卓解压缩Zip
生活随笔
收集整理的這篇文章主要介紹了
Unity_移动端安卓解压缩Zip
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Unity_移動端安卓解壓縮Zip
歡迎使用Markdown編輯器
由于工作問題,需要開發(fā)unity安卓端解壓zip功能,經(jīng)過兩天的研究簡單的例子如下:
工具:unity2018.3.13 ,ICSharpCode.SharpZipLib.dll;
在unity中放入ICSharpCode.SharpZipLib.dll這個動態(tài)鏈接庫,一般動態(tài)鏈接庫放在項目目錄中\(zhòng)Assets\Plugins目錄下
基本代碼:
using UnityEngine;using System.Collections;using System;using System.IO;using ICSharpCode.SharpZipLib.Checksums;using ICSharpCode.SharpZipLib.Zip; using UnityEditor;public class zip1 : MonoBehaviour{public string sourcesPath = "";public string targetPath = "";public string filename ="Untitled_13.zip";public string url = "http://xnwy-adm.91uutu.com/static/Untitled_13.zip";public void button(){sourcesPath = Application.persistentDataPath + "/Zip";targetPath = Application.persistentDataPath + "/Resources";Debug.Log("sourcesPaht is:" + sourcesPath + " " + targetPath);StartCoroutine(Wait_LoadDown(filename, url));}/// <summary>/// 下載/// </summary>/// <param name="ZipID" ZipID的名字,用于存儲解壓出的每一個Zip文件></param>/// <param name="url" Zip下載地址></param>/// <returns></returns>IEnumerator Wait_LoadDown(string ZipID, string url){WWW www = new WWW(url);yield return www;if (www.isDone){if (www.error == null){Debug.Log("下載成功");string dir = Application.persistentDataPath;Debug.Log(dir);if (!Directory.Exists(dir))Directory.CreateDirectory(dir);yield return new WaitForEndOfFrame();//直接使用 將byte轉(zhuǎn)換為Stream,省去先保存到本地在解壓的過程zip1.SaveZip(ZipID, url, www.bytes, null);}else{Debug.Log(www.error);}}}/// <summary> /// 解壓功能(下載后直接解壓壓縮文件到指定目錄) /// </summary> /// <param name="wwwStream">www下載轉(zhuǎn)換而來的Stream</param> /// <param name="zipedFolder">指定解壓目標(biāo)目錄(每一個Obj對應(yīng)一個Folder)</param> /// <param name="password">密碼</param> /// <returns>解壓結(jié)果</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 = Application.persistentDataPath + "/" + ZipID;Debug.Log("ZipID"+ZipID);Debug.Log(ZipID);if (!Directory.Exists(ZipID)){Directory.CreateDirectory(ZipID);}try{//直接使用 將byte轉(zhuǎn)換為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 AndroidfileName = fileName.Replace('\\', '/');Debug.Log(fileName);if (fileName.EndsWith("/")){Directory.CreateDirectory(fileName);continue;}#endregionfs = 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);Debug.Log(data.Length);fs.Write(data, 0, size);//解決讀取不完整情況}elsebreak;}}}}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;} }代碼已經(jīng)完畢,那么我們說下問題:
首先 在pc端運行會發(fā)現(xiàn)下載及解壓縮zip功能是暢通無阻。
當(dāng)發(fā)步安卓版本就會出現(xiàn)無法解壓問題
通過安卓ADB 真機調(diào)試發(fā)現(xiàn)錯誤問題: 確保已安裝正確的國際代碼集程序集并激活
解決問題方法:
總結(jié)
以上是生活随笔為你收集整理的Unity_移动端安卓解压缩Zip的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 南京印象之新街口地铁站
- 下一篇: DHCP Snooping讲解