Iconv::IllegalSequence в имени файла, сохраненном wget (Ruby 1.9.2)

Я сохраняю файлы в раздел ext2 с помощью wget, используя обычные флаги. Получить их имена иногда не удается:

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 не возвращает такого файла или каталога.


person typist    schedule 22.07.2011    source источник
comment
Причина, вероятно, заключается в преднамеренной ошибке в wget -- bugs.debian.org /cgi-bin/bugreport.cgi?bug=411290.   -  person typist    schedule 23.07.2011


Ответы (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...
person Seamus Abshere    schedule 22.07.2011
comment
Сначала мне пришлось сделать a.force_encoding('us-ascii'), и это сработало. Спасибо. - person typist; 23.07.2011
comment
Извините, я был слишком поспешным. Это не то, как имя файла выглядит в bash. Я добавил это к моему вопросу. - person typist; 23.07.2011