Reading filename in multiple OS without encoding problem with Ruby

后端 未结 1 562
挽巷
挽巷 2021-01-22 17:00

I want to get all filenames in UTF-8. For example, after I read filename in Windows, I do

filename = Iconv.iconv(\"UTF-8\", \"Windows-1251\", filename)


        
相关标签:
1条回答
  • 2021-01-22 17:47

    I do this on Ruby 1.9 when I want to make sure things are in UTF-8:

    if filename.encoding.to_s != 'UTF-8'
      filename.encode!('UTF-8')
    end
    

    Unless your OS gives a file name encoded with a coding system which doesn't support some special characters found on the file name, it can be encoded to UTF-8 without a hickup.

    0 讨论(0)
提交回复
热议问题