Unity-ZipHelper类封装

74 篇文章 4 订阅

Reference DLL

ICSharpCode.SharpZipLib.dll
SharpZipLib Git samples, src都有

Usage

// 只能在Unity的 Coroutine内调用哦
StartCoroutine(_Coroutine());
private IEnumerator _Coroutine()
{
    Debug.Log("uncompress enter...");
    var zipFile = "x:/xxx/xxx.zip";
    var toFolder = "x:/xxx/xxx";
    var async = ZipHelper.UncompressToAsync(zipFile, toFolder, true);
    async.onProgress = context =>
    {
        Debug.Log($"uncompress progress percentage:{context.percentage.ToString("0.000")}");
    };
    async.onComplete = context =>
    {
        Debug.Log($"uncompress complete complete:{context.complete}");
    };
    async.onError = context => 
    {
        Debug.Log($"uncompress error :{context.error.ToString()}");
    };
    yield return async.Start();
    Debug.Log("uncompress exit...");
}

Code

//------------------------------------------------------------------------
// Craeted by Jave.Lin 5/18/2018 1:06:42 PM
//------------------------------------------------------------------------

using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.IO;
using UnityEngine;

namespace Assets.Scripts
{
    /// <summary>
    /// [des]
    /// author  :   Jave.Lin
    /// date    :   5/18/2018 1:06:42 PM
    /// </summary>
    public static class ZipHelper
    {
        public static void CompressTo(string folder, string toFile)
        {
            FastZip zip = new FastZip();
            zip.CreateZip(toFile, folder, true, "");
        }
        public static ZipUncompressAsync UncompressToAsync(string zipFile, string toFolder, bool createdFolder = true)
        {
            var ret = new ZipUncompressAsync(createdFolder);
            ret.context.zipFile = zipFile;
            ret.context.toFolder = toFolder;
            return ret;
        }
        public static bool ExtractZip(byte[] zipBytes, string toFolder)
        {
            try
            {
                byte[] writeBuffer = new byte[2048];
                using (MemoryStream ms = new MemoryStream(zipBytes))
                {
                    using (ZipInputStream zis = new ZipInputStream(ms))
                    {
                        ZipEntry entity = null;
                        while ((entity = zis.GetNextEntry()) != null)
                        {
                            if (!string.IsNullOrEmpty(entity.Name))
                            {
                                var fileName = Utils.PathCombine(toFolder, entity.Name);
                                var parent = Path.GetDirectoryName(fileName);
                                if (!Directory.Exists(parent)) Directory.CreateDirectory(parent);
                                using (var fs = File.Create(fileName))
                                {
                                    var size = 0;
                                    while ((size = zis.Read(writeBuffer, 0, writeBuffer.Length)) > 0)
                                    {
                                        fs.Write(writeBuffer, 0, size);
                                    }
                                }
                            }
                        }
                    }
                }
                Array.Resize(ref writeBuffer, 0);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
    public delegate void OnZipUncompressProgress(ZipUncompressContext context);
    public delegate void OnZipUncompressComplete(ZipUncompressContext context);
    public delegate void OnZipUncompressError(ZipUncompressContext context);
    public struct ZipUncompressContext
    {
        public string zipFile;
        public string toFolder;
        public float percentage;
        public bool done;
        public bool isError;
        public Exception error;
    }
    public class ZipUncompressAsync
    {
        public OnZipUncompressProgress onProgress;
        public OnZipUncompressComplete onComplete;
        public OnZipUncompressError onError;

        public ZipUncompressContext context;

        private bool createdFolder;
        public ZipUncompressAsync(bool createdFolder = true)
        {
            this.createdFolder = createdFolder;

            context = new ZipUncompressContext();
        }
        public IEnumerator Start()
        {
            if (createdFolder)
            {
                if (!Directory.Exists(context.toFolder))
                {
                    Directory.CreateDirectory(context.toFolder);
                }
            }
#if UNITY_ANDROID && !UNITY_EDITOR
            var saReader = Utils.GetMgr<SAReaderMgr>().LoadByURI(context.zipFile);
            yield return saReader.Start();

            if (saReader.pError != null)
            {
                context.done = true;
                context.error = saReader.pError;
                context.isError = true;
                onError?.Invoke(context);
                yield break;
            }

            //// 这种方式在android下会报错,改用:ZipHelper.ExtractZip,原因不明
            //using (var ms = new MemoryStream(saReader.pData))
            //{
            //    var events = new FastZipEvents();
            //    events.ProgressInterval = TimeSpan.FromSeconds(0.1f);
            //    events.Progress = (sender, e) =>
            //    {
            //        context.percentage = e.PercentComplete;
            //        onProgress?.Invoke(context);
            //    };
            //    events.CompletedFile = (sender, e) =>
            //    {
            //        events.Progress = null;
            //        events.CompletedFile = null;
            //        events.FileFailure = null;
            //        events.DirectoryFailure = null;
            //        context.done = true;
            //        onComplete?.Invoke(context);
            //    };
            //    events.FileFailure = (sender, e) =>
            //    {
            //        events.Progress = null;
            //        events.CompletedFile = null;
            //        events.FileFailure = null;
            //        events.DirectoryFailure = null;
            //        context.done = true;
            //        context.error = e.Exception;
            //        context.isError = true;
            //        onError?.Invoke(context);
            //    };
            //    events.DirectoryFailure = (sender, e) =>
            //    {
            //        events.Progress = null;
            //        events.CompletedFile = null;
            //        events.FileFailure = null;
            //        events.DirectoryFailure = null;
            //        context.done = true;
            //        context.error = e.Exception;
            //        context.isError = true;
            //        onError?.Invoke(context);
            //    };
            //    FastZip zip = new FastZip(events);
            //    zip.ExtractZip(ms, context.toFolder, FastZip.Overwrite.Always, (s) => false, "", "", false, false);
            //}

            ZipHelper.ExtractZip(saReader.pData, context.toFolder);
            context.done = true;
#else
            var events = new FastZipEvents();
            events.ProgressInterval = TimeSpan.FromSeconds(0.1f);
            events.Progress = (sender, e) =>
            {
                context.percentage = e.PercentComplete;
                onProgress?.Invoke(context);
            };
            events.CompletedFile = (sender, e) =>
            {
                events.Progress = null;
                events.CompletedFile = null;
                events.FileFailure = null;
                events.DirectoryFailure = null;
                context.done = true;
                onComplete?.Invoke(context);
            };
            events.FileFailure = (sender, e) =>
            {
                events.Progress = null;
                events.CompletedFile = null;
                events.FileFailure = null;
                events.DirectoryFailure = null;
                context.done = true;
                context.error = e.Exception;
                context.isError = true;
                onError?.Invoke(context);
            };
            events.DirectoryFailure = (sender, e) =>
            {
                events.Progress = null;
                events.CompletedFile = null;
                events.FileFailure = null;
                events.DirectoryFailure = null;
                context.done = true;
                context.error = e.Exception;
                context.isError = true;
                onError?.Invoke(context);
            };
            FastZip zip = new FastZip(events);
            zip.ExtractZip(context.zipFile, context.toFolder, "");
#endif
            while (!context.done)
            {
                yield return null;
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spine-Unity-3.8-2021是一个Spine动画的Unity插件,可以让开发人员在游戏开发过程中轻松实现Spine动画。这个插件包含了最新的Spine Runtime库,可以让开发人员在使用Spine动画时获得更好的性能和可靠性。此版本还提供了对C# 9的支持,并修复了在以前的Spine Unity版本中存在的一些问题和异常。此外,此版本也提供了一些改进和优化,能够让开发人员更快速、更容易地实现动画效果并提高游戏的运行效率。总的来说,Spine-Unity-3.8-2021是一个值得开发人员关注和使用的插件,它可以帮助游戏开发人员更好地实现动画效果,提升游戏的质量和用户体验。 ### 回答2: spine-unity-3.8-2021是一款在Unity中使用的Spine动画软件包。Spine是一种2D骨骼动画软件,它可以帮助制作2D游戏中的角色动画。与传统的逐帧动画相比,Spine骨骼动画具有更高的效率和更好的表现效果。 spine-unity-3.8-2021是Spine运行在Unity中的版本。它提供了一个简便的方式来将Spine制作的动画集成到Unity游戏中。spine-unity-3.8-2021拥有许多实用的功能,例如动画的播放、循环、暂停和停止等。 此外,spine-unity-3.8-2021还支持动画的混合、遮罩、缩放等高级特性。这些功能可以大大提升2D游戏的动画表现效果。 总之,spine-unity-3.8-2021是一款强大的Spine骨骼动画软件包,它可以帮助Unity开发者更方便、更高效地制作2D游戏中的动画效果。 ### 回答3: Spine-Unity-3.8-2021是Spine动画引擎的一个版本,其主要特点是可以与Unity引擎无缝集成,提供了高效、灵活、可定制的动画解决方案。此版本相比之前版本,主要增加了一些新功能和改进,如支持GPU动画混合、2D环境的自适应、高效的顶点色边框渲染、Spine Atlas纹理集加载、支持Mecanim(动画过渡和状态机和蒙太奇)和 IK姿势、环境光遮蔽、大量优化和 bug 修复等。除此之外,它还易于使用和实现,并具有快速迭代的能力,可以让开发者轻松创建精美的动画效果,提升游戏的用户体验。该版本是Spine引擎的主要升级版本之一,同时也体现出Spine开发团队对于产品需求和用户反馈的重视和努力,不仅提高了动画制作领域的生产力和创造力,也为游戏行业推陈出新提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值