Read binary file as string in Ruby

后端 未结 8 1618
无人共我
无人共我 2020-11-30 16:40

I need an easy way to take a tar file and convert it into a string (and vice versa). Is there a way to do this in Ruby? My best attempt was this:

file = File         


        
8条回答
  •  春和景丽
    2020-11-30 17:17

    You can probably encode the tar file in Base64. Base 64 will give you a pure ASCII representation of the file that you can store in a plain text file. Then you can retrieve the tar file by decoding the text back.

    You do something like:

    require 'base64'
    
    file_contents = Base64.encode64(tar_file_data)
    

    Have look at the Base64 Rubydocs to get a better idea.

提交回复
热议问题