Project

General

Profile

Bug #9790 ยป zlib-gzreader-each_file-9790.patch

jeremyevans0 (Jeremy Evans), 11/27/2019 03:35 PM

View differences:

ext/zlib/zlib.c
3723 3723
    return gzfile_s_open(argc, argv, klass, "rb");
3724 3724
}
3725 3725

  
3726
/*
3727
 * Document-method: Zlib::GzipReader.each_file
3728
 *
3729
 * call-seq:
3730
 *   Zlib::GzipReader.each_file(io, options = {}, &block) => nil
3731
 *
3732
 * Many gzip handling utilities support multiple concatenated files
3733
 * in a single .gz file. Zlib::GzipReader instances only support
3734
 * a single compressed file in .gz file, starting at the current
3735
 * position in the .gz file.  Zlib::GzipReader.each_file yields
3736
 * Zlib::GzipReader instances, one per file in the .gz file.
3737
 * Example:
3738
 *
3739
 *   Zlib::GzipReader.each_file(io) do |gz|
3740
 *     puts gz.read
3741
 *   end
3742
 */
3743
static VALUE
3744
rb_gzreader_s_each_file(int argc, VALUE *argv, VALUE klass)
3745
{
3746
    VALUE io, unused, obj;
3747
    long pos;
3748

  
3749
    rb_check_arity(argc, 1, 2);
3750
    io = argv[0];
3751

  
3752
    do {
3753
        obj = rb_funcallv(klass, rb_intern("new"), argc, argv);
3754
        rb_yield(obj);
3755

  
3756
        rb_gzreader_read(0, 0, obj);
3757
        pos = NUM2LONG(rb_funcall(io, rb_intern("pos"), 0));
3758
        unused = rb_gzreader_unused(obj);
3759
        rb_gzfile_finish(obj);
3760
        if (!NIL_P(unused)) {
3761
            pos -= NUM2LONG(rb_funcall(unused, rb_intern("length"), 0));
3762
            rb_funcall(io, rb_intern("pos="), 1, LONG2NUM(pos));
3763
        }
3764
    } while (pos < NUM2LONG(rb_funcall(io, rb_intern("size"), 0)));
3765

  
3766
    return Qnil;
3767
}
3768

  
3726 3769
/*
3727 3770
 * Document-method: Zlib::GzipReader.new
3728 3771
 *
......
4696 4739
    rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1);
4697 4740

  
4698 4741
    rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1);
4742
    rb_define_singleton_method(cGzipReader, "each_file", rb_gzreader_s_each_file, -1);
4699 4743
    rb_define_alloc_func(cGzipReader, rb_gzreader_s_allocate);
4700 4744
    rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1);
4701 4745
    rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0);
test/zlib/test_zlib.rb
446 446
  end
447 447

  
448 448
  class TestZlibGzipFile < Test::Unit::TestCase
449
    def test_gzip_reader_each_file
450
      Tempfile.create("test_zlib_gzip_file_to_io") {|t|
451
        gz = Zlib::GzipWriter.new(t)
452
        gz.print("foo")
453
        gz.close
454
        t = File.open(t.path, 'ab')
455
        gz = Zlib::GzipWriter.new(t)
456
        gz.print("bar")
457
        gz.close
458

  
459
        results = []
460
        t = File.open(t.path)
461
        Zlib::GzipReader.each_file(t) do |f|
462
          results << f.read
463
        end
464
        assert_equal(["foo", "bar"], results)
465
        t.close
466
      }
467
    end
468

  
449 469
    def test_to_io
450 470
      Tempfile.create("test_zlib_gzip_file_to_io") {|t|
451 471
        t.close
452
-