javascriptstringencryptioncompression

Is there a way to compress a long string to a smaller one and vise versa by code?


I am working on a plugin for an existing web-based tool in JavaScript. We are collecting data and store it in a string like this: "2.545,3.552,8.568;2.553,9.898,6.542;..." and so on.

The problem is, that we reach an export limit of 64k characters too fast. I wanted to ask - and excuse me if it is a dumb question - if someone knows of an algorithm or method we could use to compress the string before we export it. I am sure that it is technically possible but it certainly exceeds my skills as a programmer.

Thanks for any tips, link or suggestion.


Solution

  • lz-string looks like it will work.

    var string = "2.545,3.552,8.568;2.553,9.898,6.542";
    alert("Size of sample is: " + string.length);
    var compressed = LZString.compress(string);
    alert("Size of compressed sample is: " + compressed.length);
    string = LZString.decompress(compressed);
    alert("Sample is: " + string);
    <script src="https://cdn.jsdelivr.net/gh/pieroxy/lz-string/libs/lz-string.js"></script>