您的位置:首页 > 移动开发 > Unity3D

移动设备可用的压缩解压缩源码

2017-12-15 15:55 183 查看
最近在做客户端数据的分离,不希望对项目有什么影响,也不太想用AssetBundle,太麻烦,就在网上找了找开源的C#压缩算法,找来找去,发现不是不支持移动端,就是不能直接压缩文件夹,总之没有一个满意的方案,最后还是找了开源的ICSharpCode.SharpZipLib.Zip的源码,调试了一下修了修,让它支持了移动端,最终解决这个问题,本着开源的精神,发到这里,希望对大家有用。


 zip
src.zip (271.36 KB, 下载次数: 105) 

使用方法很简单,里面最外层有一个Util_Zip脚本,直接调用就行

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
 
public class Util_DownloadZip : MonoBehaviour
{
        public void DownloadTo(string url, string targetDirectory, Action<string> failedDel, Action completeDel)
        {
                StartCoroutine(DownloadURL(url, targetDirectory, failedDel, completeDel));
        }
        private IEnumerator DownloadURL(string url, string targetDirectory, Action<string> failedDel, Action completeDel)
        {
                WWW www = new WWW(url);
                yield return www;
                if (www.error == null)
                {
                        try
                        {
                                Util_Zip.ExtractZip(new MemoryStream(www.bytes), targetDirectory);
                                if (completeDel != null)
                                {
                                        completeDel();
                                }
                        }
                        catch (Exception ex)
                        {
                                if (failedDel != null)
                                {
                                        failedDel("Util_Zip.ExtractZip error:" + ex.Message);
                                }
                        }
                }
                else
                {
                        if (failedDel != null)
                        {
                                failedDel(www.error + "\r\n" + url);
                        }
                }
        }
}

调用的时候第一个参数传压缩包地址,第二个参数传解压缩的文件夹,我是传的Application.persistentDataPath,这个目录安卓iOS都可读写,第三四个参数是成功和失败的回调,可空。

压缩的接口直接传两个路径进去,就不用说了吧

Enjoy~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息