File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -72,6 +72,9 @@ The module defines the following items:
7272 .. versionchanged :: 3.1
7373 Support for the :keyword: `with ` statement was added.
7474
75+ .. versionchanged :: 3.2
76+ Support for zero-padded files was added.
77+
7578
7679.. function :: open(filename, mode='rb', compresslevel=9)
7780
Original file line number Diff line number Diff line change @@ -348,6 +348,15 @@ def _read_eof(self):
348348 elif isize != (self .size & 0xffffffff ):
349349 raise IOError ("Incorrect length of data produced" )
350350
351+ # Gzip files can be padded with zeroes and still have archives.
352+ # Consume all zero bytes and set the file position to the first
353+ # non-zero byte. See http://www.gzip.org/#faq8
354+ c = b"\x00 "
355+ while c == b"\x00 " :
356+ c = self .fileobj .read (1 )
357+ if c :
358+ self .fileobj .seek (- 1 , 1 )
359+
351360 @property
352361 def closed (self ):
353362 return self .fileobj is None
Original file line number Diff line number Diff line change @@ -253,6 +253,18 @@ def test_with_open(self):
253253 else :
254254 self .fail ("1/0 didn't raise an exception" )
255255
256+ def test_zero_padded_file (self ):
257+ with gzip .GzipFile (self .filename , "wb" ) as f :
258+ f .write (data1 * 50 )
259+
260+ # Pad the file with zeroes
261+ with open (self .filename , "ab" ) as f :
262+ f .write (b"\x00 " * 50 )
263+
264+ with gzip .GzipFile (self .filename , "rb" ) as f :
265+ d = f .read ()
266+ self .assertEqual (d , data1 * 50 , "Incorrect data in file" )
267+
256268def test_main (verbose = None ):
257269 support .run_unittest (TestGzip )
258270
Original file line number Diff line number Diff line change @@ -164,6 +164,7 @@ Simon Cross
164164Drew Csillag
165165John Cugini
166166Tom Culliton
167+ Brian Curtin
167168Lisandro Dalcin
168169Andrew Dalke
169170Lars Damerow
Original file line number Diff line number Diff line change @@ -213,6 +213,9 @@ C-API
213213Library
214214-------
215215
216+ - Issue #2846: Add support for gzip.GzipFile reading zero-padded files.
217+ Patch by Brian Curtin.
218+
216219- Issue #7681: Use floor division in appropiate places in the wave module.
217220
218221- Issue #5372: Drop the reuse of .o files in Distutils' ccompiler (since
You can’t perform that action at this time.
0 commit comments