1
0
mirror of synced 2024-11-24 06:50:15 +01:00
TaikoLocalServer/Application/Utils/GZipBytesUtil.cs
2024-11-24 02:23:25 +08:00

61 lines
1.7 KiB
C#

using System.Runtime.InteropServices;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;
namespace Application.Utils;
public static class GZipBytesUtil
{
public static MemoryStream GenerateStreamFromString(string value)
{
return new MemoryStream(Encoding.UTF8.GetBytes(value));
}
public static byte[] GetEmptyJsonGZipBytes()
{
var outputStream = new MemoryStream(1024);
using (var stream = new GZipOutputStream(outputStream))
using (var writer = new StreamWriter(stream, Encoding.UTF8))
{
/*writer.AutoFlush = true;
writer.WriteLine("{}");*/
stream.Write(Array.Empty<byte>());
}
return outputStream.ToArray();
}
public static byte[] GetGZipBytes(byte[] input)
{
var outputStream = new MemoryStream(1024);
using (var stream = new GZipOutputStream(outputStream))
{
stream.Write(input);
}
return outputStream.ToArray();
}
public static byte[] GetGZipBytes<T>(T[] data) where T : struct
{
var outputStream = new MemoryStream(1024);
using (var stream = new GZipOutputStream(outputStream))
{
var byteRef = MemoryMarshal.AsBytes(new ReadOnlySpan<T>(data));
stream.Write(byteRef);
}
return outputStream.ToArray();
}
public static byte[] DecompressGZipBytes(byte[] input)
{
using (var inputStream = new MemoryStream(input))
using (var stream = new GZipInputStream(inputStream))
using (var outputStream = new MemoryStream(1024))
{
stream.CopyTo(outputStream);
return outputStream.ToArray();
}
}
}