I have some large data sets which I would like to compress before I send to my client. The compression works.
Utilizing this bit of code which turns my data into a nice, small base64String:
Example: string mytest = "This is some test text.";
public static string Compress(string mytest)
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
{
zip.Write(buffer, 0, buffer.Length);
}
ms.Position = 0;
MemoryStream outStream = new MemoryStream();
byte[] compressed = new byte[ms.Length];
ms.Read(compressed, 0, compressed.Length);
byte[] gzBuffer = new byte[compressed.Length + 4];
System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
return Convert.ToBase64String(gzBuffer);
}
On the client side, I need to walk this whole thing backwards.
I can convert the base64string back to a byte array using (library):
var byteArray = Base64Binary.decodeArrayBuffer(source);
Then using pako.js I can deflate the gzip compressed content:
var deflate = new pako.Deflate({ level: 1 });
deflate.push(uintArray, true);
if (deflate.err) { throw new Error(deflate.err); }
Finally, I should be able to convert this back to my text:
var encodedString = String.fromCharCode.apply(null, deflate.result)
var decodedString = decodeURIComponent(encodedString);
Problem is that while I get no errors, I don't get expected results, which should be the the original string - "This is some test text."
Output is like this (can't paste it all): xg``ïæ
Any thought on what am I missing?
You need to use pako.Inflate
in your frontend.
Additionally you need to remove the 4 bytes size you added to the front of the gzBuffer
in the frontend before decoding.
Something like this should work:
// "cookies rule the world" compressed with your c# code
let sample = "FgAAAB+LCAAAAAAABABLzs/PzkwtVigqzUlVKMlIVSjPL8pJAQBkkN7rFgAAAA==";
// decode base64 & convert to Uint8 Array
let binary = atob(sample);
let bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
// You appended the length at the start of gzBuffer, so you need to remove those bytes
bytes = bytes.slice(4);
// inflate the message & convert it to a string
let inflated = pako.inflate(bytes);
let message = String.fromCharCode.apply(null, inflated);
console.log(message);
<script src="https://raw.githubusercontent.com/danguer/blog-examples/master/js/base64-binary.js"></script>
<script src="https://unpkg.com/pako@1.0.10/dist/pako.min.js"></script>