title: gzip unexpected end of file
body: <p>I can check the integrity of a gzip file with <code>gzip -t file.gz</code> and <code>zcat file.gz &gt; /dev/null</code> as per <a href="https://stackoverflow.com/questions/2001709/how-to-check-if-a-unix-tar-gz-file-is-a-valid-file-without-uncompressing">previous answers</a>.</p>
  
  <p>Sometimes I have jobs dying before a compression of a large file finishes. I will get an error about unexpected end of file, if I check the file from beginning to end. But is it possible to only test, that there is no unexpected end of the compressed file, so I don't have to read through the entire file?</p>
  
  <p>EDIT 2018 in accordance with answer from Mark Adler below (Python 3.2+ solution):</p>
  
  <pre><code>import os
  import string
  import gzip
  
  with gzip.open('test.gz', 'wt') as f:
      f.write(string.ascii_lowercase)
  
  with open('test.gz', 'rb') as f:
      f.seek(-4 , os.SEEK_END)
      length = int.from_bytes(f.read(), byteorder='little')
      assert length == 26
      print('Thanks Mark Adler!') 
      print('The English alphabet has {length} letters.'.format(length=length))
  </code></pre>