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?