8

I have a big gzip file and I would like to read only parts of it using seek. About the use of seek on gzip files, this page says:

The seek() position is relative to the uncompressed data, so the caller does not even need to know that the data file is compressed.

Does this imply that seek has to read and decompress the data from the beginning of the file to the target position?

usual me
  • 8,338
  • 10
  • 52
  • 95

1 Answers1

5

Yes. This is the code:

elif self.mode == READ:
    if offset < self.offset:
        # for negative seek, rewind and do positive seek
        self.rewind()
    count = offset - self.offset
    for i in range(count // 1024):
        self.read(1024)
    self.read(count % 1024)

Alternatives are discussed here. The problem is inherent to the gzip format.

Community
  • 1
  • 1
Veedrac
  • 58,273
  • 15
  • 112
  • 169