前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity网络交互☀️压缩包zip下载与解压

Unity网络交互☀️压缩包zip下载与解压

作者头像
星河造梦坊官方
发布2024-08-15 18:15:05
730
发布2024-08-15 18:15:05
举报
文章被收录于专栏:星河造梦坊专栏

🟥 注意事项

要注意PC和安卓区别:存储路径设置

🟧 PC版

可使用如下两种方法。

1️⃣ 将Zip保存到本地,解压本地Zip

推荐第3种方法。因为本种方法在PC成功,在安卓不能解压。

代码语言:javascript
复制
string downLoadPath=写入zip的路径.zip;
string unZipPath=要解压到的文件夹;

//下载结束写入本地
File.WriteAllBytes(downLoadPath, 下载的数据);

//解压本地zip
Skode_UnzipFile(downLoadPath, unZipPath, null);

    public static bool Skode_UnzipFile(string _filePathName, string _outputPath, string _password = null)
    {
        try
        {
            return Skode_UnzipFile(File.OpenRead(_filePathName), _outputPath, _password);
        }
        catch (Exception _e)
        {
            Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString());
            return false;
        }
    }


    public static bool Skode_UnzipFile(Stream _inputStream, string _outputPath, string _password = null)
    {
        // 创建文件目录
        if (!Directory.Exists(_outputPath))
            Directory.CreateDirectory(_outputPath);

        // 解压Zip包
        ZipEntry entry = null;
        Encoding gbk = Encoding.GetEncoding("gbk");
        ZipConstants.DefaultCodePage = gbk.CodePage;
        using (ZipInputStream zipInputStream = new ZipInputStream(_inputStream))
        {
            if (!string.IsNullOrEmpty(_password))
                zipInputStream.Password = _password;

            while (null != (entry = zipInputStream.GetNextEntry()))
            {
                if (null == entry || string.IsNullOrEmpty(entry.Name))
                    continue;

                string filePathName = Path.Combine(_outputPath, entry.Name);

                // 创建文件目录
                if (entry.IsDirectory)
                {
                    Directory.CreateDirectory(filePathName);
                    continue;
                }

                // 写入文件
                try
                {
                    Skode_EventHandler.instance.Skode_StartUnZip();

                    using (FileStream fileStream = File.Create(filePathName))
                    {
                        byte[] bytes = new byte[1024];
                        while (true)
                        {
                            int count = zipInputStream.Read(bytes, 0, bytes.Length);
                            if (count > 0)
                                fileStream.Write(bytes, 0, count);
                        }
                    }
                }
                catch (System.Exception _e)
                {
                    Debug.LogError("[ZipUtility.UnzipFile]: " + _e.ToString());
                    return false;
                }
            }
        }

        return true;
    }

2️⃣ 直接将下载的byte[]解压到指定路径

直接传入下载的数据,解压出来。即省去保存为zip这一步。

注意:要引入ICSharpCode.SharpZipLib.Zip dll文件。

代码语言:javascript
复制
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
 
public class SkodeUnPack: MonoBehaviour
{
    /// <summary> 
    /// 解压功能(下载后直接解压压缩文件到指定目录) 
    /// </summary> 
    /// <param name="unZipPath">解压目录</param> 
    /// <param name="ZipByte">网络下载的数据</param> 
    /// <param name="password">密码</param> 
    /// <returns>解压结果</returns> 
    public static bool SaveZip(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
                    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)
        {
            UnityEngine.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;
    }
}

🟨 安卓版

安卓版使用二——2方法。但需再引用几个dll文件,否则在安卓无法解压:

在Unity搜索 I18N ,将这几个文件复制到Unity:Asset/DLL文件夹即可。

大家还有什么问题,欢迎在下方留言!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 🟥 注意事项
  • 🟧 PC版
  • 🟨 安卓版
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档