Unity 用ICSharpCode.SharpZipLib 解压zip文件遇到的坑

Unity在编辑器模式下运行解压网络下载的zip压缩包都没有问题,打包出来以后解压就不产生作用,找了好久才解决

一、引用的库文件:I18N.CJK.dll,I18N.dll ,I18N.West.dll ,Mono.Data.Tds.dll,System.Data.dll这五个DLL 加入到Plugins文件夹中。

这五个库文件内容所在地址为:XXX\Unity\Editor\Data\Mono\lib\mono\2.0文件夹下

参考连接 https://answers.unity.com/questions/1440392/unity-net-46-framework-causing-sqlconnection-conne.html

参考连接 https://blog.csdn.net/xiaochenXIHUA/article/details/86573837

参考链接 https://blog.csdn.net/DoitPlayer/article/details/48731817?utm_source=blogxgwz4

代码

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

public class LoadScene : MonoBehaviour
{
    public Text LoadText;
    private bool IsLoad = false;
    private static string LoadStatus = "";
    void Start()
    {
        IsLoad = false;
        GetPath();
    }
    private static string LoadPath;
    public void GetPath()
    {
        LoadPath = Application.dataPath;
    }

    public void StartLoadData(string url)
    {
        StartCoroutine(Download("Work", url));
    }

    IEnumerator Download(string ZipID, string url)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();
        Debug.Log(request.isDone);
        while (!request.isDone)
        {
            LoadText.text = Math.Floor(request.downloadProgress * 100) + "%";
            yield return 1;
        }
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
        }
        else
        {
            LoadText.text = "";
            string dir = LoadPath;
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            yield return new WaitForEndOfFrame();

            //存储压缩包到本地
            // File.WriteAllBytes(LoadPath + "/Job" + "/PC_ExE.zip", request.downloadHandler.data);
            // 解压数据到本地
            SaveZip(ZipID, url, request.downloadHandler.data, null);
        }
    }
    /// <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 = LoadPath + "/" + ZipID;

        LoadStatus = 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      Android
                    fileName = fileName.Replace('\\', '/');

                    if (fileName.EndsWith("/"))
                    {
                        Directory.CreateDirectory(fileName);
                        continue;
                    }
                    #endregion
                    fs = 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);
                            fs.Write(data, 0, size);//解决读取不完整情况
                        }
                        else
                            break;
                    }
                }
            }
        }
        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;
    }

}
发布了112 篇原创文章 · 获赞 40 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/chh19941125/article/details/104984884
今日推荐