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;
	}
	

}

猜你喜欢

转载自blog.csdn.net/moonlightpeng/article/details/85141504
今日推荐