About the use of seek on gzip files

最后都变了- 提交于 2019-12-23 12:57:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25985645/about-the-use-of-seek-on-gzip-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!