c#使用SharpZipLib对二进制数据进行压缩和解压
压缩:
[csharp] view plain copy
- 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();
- }
解压:
[csharp] view plain copy
- 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;
- }
测试:
[csharp] view plain copy
- 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));
- }
以上是 c#使用SharpZipLib对二进制数据进行压缩和解压 的全部内容, 来源链接: utcz.com/z/510688.html