欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Lua脚本的加密压缩和解压缩

程序员文章站 2024-03-17 13:47:28
...

项目中lua脚本的热更新、将lua代码库打包成zip进行上传、下载 统一管理所有lua脚本的热更新。
项目中的配置文件也可以相同的办法处理
这里借助第三方压缩dll,ICSharpCode.SharpZipLib.dll

压缩部分

   [MenuItem("Lua/打包lua脚本")]
    public static void PackLua()
    {
        string[] files = Directory.GetFiles(Application.dataPath + "/../code/Lua", "*.*", SearchOption.TopDirectoryOnly);
        string[] directories = Directory.GetDirectories(Application.dataPath + "/../code/Lua", "*.*", SearchOption.TopDirectoryOnly);
        string[] paths = new string[files.Length + directories.Length];
        directories.CopyTo(paths, 0);
        files.CopyTo(paths, directories.Length);

        ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(Application.dataPath + "/../../Bundle/lua.package"));
        //压缩等级
        zipOutputStream.SetLevel(9);
        zipOutputStream.Password = "aaa@qq.com#lua.package";

        for (int index = 0; index < paths.Length; ++index)
        {
            string fileOrDirectory = paths[index];
            if (Directory.Exists(fileOrDirectory))
                ZipDirectory(fileOrDirectory, string.Empty, zipOutputStream);
            else if (File.Exists(fileOrDirectory))
                ZipFile(fileOrDirectory, string.Empty, zipOutputStream);

            EditorUtility.DisplayProgressBar("正在压缩文件夹", index + "/" + paths.Length, (float)index / paths.Length);
        }

        EditorUtility.ClearProgressBar();
        zipOutputStream.Finish();
        zipOutputStream.Close();
    }
    /// <summary>
    /// 压缩文件
    /// </summary>
    private static bool ZipFile(string filePathName, string targetPath, ZipOutputStream zipOutputStream)
    {
        ZipEntry entry = null;
        FileStream fileStream = null;
        try
        {
            string entryName = null;
            if (string.IsNullOrEmpty(targetPath))
            {
                entryName = Path.GetFileName(filePathName);
            }
            else
            {
                entryName = targetPath + '/' + Path.GetFileName(filePathName);
            }

            entry = new ZipEntry(entryName);
            entry.DateTime = System.DateTime.Now;

            fileStream = File.OpenRead(filePathName);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, buffer.Length);
            fileStream.Close();

            entry.Size = buffer.Length;

            zipOutputStream.PutNextEntry(entry);
            zipOutputStream.Write(buffer, 0, buffer.Length);
        }
        catch (System.Exception _e)
        {
            Log.Error("[ZipUtility.ZipFile]: " + _e.ToString());
            return false;
        }
        finally
        {
            if (null != fileStream)
            {
                fileStream.Close();
                fileStream.Dispose();
            }
        }

        return true;
    }

    /// <summary>
    /// 压缩文件夹
    /// </summary>
    private static bool ZipDirectory(string path, string targetPath, ZipOutputStream zipOutputStream)
    {
        ZipEntry entry = null;
        try
        {
            string entryName = Path.Combine(targetPath, Path.GetFileName(path) + '/');
            entry = new ZipEntry(entryName);
            entry.DateTime = System.DateTime.Now;
            entry.Size = 0;

            zipOutputStream.PutNextEntry(entry);
            zipOutputStream.Flush();

            string[] files = Directory.GetFiles(path);
            for (int index = 0; index < files.Length; ++index)
                ZipFile(files[index], Path.Combine(targetPath, Path.GetFileName(path)), zipOutputStream);

        }
        catch (System.Exception _e)
        {
            Log.Error("[ZipUtility.ZipDirectory]: " + _e.ToString());
            return false;
        }

        string[] directories = Directory.GetDirectories(path);
        for (int index = 0; index < directories.Length; ++index)
        {
            if (!ZipDirectory(directories[index], Path.Combine(targetPath, Path.GetFileName(path)), zipOutputStream))
                return false;
        }

        return true;
    }

压缩前的文件大小
Lua脚本的加密压缩和解压缩

打包后的lua文件夹大小
Lua脚本的加密压缩和解压缩
读取压缩文件

[MenuItem("Lua/解压lua.package")]
    public static void UnZip()
    {
        float startTime = Time.realtimeSinceStartup;
        byte[] bytes = null;
        string fileName = null;
        string filePath = null;
        string writeFilePath = null;
        ZipEntry entry = null;
        FileStream fileStream = File.OpenRead(Application.dataPath + "/../../Bundle/lua.package");

        using (ZipInputStream zipInputStream = new ZipInputStream(fileStream))
        {
            zipInputStream.Password = "aaa@qq.com#lua.package";
            while ((entry = zipInputStream.GetNextEntry()) != null)
            {
                if (string.IsNullOrEmpty(entry.Name))
                    continue;

                try
                {
                    if (entry.IsFile)
                    {
                        fileName = entry.Name;
                        if (!fileName.EndsWith(".lua") && !fileName.EndsWith(".list"))
                            continue;

                        fileName = fileName.Replace("\\", "/");

                        bytes = new byte[entry.Size];
                        int count = zipInputStream.Read(bytes, 0, bytes.Length);

                        if (count > 0)
                        {
                            filePath = KPath.luaPath + "/" + Path.GetDirectoryName(fileName);
                            if (!Directory.Exists(filePath))
                            {
                                DirectoryInfo directory = Directory.CreateDirectory(filePath);
                            }

                            writeFilePath = KPath.luaPath + "/" + fileName;
                            FileStream stream = File.Create(writeFilePath);
                            stream.Write(bytes, 0, count);
                            stream.Close();
                            stream.Dispose();
                        }
                        else
                        {
                            Log.Error("error: 0 byte, name == " + fileName);
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                    continue;
                }
            }

            zipInputStream.Close();
            zipInputStream.Dispose();
        };
        float endTime = Time.realtimeSinceStartup;
        Debug.Log("解压缩时间: " + (endTime - startTime));
    }

解压耗时 单位秒
Lua脚本的加密压缩和解压缩

用途:将一些零碎的文件集中打包管理更新
第一次写帖子,有点小激动,如果有不对的地方,还望指出
大神有更好的思路和方法 记得分享哦

下篇文章,和大家讨论下,lua中如何比较优的读取配置文件,网上大多数的方法,是将配置文件转换成LuaTable进行加载,这种方式读取起来比较费时,下篇文章讨论下用c语言读取配置文件解析到_G表中 集成到tolua.dll中

参考链接:
https://blog.csdn.net/qq_39849535/article/details/104543576

相关标签: zip