原文:http://www.manew.com/thread-103250-1-1.html
最近在做客戶端數據的分離,不希望對項目有什么影響,也不太想用AssetBundle,太麻煩,就在網上找了找開源的C#壓縮算法,找來找去,發現不是不支持移動端,就是不能直接壓縮文件夾,總之沒有一個滿意的方案,最后還是找了開源的ICSharpCode.SharpZipLib.Zip的源碼,調試了一下修了修,讓它支持了移動端,最終解決這個問題,本著開源的精神,發到這里,希望對大家有用
?
使用方法很簡單,里面最外層有一個Util_Zip腳本,直接調用就行
?
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using UnityEngine; 8 9 public class Util_DownloadZip : MonoBehaviour 10 { 11 public void DownloadTo(string url, string targetDirectory, Action<string> failedDel, Action completeDel) 12 { 13 StartCoroutine(DownloadURL(url, targetDirectory, failedDel, completeDel)); 14 } 15 private IEnumerator DownloadURL(string url, string targetDirectory, Action<string> failedDel, Action completeDel) 16 { 17 WWW www = new WWW(url); 18 yield return www; 19 if (www.error == null) 20 { 21 try 22 { 23 Util_Zip.ExtractZip(new MemoryStream(www.bytes), targetDirectory); 24 if (completeDel != null) 25 { 26 completeDel(); 27 } 28 } 29 catch (Exception ex) 30 { 31 if (failedDel != null) 32 { 33 failedDel("Util_Zip.ExtractZip error:" + ex.Message); 34 } 35 } 36 } 37 else 38 { 39 if (failedDel != null) 40 { 41 failedDel(www.error + "\r\n" + url); 42 } 43 } 44 } 45 }
?
調用的時候第一個參數傳壓縮包地址,第二個參數傳解壓縮的文件夾,我是傳的Application.persistentDataPath,這個目錄安卓iOS都可讀寫,第三四個參數是成功和失敗的回調,可空。
壓縮的接口直接傳兩個路徑進去,就不用說了吧
Enjoy~
?
鏈接: https://pan.baidu.com/s/1jI9NRj4 密碼: 6n3c
?