Unity(十四) Android设备解压ZIP

PS: pc端很多方法都可以用,到安卓上却不行

云盘文件SharpZipLib(包含3.5   4.X)

链接:https://pan.baidu.com/s/11wIhsBPDmkEh3WDBVGfCYg 
提取码:ox7h 

需要使用哪个dll参见UnityPlayerSetting

如报错可把当前版本Unity 安装目录下的文件拷贝到工程中

路径 D:\Program Files\Unity2018.4.28\Editor\Data\Mono\lib\mono\unity

版本:Unity2018.4.28f1

DLL:   ICSharpCode.SharpZipLib(.NET 4.X)

Android和PC亲测可用

//解压ZIP
public static void UnpackFiles(string file/*zip路径*/, string dir/*保存路径*/)
    {
        try
        {
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            ZipInputStream s = new ZipInputStream(File.OpenRead(file));
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.FileName);
                if (directoryName != String.Empty)
                    Directory.CreateDirectory(dir + directoryName);

                if (fileName != String.Empty)
                {
                    FileStream streamWriter = File.Create(dir + theEntry.Name);
                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
        }
        catch (Exception)
        {
            throw;
        }
    }


//调用
//UnpackFiles("d:/Test/a.zip","d:/Test/")
    /// <summary>
    /// 解压ZIP(下载数据后直接解压)
    /// </summary>
    /// <param name="unZipPath">解压目录</param>
    /// <param name="ZipByte">字节</param>
    /// <param name="password">解压密码</param>
    /// <returns></returns>
    public static bool UnpakeZip(string unZipPath, byte[] ZipByte, string password =null)
    {
        bool result = true;
        FileStream fs = null;
        ZipInputStream zipStream = null;
        ZipEntry ent = null;
        string fileName;

        if (!Directory.Exists(unZipPath))
        {
            Directory.CreateDirectory(unZipPath);
        }
        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(unZipPath, ent.Name);

                    #region Android
                    if (Application.platform == RuntimePlatform.Android)
                    {
                        fileName = fileName.Replace('\\', '/');

                        if (fileName.EndsWith("/"))
                        {
                            Directory.CreateDirectory(fileName);
                            continue;
                        }
                    }
                    #endregion

                    fs = File.Create(fileName);

                    int size = 40960;
                    byte[] data = new byte[size];
                    while (true)
                    {
                        size = zipStream.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            fs.Write(data, 0, size);
                        }
                        else
                            break;
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError("Unpack ZIP error:"+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;
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值