[转]Unity-移动设备可用的压缩解压缩源码

原文: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

 

posted @ 2017-04-27 09:23  NIKEY_M  阅读(760)  评论(0编辑  收藏  举报