CodeToad
CodeToad

Reputation: 4724

Compress/decompress string on .NET server that was encoded with lz-string.js on client

I am using the LZString.compressToBase64 function of lz-string.js and need to decompress/compress the data on the server side.

The obvious solution seems to be lz_string_csharp but I am concerned about

this statement:

If you use just the regular Javascript 'compress' function then depending on the data in the string, it will not decompress correctly on the C# side.

However, if you are using the 'compress' function built into this C# version, then you should be ok to use the regular 'decompress' function included.

and about this reported issue: possible bug in c# version of compressToBase64

Upvotes: 4

Views: 4447

Answers (2)

CodeToad
CodeToad

Reputation: 4724

We fixed this by adding enc1 = enc2 = enc3 = enc4 = 0; between the two rows below (line 580 in original file before stringbuilder version)

From what I remember, the bug was caused by the values of enc1, enc2, etc... not being reset at the start of each loop, so sometimes a new iteration of the loop had wrong values from the previous round.

  i += 3;

                    enc1 = enc2 = enc3 = enc4 = 0;

                    enc1 = (int)(Math.Round(chr1)) >> 2;

Upvotes: 0

Richard
Richard

Reputation: 16772

The full description in the link you give says that you should be able to use 'compressToUTF16' and it will always work, rather than just 'compress', which won't always work.

I've tested it personally and seen that it works.

(Though I changed the Context_Compress_Data.str field from a string to a StringBuilder in the C# file, because it was too slow to run. After that it took only 8 seconds for an 8 MB JSON file and compressed to 7% of the original size.)

Upvotes: 1

Related Questions