Created
March 20, 2021 15:02
-
-
Save foobit/205c686a6638daef7d43afa21aa8f145 to your computer and use it in GitHub Desktop.
json -> gzip compress -> base64 -> data[] -> gzip decompress -> json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Text; | |
// read json | |
var file = @"somedata.json"; | |
using var inStream = File.OpenRead(file); | |
// compress | |
using var memStream = new MemoryStream(); | |
using var outStream = new GZipStream(memStream, CompressionMode.Compress); | |
inStream.CopyTo(outStream); | |
outStream.Flush(); | |
// convert to base64 | |
var data = memStream.ToArray(); | |
var base64 = Convert.ToBase64String(data); | |
var len = base64.Length; | |
// convert from base64 | |
var indata = Convert.FromBase64String(base64); | |
// decompress | |
using var compressedStream = new MemoryStream(indata); | |
using var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress); | |
using var resultStream = new MemoryStream(); | |
zipStream.CopyTo(resultStream); | |
zipStream.Flush(); | |
// conver to string | |
var outData = Encoding.UTF8.GetString(resultStream.ToArray()); | |
Console.WriteLine(outData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment