Unity使用lzma的测试记录

来源:互联网 发布:手机cf国外视频软件 编辑:程序博客网 时间:2024/05/18 22:43
对资源文件的传输总会需要压缩以减小尺寸。unity支持lzma格式,其压缩效果也挺好。[下载地址: LZMA SDK ](http://www.7-zip.org/sdk.html)

    为了实现不同资源文件压缩成一个,首先需要记录每个资源文件的路径以及长度,作为头文件信息,将此头文件和对应资源文件(byte格式)一起写入memoryStream。

for (int i = 0; i < filePathCount; i++){    string assetPath = AssetDatabase.GetAssetPath(resourcesLis[i]);    string filePath = prePath + assetPath;    string zipBundlePath = assetPath.Replace("Assets/", "");    FileStream tempFileStream = File.Open(filePath, FileMode.Open);    //头文件信息设置 assets下路径 + 尺寸 + 回车分隔符    StringBuilder sb = new StringBuilder();     sb.Append(zipBundlePath).        Append(",").        Append(tempFileStream.Length).        Append("\n");    byte[] tempBuff = new byte[tempFileStream.Length];    byte[] header = Encoding.UTF8.GetBytes(sb.ToString());    //字节数组格式的文件数据    tempFileStream.Read(tempBuff, 0, (int)tempFileStream.Length);       // 写入临时内存流    memoryStream.Write(header, 0, header.Length);    memoryStream.Write(tempBuff, 0, tempBuff.Length);    tempFileStream.Close();}
之后进行lzma接口。调用需要先将组合文件的临时内存流的Position重置为0。
    SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();    encoder.WriteCoderProperties(compressStream);    CodeProgress codeProgress = new CodeProgress();       codeProgress.totalSize = memoryStream.Length;    // 压缩有回调,可以展示进度    encoder.Code(memoryStream, compressStream, memoryStream.Length, -1, codeProgress);
解压缩就是调逆的过程。先调用lzma的接口。
        FileStream compressStream = new FileStream(destFolderPath, FileMode.Open, FileAccess.Read);        MemoryStream tempStream = new MemoryStream();        SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();        byte[] properties = new byte[5];        compressStream.Read(properties, 0, 5);        decoder.SetDecoderProperties(properties);        long compressRealLen = compressStream.Length - compressStream.Position;        decoder.Code(compressStream, tempStream, compressRealLen, outsize, null);
解压后的文件集合,顺序读取每个文件的头文件信息, 获取解压路径以及每个文件尺寸。
    while (tempStream.Position != tempStream.Length)    {        int one = tempStream.ReadByte();        // 根据之前记录的 \n 来切割头文件数据        if (one != 10)         {            head[index++] = (byte)one;        }        else        {            index = 0;            string headStr = Encoding.UTF8.GetString(head);            head = new byte[1024];            string[] headArr = headStr.Split(',');            // assets 下路径名            string fileBundleName = headArr[0];             int lastXIndex = fileBundleName.LastIndexOf("/");            string fileName = fileBundleName.Substring(lastXIndex + 1, fileBundleName.Length - lastXIndex - 1);            string fileFolderName = fileBundleName.Substring(0, lastXIndex);            int fileSize = 0;            if(!int.TryParse(headArr[1], out fileSize))            {                EditorUtility.DisplayDialog("Error", "File size exception!!! -->> " + headArr[1], "ok");                break;            }            // 写入文件路径            string filePath = assetbundlePath + "/Decompress/" + fileFolderName;            if (!Directory.Exists(filePath))            {                Directory.CreateDirectory(filePath);            }            WriteToLocal(tempStream, fileSize, filePath + "/" + fileName);        }    }
我写时几个问题没考虑好:     1:临时memoryStream的写入后,其Position是否需要进行重置。    2:异常需要添加try catch, 否则文件写入报错会使unity目录出现一些奇怪的反应。以下是项目路径(其实一个脚本就搞定了ABTest.cs):    https://github.com/feixus/UnityLzmaTest
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信图片打印不清楚怎么办 淘宝上传商品视频不清楚怎么办 手机安装器没了怎么办 我不做直播换工作怎么办 天猫发票被投诉怎么办 天猫机顶盒闪退怎么办 苹果8红色掉漆怎么办 毛坯房验房房及厅试水时漏水怎么办 淘宝退货赠品被拆了怎么办 如果淘宝买家说赠品不好怎么办 淘宝顾客反应没赠品怎么办 淘宝上买东西赠品不给怎么办 天猫店关了质量有问题怎么办 蘑菇街开店被骗了怎么办 百视通网络机顶盒恢复出厂后怎么办 王牌电视出现无信号怎么办 联通电视串台了怎么办 电视上出现系统更新怎么办 电视开机一直在更新怎么办 云视听极光闪退怎么办 不小心打错电话怎么办 相亲发信息不回怎么办 如果一个人微信不回电话不接怎么办 跟老公吵架打电话不接怎么办 起诉离婚对方不接电话怎么办 苹果手机接电话声音小怎么办 老公不回你微信怎么办 工地欠货款不给怎么办 朋友微信借钱不还怎么办 微信上借钱不还怎么办 微信借钱不还怎么办 微信转账借钱不还怎么办 没欠条人家欠钱不给怎么办 欠货款被告没去怎么办 别人欠我钱不接电话怎么办 借给别人钱不接电话怎么办 欠钱的人玩消失怎么办 被婚介所骗了5万怎么办 贷款紧急联系人被骚扰怎么办 微信限制加好友怎么办 珍爱网的客服老打电话怎么办