2

Please can anyone point me in the right direction for compressing a (quite long) string of text in C# and then decompress it in JavaScript?

Basically I'm encoding an image to base64 and then sending it via a websocket to JavaScript, but want to be able to reduce the size of the string before sending it.
I know of ways it can be done from C# to C#, like using gzip or quicklz, but haven't found anything for C# to JavaScript. Would appreciate any info you have. :) Thanks.

Joey Morani
  • 25,431
  • 32
  • 84
  • 131
  • Wouldn't it take more bandwidth to write a javascript program to decompress the input than to just send the input as uncompressed ascii? – J V Aug 21 '12 at 14:49
  • 2
    Out of interest, any reason not to return it as a binary image with content- or transfer-encoding? – Basic Aug 21 '12 at 14:50
  • 1
    Just use the HTTP gzip compression, it will do the work for you. – Bergi Aug 21 '12 at 14:51
  • Don't know if this will be able to uncompress the string compressed by C#, but there is an Open source JS lib called [JSZip](http://stuartk.com/jszip/) – Scorpion-Prince Aug 21 '12 at 14:52
  • My first choice was to send the image bytes instead of converting it to a base64 string, but I lack to know-how to do it. In fact, my previous question was asking how - so if anyone knows, I would appreciate a answer on it :) http://stackoverflow.com/questions/12041851/converting-a-bytes-to-an-image-for-drawing-on-a-html5-canvas – Joey Morani Aug 21 '12 at 15:59

2 Answers2

2

Basically I'm encoding an image to base64 and then sending it via a websocket to JavaScript, but want to be able to reduce the size of the string before sending it.

Images are almost always compressed anyway, so I wouldn't expect you to be able to get any benefit there. It strikes me that the main thing you could try to do is remove the "text" part here, avoiding converting to/from base64 in the first place.

I don't have any experience with websockets myself, but I would investigate transferring binary data that way instead of text.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

There seems to be a library for that, but I haven't tried it though: zip.js

victorvartan
  • 1,002
  • 2
  • 11
  • 31