Compressing large string in ruby

后端 未结 1 512
耶瑟儿~
耶瑟儿~ 2021-01-31 09:37

I have a web application(ruby on rails) that sends some YAML as the value of a hidden input field.

Now I want to reduce the size of the text that is sent across to the

相关标签:
1条回答
  • 2021-01-31 10:19

    You could use the zlib implementation in the ruby core to in/de-flate data:

    require "zlib"
    data = "some long yaml string" * 100
    compressed_data = Zlib::Deflate.deflate(data)
    #=> "x\x9C+\xCE\xCFMU\xC8\xC9\xCFKW\xA8L\xCC\xCDQ(.)\xCA\xCCK/\x1E\x15\x1C\x15\x1C\x15\x1C\x15\x1C\x15\x1C\x15\x1C\x15\x1C\x15D\x15\x04\x00\xB3G%\xA6"
    

    You should base64-encode the compressed data to make it printable:

    require 'base64'
    encoded_data = Base64.encode64 compressed_data
    #=> "eJwrzs9NVcjJz0tXqEzMzVEoLinKzEsvHhUcFRwVHBUcFRwVHBUcFUQVBACz\nRyWm\n"
    

    Later, on the client-side, you might use pako (a zlib port to javascript) to get your data back. This answer probably helps you with implementing the JS part.

    To give you an idea on how effective this is, here are the sizes of the example strings:

    data.size            # 2100
    compressed_data.size #   48
    encoded_data.size    #   66
    

    Same thing goes vice-versa when compressing on the client and inflating on the server.

    Zlib::Inflate.inflate(Base64.decode64(encoded_data))
    #=> "some long yaml stringsome long yaml str ... (shortened, as the string is long :)
    

    Disclaimer:

    • The ruby zlib implementation should be compatible with the pako implementation. But I have not tried it.
    • The numbers about string sizes are a little cheated. Zlib is really effective here, because the string repeats a lot. Real life data usually does not repeat as much.
    0 讨论(0)
提交回复
热议问题