ソースコード
using System.IO;
using System.IO.Compression;
using System.Text;
namespace MyProject
{
<summary>
</summary>
public static class BrotliCompressor
{
private static readonly Encoding ENCODING = Encoding.UTF8;
<summary>
</summary>
public static byte[] Compress( string rawString )
{
var bytes = ENCODING.GetBytes( rawString );
using var memoryStream = new MemoryStream();
using ( var gZipStream = new BrotliStream( memoryStream, CompressionMode.Compress ) )
{
gZipStream.Write( bytes, 0, bytes.Length );
}
return memoryStream.ToArray();
}
<summary>
</summary>
public static string Decompress( byte[] bytes )
{
using var memoryStream = new MemoryStream( bytes );
using var gZipStream = new BrotliStream( memoryStream, CompressionMode.Decompress );
using var streamReader = new StreamReader( gZipStream, ENCODING );
return streamReader.ReadToEnd();
}
}
}