Zlib压缩与解压字符串

使用Ionic.Zlib.Net对base64位字符串进行压缩与解压

需要从NuGet获取Zlib.Portable类库

using Ionic.Zlib;
using System;
using System.Text;

public static class ZlibHelper
    {
        /// 
        /// 字符串压缩
        /// 
        /// 64位编码字符串
        public static string CompressString(string source)
        {
            byte[] byteArray = Encoding.UTF8.GetBytes(source);
            byte[] resultByteArray = ZlibStream.CompressBuffer(byteArray);
            string result = Convert.ToBase64String(resultByteArray);
            return result;
        }
        /// 
        /// 64位编码字符串解压
        /// 
        /// 解压字符串
        public static string UncompressString(string source)
        {
            byte[] byteArray = Convert.FromBase64String(source);
            byte[] resultByteArray = ZlibStream.UncompressBuffer(byteArray);
            string result = Encoding.UTF8.GetString(resultByteArray);
            return result;
        }
    }

 

你可能感兴趣的:(.net类库)