public string Compress(string B64) { using MemoryStream Stream = new MemoryStream(Convert.FromBase64String(B64)); using MemoryStream Result = new MemoryStream(); using BrotliStream Compressor = new BrotliStream(Result, CompressionMode.Compress); Compressor.SetQuality(8); Stream.CopyTo(Compressor); Compressor.Flush(); var Rst = Convert.ToBase64String(Result.ToArray()); return(Rst); }
public static void CompressToBrotlin(string Filename) { var InputPath = Path.GetFileNameWithoutExtension(Filename).Combine(Path.GetDirectoryName(Filename)); using (Stream Input = File.Open(InputPath, FileMode.Open)) using (Stream Output = File.Create(Filename)) using (BrotliStream Compressor = new BrotliStream(Output, CompressionMode.Compress)) { Compressor.SetQuality(11); Input.CopyTo(Compressor); Compressor.Flush(); } }
private void BrotliCompress(string source, string destination) { using (var input = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var output = File.Create(destination)) using (var bs = new BrotliSharpLib.BrotliStream(output, CompressionMode.Compress)) { // By default, BrotliSharpLib uses a quality value of 1 and window size of 22 if the methods are not called. bs.SetQuality(BrotliCompressionQuality); /** bs.SetWindow(windowSize); **/ /** bs.SetCustomDictionary(customDict); **/ input.CopyTo(bs); /* IMPORTANT: Only use the destination stream after closing/disposing the BrotliStream * as the BrotliStream must be closed in order to signal that no more blocks are being written * for the final block to be flushed out */ bs.Dispose(); } }
public override void OnActionExecuted(ActionExecutedContext context) { if (context.Result is ViewResult viewResult) { string htmlToCache = viewResult.ToHtml(HttpContext); bool isMobile = HttpContext.IsMobileBrowser(); int statusCode = HttpContext.Response.StatusCode; if (statusCode == StatusCodes.Status200OK) { // do not cache dynamically calculated data - as real exam with random questions if (!HttpContext.Request.Path.ToString().Contains("test", StringComparison.InvariantCultureIgnoreCase)) { cache.Set <string>(HttpContext.Request.Path.ToString() + "_IsMobile_" + isMobile.ToString(), htmlToCache); // convert string to stream byte[] byteArray = Encoding.ASCII.GetBytes(htmlToCache); using (MemoryStream fs = new MemoryStream(byteArray)) using (var ms = new MemoryStream()) { using (var bs = new BrotliSharpLib.BrotliStream(ms, System.IO.Compression.CompressionMode.Compress)) { bs.SetQuality(11); fs.Position = 0; fs.CopyTo(bs); bs.Dispose(); byte[] compressed = ms.ToArray(); cache.Set <byte[]>(HttpContext.Request.Path.ToString() + "_IsMobile_" + isMobile.ToString() + "compressedBr", compressed); } } } } } base.OnActionExecuted(context); }