title: Unzipping part of a .gz file using python
body: <p>So here's the problem. I have sample.gz file which is roughly 60KB in size. I want to decompress the first 2000 bytes of this file. I am running into CRC check failed error, I guess because the gzip CRC field appears at the end of file, and it requires the entire gzipped file to decompress. Is there a way to get around this? I don't care about the CRC check. Even if I fail to decompress because of bad CRC, that is OK. Is there a way to get around this and unzip partial .gz files?</p>
  
  <p>The code I have so far is</p>
  
  <pre><code>import gzip
  import time
  import StringIO
  
  file = open('sample.gz', 'rb')
  mybuf = MyBuffer(file)
  mybuf = StringIO.StringIO(file.read(2000))
  f = gzip.GzipFile(fileobj=mybuf)
  data = f.read()
  print data
  </code></pre>
  
  <p>The error encountered is</p>
  
  <pre><code>File "gunzip.py", line 27, in ?
      data = f.read()
  File "/usr/local/lib/python2.4/gzip.py", line 218, in read
    self._read(readsize)
  File "/usr/local/lib/python2.4/gzip.py", line 273, in _read
    self._read_eof()
  File "/usr/local/lib/python2.4/gzip.py", line 309, in _read_eof
    raise IOError, "CRC check failed"
  IOError: CRC check failed
  </code></pre>
  
  <p>Also is there any way to use zlib module to do this and ignore the gzip headers?</p>