Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript compression algorithm that supports BINARY data?

I'm looking for a lossless compression algorithm (like LZW or Huffman or anything) in javascript, that accepts and returns raw, binary data.

With 'binary data' I mean a sequence of bytes, which may come in any of the following forms:

  • a string containing characters with any value from 0 to 255
  • an array containing integers with any value from 0 to 255
  • a string containing a hexadecimal representation of the data (i.e. 2 hex digits per byte)
  • a string containing the base64 encoded representation of the data
  • or anything else that can be unambiguously converted from or to any of the above

Now obviously there are TONS of javascript implementations available everywhere, for a wide range of algorithms. However EVERYTHING I find seems to do crazy stuff like:

  • returning an array containing also values >255 (so what is the compression ratio now? how do I represent this in bytes, or how would I go about saving this to a file for example?)

  • messing with character encodings in strings, converting from/to unicode or url/html entities or whatnot (it's BINARY, character encoding does not apply here!)

  • return other representations that don't seem suitable for binary storage (i.e. cannot be converted to sequence of bytes)

Would anyone know of a good javascript compression (+decompression) implementation that suits my binary fetish?

like image 781
Sheldon Pinkman Avatar asked Mar 29 '12 10:03

Sheldon Pinkman


People also ask

Does JavaScript support binary data?

You can send JavaScript typed arrays as binary data as well. This is building a 512-byte array of 8-bit integers and sending it; you can use any binary data you'd like, of course.

Can binary data be compressed?

When you use COMPRESS=BINARY, patterns of multiple characters across the entire observation are compressed. Binary compression uses two techniques at the same time. This option searches for the following: Repeating byte sequences (for example, 10 blank spaces or 10 zero bytes in a row)

How does JavaScript handle binary data?

Binary data in JavaScript is implemented in a non-standard way, compared to other languages. But when we sort things out, everything becomes fairly simple. The basic binary object is ArrayBuffer – a reference to a fixed-length contiguous memory area.

What is compression in JavaScript?

The Compression Streams API provides a JavaScript API for compressing and decompressing streams of data using the gzip or deflate formats. Built in compression means that JavaScript applications will not need to include a compression library, which makes the download size of the application smaller.


1 Answers

I think I found what I was looking for after all: this deflate + inflate implementation in javascript seems to work with strings as byte sequences.

like image 55
Sheldon Pinkman Avatar answered Oct 06 '22 02:10

Sheldon Pinkman