Unicode filenames on Windows in Ruby

陌路散爱 提交于 2020-01-14 07:27:26

问题


I have a piece of code that looks like this:

Dir.new(path).each do |entry|
    puts entry
end

The problem comes when I have a file named こんにちは世界.txt in the directory that I list. On a Windows 7 machine I get the output:

???????.txt

From googling around, properly reading this filename on windows seems to be an impossible task. Any suggestions?


回答1:


You're out of luck with pure ruby (either 1.8 or 1.9.1) since it uses the ANSI versions of the Windows API.

It seems like Ruby 1.9.2 will support Unicode filenames on Windows. This bug report has 1.9.2 as target. According to this announcement Ruby 1.9.2 will be released at the end of July 2010.

If you really need it earlier you could try to use FindFirstFileW etc. directly via Win32API.new or win32-api.




回答2:


I had the same problem & just figured out how to get the entries of a directory in UTF-8 in Windows. The following worked for me (using Ruby 1.9.2p136):

opts = {}
opts[:encoding] = "UTF-8"
entries = Dir.entries(path, opts)
entries.each do |entry|
  # example
  stat = File::stat(entry)
  puts "Size: " + String(stat.size)
end



回答3:


My solution was to use Dir.glob instead of Dir.entries. But it only works with * parameter. It does not work when passing a path (c:/dir/*). Tested in 1.9.2p290 and 1.9.3p0 on Windows 7.

There are many other issues with unicode paths on Windows. It is still an open issue. The patches are currently targeted at Ruby 2.0, which is rumored to be released in 2013.



来源:https://stackoverflow.com/questions/2703283/unicode-filenames-on-windows-in-ruby

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