29 lines
830 B
C#
29 lines
830 B
C#
using System.IO.Compression;
|
|
using System.Text;
|
|
|
|
namespace ScrapperAPI.Utils;
|
|
|
|
public static class CompressionUtils
|
|
{
|
|
public static byte[] GzipCompressUtf8(string text, CompressionLevel level = CompressionLevel.Fastest)
|
|
{
|
|
var inputBytes = Encoding.UTF8.GetBytes(text);
|
|
|
|
using var output = new MemoryStream();
|
|
using (var gzip = new GZipStream(output, level, leaveOpen: true))
|
|
{
|
|
gzip.Write(inputBytes, 0, inputBytes.Length);
|
|
}
|
|
|
|
return output.ToArray();
|
|
}
|
|
|
|
public static string GzipDecompressUtf8(byte[] gzBytes)
|
|
{
|
|
using var input = new MemoryStream(gzBytes);
|
|
using var gzip = new GZipStream(input, CompressionMode.Decompress);
|
|
using var reader = new StreamReader(gzip, Encoding.UTF8);
|
|
|
|
return reader.ReadToEnd();
|
|
}
|
|
} |