Index: Doc/library/gzip.rst =================================================================== --- Doc/library/gzip.rst (revision 77470) +++ Doc/library/gzip.rst (working copy) @@ -72,7 +72,10 @@ .. versionchanged:: 2.7 Support for the :keyword:`with` statement was added. + .. versionchanged:: 2.7 + Support for zero-padded files was added. + .. function:: open(filename[, mode[, compresslevel]]) This is a shorthand for ``GzipFile(filename,`` ``mode,`` ``compresslevel)``. Index: Lib/gzip.py =================================================================== --- Lib/gzip.py (revision 77470) +++ Lib/gzip.py (working copy) @@ -330,6 +330,15 @@ elif isize != (self.size & 0xffffffffL): raise IOError, "Incorrect length of data produced" + # Gzip files can be padded with zeroes and still have archives. + # Consume all zero bytes and set the file position to the first + # non-zero byte. See http://www.gzip.org/#faq8 + c = "\x00" + while c and c == "\x00": + c = self.fileobj.read(1) + if c: + self.fileobj.seek(-1, 1) + @property def closed(self): return self.fileobj is None Index: Lib/test/test_gzip.py =================================================================== --- Lib/test/test_gzip.py (revision 77470) +++ Lib/test/test_gzip.py (working copy) @@ -252,6 +252,18 @@ else: self.fail("1/0 didn't raise an exception") + def test_zero_padded_file(self): + with gzip.GzipFile(self.filename, "wb") as f: + f.write(data1 * 50) + + # Pad the file with zeroes + with open(self.filename, "ab") as f: + f.write("\x00" * 50) + + with gzip.GzipFile(self.filename, "rb") as f: + d = f.read() + self.assertEqual(d, data1 * 50, "Incorrect data in file") + def test_main(verbose=None): test_support.run_unittest(TestGzip) Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 77470) +++ Misc/NEWS (working copy) @@ -39,6 +39,8 @@ Extension extra options may change the output without changing the .c file). Initial patch by Collin Winter. +- Issue #2846: Add support for gzip.GzipFile reading zero-padded files. + Build -----