• c#使用SharpZipLib对二进制数据进行压缩和解压


     

    首先需要下载SharpZipLib,下载地址:http://icsharpcode.github.io/SharpZipLib/

    需要引入命名空间:

    [csharp] view plain copy
     
    1. using ICSharpCode.SharpZipLib.GZip;  
    2. using System.IO;  
    压缩:
    [csharp] view plain copy
     
    1. public static byte[] CompressGZip(byte[] rawData)  
    2. {  
    3.     MemoryStream ms = new MemoryStream();  
    4.     GZipOutputStream compressedzipStream = new GZipOutputStream(ms);  
    5.     compressedzipStream.Write(rawData, 0, rawData.Length);  
    6.     compressedzipStream.Close();  
    7.     return ms.ToArray();  
    8. }  
    解压:
    [csharp] view plain copy
     
    1. public static byte[] UnGZip(byte[] byteArray)   
    2. {   
    3.     GZipInputStream gzi = new GZipInputStream(new MemoryStream(byteArray));  
    4.       
    5.     MemoryStream re = new MemoryStream(50000);  
    6.     int count;  
    7.     byte[] data = new byte[50000];  
    8.     while ((count = gzi.Read(data, 0, data.Length)) != 0)  
    9.     {  
    10.         re.Write(data, 0, count);  
    11.     }  
    12.     byte[] overarr = re.ToArray();  
    13.     return overarr;   
    14. }   
    测试:
    [csharp] view plain copy
     
    1. public static void GZipTest()  
    2.     {  
    3.         string testdata = "aaaa11233GZip压缩和解压";  
    4.   
    5.         byte[] gzipdata = Tools.CompressGZip(Encoding.UTF8.GetBytes(testdata));  
    6.         byte[] undata = Tools.UnGZip(gzipdata);  
    7.   
    8.         Debug.Log("[GZipTest]  : data" + Encoding.UTF8.GetString(undata));  
    9.     }  
    结果:

  • 相关阅读:
    游戏运营-三节课
    游戏运营-游戏付费
    游戏运营--资深
    django 导出csv
    django-分页
    django-中间件
    django-缓存
    django post 与get请求理解
    django 中遇到的问题
    Linux下使用XAMPP
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/9056464.html
Copyright © 2020-2023  润新知