Reduced the memory usage, as well as hopefully fixing the memory leak. Also implemented database compression across rows, including Brotli compression and bitshifting.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
|
||||
namespace Models.Helper;
|
||||
|
||||
@@ -19,4 +20,31 @@ public static class CompressionHelper
|
||||
using GZipStream decompressor = new(compressedFileStream, CompressionMode.Decompress);
|
||||
decompressor.CopyTo(decompressedFileStream);
|
||||
}
|
||||
|
||||
public static string CompressString(string text)
|
||||
{
|
||||
byte[] byteArray = Encoding.UTF8.GetBytes(text);
|
||||
|
||||
using MemoryStream memoryStream = new();
|
||||
|
||||
using (BrotliStream compressionStream = new(memoryStream, CompressionLevel.SmallestSize))
|
||||
{
|
||||
compressionStream.Write(byteArray, 0, byteArray.Length);
|
||||
}
|
||||
|
||||
return Convert.ToBase64String(memoryStream.ToArray());
|
||||
}
|
||||
|
||||
public static string DecompressString(string compressedText)
|
||||
{
|
||||
byte[] byteArray = Convert.FromBase64String(compressedText);
|
||||
|
||||
using MemoryStream memoryStream = new(byteArray);
|
||||
|
||||
using BrotliStream decompressionStream = new(memoryStream, CompressionMode.Decompress);
|
||||
|
||||
using StreamReader reader = new(decompressionStream);
|
||||
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user