Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import gzip
  2.  
  3. class gzipFile(gzip.GzipFile):
  4. # adds seek/tell support to GzipFile
  5.  
  6. offset = 0
  7.  
  8. def read(self, size=None):
  9. data = gzip.GzipFile.read(self, size)
  10. self.offset = self.offset + len(data)
  11. return data
  12.  
  13. def seek(self, offset, whence=0):
  14. # figure out new position (we can only seek forwards)
  15. if whence == 0:
  16. position = offset
  17. elif whence == 1:
  18. position = self.offset + offset
  19. else:
  20. raise IOError, "Illegal argument"
  21. if position < self.offset:
  22. raise IOError, "Cannot seek backwards"
  23.  
  24. # skip forward, in 16k blocks
  25. while position > self.offset:
  26. if not self.read(min(position - self.offset, 16384)):
  27. break
  28.  
  29. def tell(self):
  30. return self.offset
  31.  
  32. #
  33. # try it
  34.  
  35. file = gzipFile("samples/sample.gz")
  36. file.seek(80)
  37.  
  38. print file.read()
  39.  
  40. ## this the 127th
  41. ## Upperclass Twit of the Year Show.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement