开发者

Reading filename in multiple OS without encoding problem with Ruby

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)

In Ubuntu I don't convert filename and get it in UTF-8. Maybe exists some method to determine OS filename encoding?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜