Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ext/zlib/zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4591,7 +4591,10 @@ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
static VALUE
rb_gzreader_external_encoding(VALUE self)
{
return rb_enc_from_encoding(get_gzfile(self)->enc);
struct gzfile *gz = get_gzfile(self);
/* with a conversion set up, enc is the internal encoding and enc2 the
* external one (same convention as rb_io_extract_encoding_option) */
return rb_enc_from_encoding(gz->enc2 ? gz->enc2 : gz->enc);
}

static VALUE
Expand Down
9 changes: 9 additions & 0 deletions test/zlib/test_zlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,15 @@ def test_reader_wrap
}
end

def test_external_encoding_with_internal_encoding
pend "rb_io_extract_encoding_option ignores the encoding options on TruffleRuby" if RUBY_ENGINE == 'truffleruby'
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("caf\xE9\n".b)), external_encoding: "ISO-8859-1", internal_encoding: "UTF-8")
assert_equal(Encoding::ISO_8859_1, r.external_encoding)
assert_equal("caf\u00E9\n", r.gets)
r = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("caf\xE9\n".b)), external_encoding: "ISO-8859-1")
assert_equal(Encoding::ISO_8859_1, r.external_encoding)
end

def test_corrupted_header
gz = Zlib::GzipWriter.new(StringIO.new(s = "".dup))
gz.orig_name = "X"
Expand Down
Loading