1. 程式人生 > >unity byte[]的壓縮和解壓

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

}