diff --git a/warc-peek b/warc-peek index 5bc1bb2..65ff88c 100755 --- a/warc-peek +++ b/warc-peek @@ -16,6 +16,7 @@ # not attempt decompression when `1F 8B` is found in the last 512 bytes of the window. You can increase `LENGTH` to compensate for this if necessary. import argparse +import io import logging import zlib @@ -35,7 +36,14 @@ def finditer(b, sub): def find_offsets(warcfile, offset, length): with open(warcfile, 'rb') as fp: - fp.seek(offset) + if offset >= 0: + fp.seek(offset) + else: + # Negative offset: go back from EOF and fix offset for correct output + fp.seek(0, io.SEEK_END) + size = fp.tell() + fp.seek(offset, io.SEEK_END) + offset = size + offset buffer = fp.read(length) logger.debug('Buffer length: {:d}'.format(len(buffer)))