Reputation: 51
Let, I have a string with 20 characters. Then, I need to compress it to a string with a length of 3 or 5 characters. Again, when I need the original string back, I should be able to decompress the compressed string and get the original string.
Is it possible to do? If yes, how to do it in REACT NATIVE? Can you please suggest me a good solution?
Thanks.
Upvotes: 3
Views: 3622
Reputation: 2069
Try Unishox String Compression library.
This library was developed specifically to individually compress and decompress short strings. In general, compression utilities such as zip
, gzip
do not compress short strings well and often expand them. So that was the reason to develop Unishox.
Sample code:
var usx = require("unishox2.js")
var my_str = "The quick brown fox jumped over the lazy dog";
var out_buf = new Uint8Array(100); // A buffer with arbitrary length
var out_len = usx.unishox2_compress_simple(my_str, my_str.length, out_buf);
var out_str = usx.unishox2_decompress_simple(out_buf, out_len);
console.log(out_str);
Unishox is available for C, Javascript and Python and found to be quite stable by users. See here for a comprehensive performance test by the person who implemented it in Python.
Disclaimer: I am the author of this library and the Unishox compression method.
Another similar method is Smaz but is less efficient and does not support Unicode.
Upvotes: 0
Reputation: 572
Try like this, may help you:
var lz = require('lz-string');
let before = lz.compress("bla bla bla")
let after = lz.decompress(before)
console.log(before, before.length);// ᆁ낆J鵀 4
console.log(after, after.length); // bla bla bla 11
Upvotes: 3