unity byte[]的压缩和解压

方案一:

脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Compression;
using System;

public class EnDeBytes : MonoBehaviour {

	// Use this for initialization
	void Start () {

		byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.");
	
		print ("-----------compress before----------");
		print (test.Length);
		byte[] sucess = CompressBytes (test);
		print ("----------compress after----------");
		print (sucess.Length);

		byte[] test2 = Decompress(sucess);
		print ("----------decompress after----------");
		print (test2.Length);

		string a = System.Text.Encoding.Default.GetString(test2, 0, test.Length);
		print ("a is :" + a);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public static byte[] CompressBytes(byte[] bytes)
	{
		using (MemoryStream compressStream = new MemoryStream())
		{
			using (var zipStream = new GZipStream(compressStream, CompressionMode.Compress))
				zipStream.Write(bytes, 0, bytes.Length);
			return compressStream.ToArray();
		}
	}

	public static byte[] Decompress(byte[] bytes)
	{
		using (var compressStream = new MemoryStream(bytes))
		{
			using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
			{
//解决后数据的最后四位表示数据的长度
				byte[] nb = { bytes[bytes.Length - 4], bytes[bytes.Length - 3], bytes[bytes.Length - 2], bytes[bytes.Length - 1] };
				int count = BitConverter.ToInt32(nb, 0);

				byte[] resultStream = new byte[count];
				zipStream.Read(resultStream, 0, count);

				return resultStream;
			}
		}
	}

}

运行

发现如果内容太短,压缩后还长了,所以小的东西没必需压缩。

把压缩的内容变长

byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.");
		

运行结果

 

方案二:A 引用 ICSharpCode.SharpZipLib.dll

	public static byte[] CompressByteToByte(byte[] inputBytes)
	{
		MemoryStream ms = new MemoryStream();
		Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms);
		try
		{
			stream.Write(inputBytes, 0, inputBytes.Length);
		}
		finally
		{
			stream.Close();
			ms.Close();
		}
		return ms.ToArray();
	}

	public static byte[] DecompressByteToByte(byte[] inputBytes)
	{
		MemoryStream ms = new MemoryStream(inputBytes);
		Stream sm = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(ms);
		byte[] data = new byte[sm.Length];
		int count = 0;
		MemoryStream re = new MemoryStream();
		while ((count = sm.Read(data, 0, data.Length)) != 0)
		{
			re.Write(data, 0, count);
		}
		sm.Close();
		ms.Close();
		return re.ToArray();
	}

B, 也引用 ICSharpCode.SharpZipLib.dll

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.IO.Compression;
using System;
using ICSharpCode.SharpZipLib.GZip;

public class EnDeBytes : MonoBehaviour {

	// Use this for initialization
	void Start () {

		//byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.");
		byte[] test = System.Text.Encoding.Default.GetBytes ("Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.Love lives on inside our hearts and always will.");
//		print ("-----------compress before----------");
//		print (test.Length);
//		byte[] sucess = CompressBytes (test);
//		print ("----------compress after----------");
//		print (sucess.Length);
//
//		byte[] test2 = Decompress(sucess);
//		print ("----------decompress after----------");
//		print (test2.Length);
//


		byte[] sucess = DecompressByteToBytes (CompressByteToBytes (test));
		string a = System.Text.Encoding.Default.GetString(sucess, 0, test.Length);
		print ("a is :" + a);


	}
	
	// Update is called once per frame
	void Update () {
		
	}

	public static byte[] CompressByteToBytes(byte[] inputBytes)
	{
		
		MemoryStream ms = new MemoryStream();
		GZipOutputStream gzip = new GZipOutputStream(ms);
	    gzip.Write(inputBytes, 0, inputBytes.Length);
		gzip.Close();
		byte[] press = ms.ToArray();

		return press;
	
	}

	public static byte[] DecompressByteToBytes(byte[] inputBytes)
	{
		GZipInputStream gzi = new GZipInputStream (new MemoryStream (inputBytes));
		MemoryStream re = new MemoryStream ();
		int count = 0;
		byte[] data = new byte[1024 * 2];
		while ((count = gzi.Read (data, 0, data.Length)) != 0) {
			re.Write (data, 0, count);
		}

		byte[] depress = re.ToArray ();
		return depress;
	}
	

}

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值