# Extension of zlib

**URL:** <https://www.ruby-forum.com/t/extension-of-zlib/138152>\
**Category:** Ruby\
**Created:** [May 20, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/extension-of-zlib/138152 "2008-05-20T17:16:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [May 20, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/extension-of-zlib/138152/1 "2008-05-20T17:16:56Z")

</div>

Hi,

does anybody else think this (or something similar) would be a useful  
extension to zlib?

file zliba.rb:

require ‘zlib’

# Conveniently process a number of files that may

# be a mixture of gzip compressed and uncompressed

# files.

ARGFZ = Class.new do  
include Enumerable

def each(&b)  
::ARGV.each do |arg|  
if arg == “-”  
$stdin.each(&b)  
else  
io = ::Zlib::GzipReader.open(arg) rescue ::File.open(arg)  
begin  
io.each(&b)  
ensure  
io.close  
end  
end  
end  
self  
end

# addional methods for gets etc. could be added

# but seeking is too hard to be possible

end.new

Example usage, file catz.rb:

#!/bin/env ruby

require ‘zliba’ # this would be ‘zlib’ once this is integrated

ARGFZ.each do |line|  
puts line  
end

Then on shell prompt:

catz.rb foo foo.gz

Will print all lines from foo and then from foo.gz.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [May 20, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/extension-of-zlib/138152/2 "2008-05-20T18:19:47Z")

</div>

On May 20, 9:15 am, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # be a mixture of gzip compressed and uncompressed
> 
> ```
> io = ::Zlib::GzipReader.open(arg) rescue ::File.open(arg)
> 
> ```
> 
> # addional methods for gets etc. could be added
> 
> ARGFZ.each do |line|  
> puts line  
> end
> 
> Then on shell prompt:
> 
> catz.rb foo foo.gz
> 
> Will print all lines from foo and then from foo.gz.

This seems like something that belongs in a custom option handler, not  
in zlib directly, especially since I strongly suspect you’re going to  
break some option parsers with that code.

While we’re on the topic of zlib, see zliby:

[http://rubyforge.org/projects/zliby/](http://rubyforge.org/projects/zliby/)

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![robkuz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/34f0e0/32.png) [@robkuz](https://www.ruby-forum.com/u/robkuz)\
**Post date:** [May 20, 2008, 7:27pm UTC](https://www.ruby-forum.com/t/extension-of-zlib/138152/3 "2008-05-20T19:27:35Z")

</div>

On 20.05.2008 18:19, Daniel B. wrote:

> On May 20, 9:15 am, “Robert K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> > does anybody else think this (or something similar) would be a useful  
> > extension to zlib?

> > Will print all lines from foo and then from foo.gz.
> 
> This seems like something that belongs in a custom option handler, not  
> in zlib directly, especially since I strongly suspect you’re going to  
> break some option parsers with that code.

I don’t believe so. ARGFZ plays the same role as ARGF and your argument  
would apply there as well. But it’s not the case since option  
processing is typically done _before_ ARGF / ARGFZ are employed (see  
OptionParser documentation for examples).

> While we’re on the topic of zlib, see zliby:
> 
> [http://rubyforge.org/projects/zliby/](http://rubyforge.org/projects/zliby/)

I am not sure I get your point here. Can you elaborate?

Kind regards

robert
