Skip to content

Commit 8dd11bb

Browse files
committed
Implement seek based on io.BytesIO
1 parent 37fa093 commit 8dd11bb

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

zipstream/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
str, bytes,
2020
ZIP64_VERSION,
2121
ZIP_BZIP2, BZIP2_VERSION,
22-
ZIP_LZMA, LZMA_VERSION)
22+
ZIP_LZMA, LZMA_VERSION,
23+
SEEK_SET, SEEK_CUR, SEEK_END)
2324

2425
from zipfile import (
2526
ZIP_STORED, ZIP64_LIMIT, ZIP_FILECOUNT_LIMIT, ZIP_MAX_COMMENT,
@@ -71,7 +72,15 @@ def next(self):
7172
raise NotImplementedError()
7273

7374
def seek(self, offset, whence=None):
74-
raise NotImplementedError()
75+
if whence == SEEK_SET:
76+
if offset < 0:
77+
raise ValueError('negative seek value -1')
78+
self.data_pointer = offset
79+
elif whence == SEEK_CUR:
80+
self.data_pointer = max(0, self.data_pointer + offset)
81+
elif whence == SEEK_END:
82+
self.data_pointer = max(0, offset)
83+
return self.data_pointer
7584

7685
def tell(self):
7786
return self.data_pointer

zipstream/compat.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@
6565
try:
6666
from zipfile import ZIP_MAX_COMMENT
6767
except ImportError:
68-
ZIP_MAX_COMMENT = (1 << 16) - 1
68+
ZIP_MAX_COMMENT = (1 << 16) - 1
69+
70+
71+
# Copy from io
72+
SEEK_SET = 0 # start of the stream (the default); offset should be zero or positive
73+
SEEK_CUR = 1 # current stream position; offset may be negative
74+
SEEK_END = 2 # end of the stream; offset is usually negative

0 commit comments

Comments
 (0)