0

我正在使用普通标志使用 wget 将文件保存到 ext2 分区。检索他们的名字有时会失败:

s = `find "#{@dir}" -type f -printf "%T@\t::::\t%s\t::::\t%p\n" |sort`
s.each_line {|l|
   file_name = l.chomp.split("\t::::\t")[2] #=> 
   # ...66:in `split': invalid byte sequence in UTF-8 (ArgumentError)
}

测试:

l.encoding #=> UTF-8
l.valid_encoding #=> false
l.inspect #=> "...St. Paul\xE2%80%99s Cathedral..."
Iconv.conv('utf-8', 'utf-8', l) #=> 
# ...77:in `conv': "\xE2%80%99s Cathedr"... (Iconv::IllegalSequence)

如何获取文件名并删除文件?

忘了提一下,在 bash 中,文件看起来像:

index.php?attTag=St. Paul?%80%99s Cathedral

将此字符串粘贴回 ls 不会返回此类文件或目录。

4

1 回答 1

1

CGI.unescape您可以在运行转换之前尝试...

a = "...St. Paul\xE2%80%99s Cathedral..."
puts a

require 'cgi'
b = CGI.unescape a
puts b

require 'iconv'
c = Iconv.conv('UTF-8//TRANSLIT', 'UTF-8', b) # may not even be necessary
puts c

在我的 ruby​​-1.9.2-p180 上输出:

...St. Paul?%80%99s Cathedral...
...St. Paul’s Cathedral...
...St. Paul’s Cathedral...
于 2011-07-22T19:50:44.267 回答