The first patch has been revised according to comments from Jani and Tomi on IRC. The next two are completely new. They pass the existing tests, but they should probably get a skeptical eye, particularly the implementation of gzreadline, since it's hard to know how well existing tests exercise that code.