日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity_移动端安卓解压缩Zip

發布時間:2024/3/26 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity_移动端安卓解压缩Zip 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Unity_移動端安卓解壓縮Zip

歡迎使用Markdown編輯器

由于工作問題,需要開發unity安卓端解壓zip功能,經過兩天的研究簡單的例子如下:

工具:unity2018.3.13 ,ICSharpCode.SharpZipLib.dll;

在unity中放入ICSharpCode.SharpZipLib.dll這個動態鏈接庫,一般動態鏈接庫放在項目目錄中\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轉換為Stream,省去先保存到本地在解壓的過程zip1.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 = Application.persistentDataPath + "/" + ZipID;Debug.Log("ZipID"+ZipID);Debug.Log(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 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;} }

代碼已經完畢,那么我們說下問題:
首先 在pc端運行會發現下載及解壓縮zip功能是暢通無阻。

當發步安卓版本就會出現無法解壓問題

通過安卓ADB 真機調試發現錯誤問題: 確保已安裝正確的國際代碼集程序集并激活


解決問題方法:

總結

以上是生活随笔為你收集整理的Unity_移动端安卓解压缩Zip的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。