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

孤街醉人 提交于 2020-01-24 06:27:54

问题


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


回答1:


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.)




回答2:


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;


来源:https://stackoverflow.com/questions/24512386/compress-decompress-string-on-net-server-that-was-encoded-with-lz-string-js-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!