分享

Unity3D使用压缩和解压算法的代码

 kiki的号 2017-04-05
using UnityEngine;using System.Collections;using ICSharpCode.SharpZipLib; using ICSharpCode.SharpZipLib.GZip;using System.IO; using System.Text; using System; public class RecodeAndSave:MonoBehaviour { void Start() { byte[] binary = Encoding.UTF8.GetBytes("你好,我是小小酥.很高兴为您服务"); Debug.Log("原始数据是"+Encoding.UTF8.GetString(binary)); byte[] press = Compress(binary); Debug.Log("压缩后的数据是"+Convert.ToBase64String(press)+"长度是"+press.Length); byte[] depress = DeCompress(press); Debug.Log("解压后的数据是"+Encoding.UTF8.GetString(depress)); } byte[] Compress(byte[] binary) { MemoryStream ms = new MemoryStream(); GZipOutputStream gzip = new GZipOutputStream(ms); gzip.Write(binary,0,binary.Length); gzip.Close(); byte[] press = ms.ToArray(); return press; } byte[] DeCompress(byte[] press) { GZipInputStream gzi = new GZipInputStream(new MemoryStream(press)); MemoryStream re = new MemoryStream(); int count = 0; byte[] data = new byte[4096]; while((count=gzi.Read(data,0,data.Length))!=0) { re.Write(data,0,count); } byte[] depress = re.ToArray(); return depress; }}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多