首先需要下载SharpZipLib,下载地址:http://icsharpcode.github.io/SharpZipLib/
需要引入命名空间:
- using ICSharpCode.SharpZipLib.GZip;
- using System.IO;
- public static byte[] CompressGZip(byte[] rawData)
- {
- MemoryStream ms = new MemoryStream();
- GZipOutputStream compressedzipStream = new GZipOutputStream(ms);
- compressedzipStream.Write(rawData, 0, rawData.Length);
- compressedzipStream.Close();
- return ms.ToArray();
- }
- public static byte[] UnGZip(byte[] byteArray)
- {
- GZipInputStream gzi = new GZipInputStream(new MemoryStream(byteArray));
- MemoryStream re = new MemoryStream(50000);
- int count;
- byte[] data = new byte[50000];
- while ((count = gzi.Read(data, 0, data.Length)) != 0)
- {
- re.Write(data, 0, count);
- }
- byte[] overarr = re.ToArray();
- return overarr;
- }
- public static void GZipTest()
- {
- string testdata = "aaaa11233GZip压缩和解压";
- byte[] gzipdata = Tools.CompressGZip(Encoding.UTF8.GetBytes(testdata));
- byte[] undata = Tools.UnGZip(gzipdata);
- Debug.Log("[GZipTest] : data" + Encoding.UTF8.GetString(undata));
- }