Skip to content

CSV.new する時にGzipReaderを渡すとCSV::Parser::InvalidEncodingが出る場合がある #117

@namayaki

Description

@namayaki

バージョン

ruby 2.6.5

現象

以下のようにCSV.newする時にGZipReaderを渡すとCSV::Parser::InvalidEncodingが出てエラーになります。

code

gz = Zlib::GzipReader.new(File.open("file/to/path"))
CSV.new(gz).each_with_index do { } 

error

.rbenv/versions/2.6.5/lib/ruby/2.6.0/csv/parser.rb:314:in `rescue in parse': Invalid byte sequence in UTF-8 in line 78512. (CSV::MalformedCSVError)

考えられる要因

csv/parser.rb の202行目でinput.getsする時にchunksizeを指定していますが、この値によって、chunkが変なところで切られているように見えます。
それによって、utf8のバリデーションが失敗し、上記エラーが出ているように見えます。

ruby 2.5系では発生しません。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions