美文网首页
Unity3D使用压缩和解压算法的代码

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

作者: 小小酥XX | 来源:发表于2016-01-05 13:10 被阅读1588次
    
    
    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;
        }}
    
    

    相关文章

      网友评论

          本文标题:Unity3D使用压缩和解压算法的代码

          本文链接:https://www.haomeiwen.com/subject/gnkzhttx.html