回答編集履歴
3
修正
answer
CHANGED
@@ -20,7 +20,7 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
unless entry.name.valid_encoding?
|
23
|
-
entry.name.force_encoding case NKF.guess
|
23
|
+
entry.name.force_encoding case NKF.guess(entry.name)
|
24
24
|
when NKF::EUC then 'euc-jp'
|
25
25
|
when NKF::SJIS then 'cp932'
|
26
26
|
when NKF::UTF8 then 'utf-8'
|
2
修正
answer
CHANGED
@@ -17,6 +17,8 @@
|
|
17
17
|
```rb
|
18
18
|
# NKFを用いた場合
|
19
19
|
require 'nkf'
|
20
|
+
|
21
|
+
|
20
22
|
unless entry.name.valid_encoding?
|
21
23
|
entry.name.force_encoding case NKF.guess
|
22
24
|
when NKF::EUC then 'euc-jp'
|
1
追記
answer
CHANGED
@@ -11,4 +11,25 @@
|
|
11
11
|
なので、unpack時にutf-8でない場合は自分で文字コードを推定する必要があります。
|
12
12
|
|
13
13
|
|
14
|
-
[`NKF.guess`](https://docs.ruby-lang.org/ja/latest/method/NKF/m/guess.html)や[charlock_holmes](https://github.com/brianmario/charlock_holmes)を用いる方法があります。
|
14
|
+
[`NKF.guess`](https://docs.ruby-lang.org/ja/latest/method/NKF/m/guess.html)や[charlock_holmes](https://github.com/brianmario/charlock_holmes)を用いる方法があります。
|
15
|
+
|
16
|
+
|
17
|
+
```rb
|
18
|
+
# NKFを用いた場合
|
19
|
+
require 'nkf'
|
20
|
+
unless entry.name.valid_encoding?
|
21
|
+
entry.name.force_encoding case NKF.guess
|
22
|
+
when NKF::EUC then 'euc-jp'
|
23
|
+
when NKF::SJIS then 'cp932'
|
24
|
+
when NKF::UTF8 then 'utf-8'
|
25
|
+
when NKF::UTF16 then 'utf-16'
|
26
|
+
when Encoding::EUCJP_MS then Encoding::EUCJP_MS
|
27
|
+
when Encoding::CP51932 then Encoding::CP51932
|
28
|
+
when Encoding::WINDOWS_31J then Encoding::WINDOWS_31J
|
29
|
+
else
|
30
|
+
# JISコードもしくは不明
|
31
|
+
raise "文字コードが想定外です"
|
32
|
+
end
|
33
|
+
entry.name.encode! 'utf-8'
|
34
|
+
end
|
35
|
+
```
|