Unity_移动端安卓解压缩Zip

Unity_移动端安卓解压缩Zip

欢迎使用Markdown编辑器

由于工作问题,需要开发unity安卓端解压zip功能,经过两天的研究简单的例子如下:

工具:unity2018.3.13 ,ICSharpCode.SharpZipLib.dll;

在unity中放入ICSharpCode.SharpZipLib.dll这个动态链接库,一般动态链接库放在项目目录中\Assets\Plugins目录下

基本代码:

using UnityEngine;

using System.Collections;

using System;

using System.IO;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;
using UnityEditor;

public class zip1 : MonoBehaviour

{

    public string sourcesPath = "";

    public string targetPath = "";

    public string filename ="Untitled_13.zip";

    public string url = "http://xnwy-adm.91uutu.com/static/Untitled_13.zip";

   

  public  void button()

    {

        sourcesPath = Application.persistentDataPath + "/Zip";
        
        targetPath = Application.persistentDataPath + "/Resources";
        Debug.Log("sourcesPaht is:" + sourcesPath + "   " + targetPath);

        StartCoroutine(Wait_LoadDown(filename, url));

    }





    /// <summary>
    /// 下载
    /// </summary>
    /// <param name="ZipID" ZipID的名字,用于存储解压出的每一个Zip文件></param>
    /// <param name="url" Zip下载地址></param>
    /// <returns></returns>
    IEnumerator Wait_LoadDown(string ZipID, string url)
    {
        WWW www = new WWW(url);
        yield return www;
        if (www.isDone)
        {
            if (www.error == null)
            {
                Debug.Log("下载成功");
                string dir = Application.persistentDataPath;
                Debug.Log(dir);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                yield return new WaitForEndOfFrame();
                //直接使用 将byte转换为Stream,省去先保存到本地在解压的过程
                zip1.SaveZip(ZipID, url, www.bytes, null);

            }
            else
            {
                Debug.Log(www.error);
            }
        }

    }

    /// <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 = Application.persistentDataPath + "/" + ZipID;
        Debug.Log("ZipID"+ZipID);
        Debug.Log(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('\\', '/');
                    Debug.Log(fileName);
                    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);
                            Debug.Log(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;
    }
}

代码已经完毕,那么我们说下问题:
首先 在pc端运行会发现下载及解压缩zip功能是畅通无阻。

当发步安卓版本就会出现无法解压问题

通过安卓ADB 真机调试发现错误问题:
确保已安装正确的国际代码集程序集并**

Unity_移动端安卓解压缩Zip
解决问题方法:

Unity_移动端安卓解压缩Zip