使用Ionic.Zlib.Net对base64位字符串进行压缩与解压
需要从NuGet获取Zlib.Portable类库
using Ionic.Zlib;
using System;
using System.Text;
public static class ZlibHelper
{
/// <summary>
/// 字符串压缩
/// </summary>
/// <param name="source">64位编码字符串</param>
public static string CompressString(string source)
{
byte[] byteArray = Encoding.UTF8.GetBytes(source);
byte[] resultByteArray = ZlibStream.CompressBuffer(byteArray);
string result = Convert.ToBase64String(resultByteArray);
return result;
}
/// <summary>
/// 64位编码字符串解压
/// </summary>
/// <param name="source">解压字符串</param>
public static string UncompressString(string source)
{
byte[] byteArray = Convert.FromBase64String(source);
byte[] resultByteArray = ZlibStream.UncompressBuffer(byteArray);
string result = Encoding.UTF8.GetString(resultByteArray);
return result;
}
}