diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/gzip.py | 9 | ||||
-rw-r--r-- | Lib/test/test_gzip.py | 12 |
2 files changed, 21 insertions, 0 deletions
diff --git a/Lib/gzip.py b/Lib/gzip.py index 26f435456b..13f2ca24b7 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -330,6 +330,15 @@ class GzipFile(io.BufferedIOBase): 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 == "\x00": + c = self.fileobj.read(1) + if c: + self.fileobj.seek(-1, 1) + @property def closed(self): return self.fileobj is None diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py index 60094dceb1..b6901343eb 100644 --- a/Lib/test/test_gzip.py +++ b/Lib/test/test_gzip.py @@ -252,6 +252,18 @@ class TestGzip(unittest.TestCase): 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) |