Browse Source

Allow negative offsets to peek near the end of the file

master
JustAnotherArchivist 1 year ago
parent
commit
ccf4d678fb
1 changed files with 9 additions and 1 deletions
  1. +9
    -1
      warc-peek

+ 9
- 1
warc-peek View File

@@ -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)))


Loading…
Cancel
Save