forked from Jueee/PythonStandardLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib05.12-gzip.py
56 lines (35 loc) · 980 Bytes
/
lib05.12-gzip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
gzip 模块
gzip 模块用来读写 gzip 格式的压缩文件
'''
# 使用 gzip 模块读取压缩文件
import gzip
file = gzip.GzipFile('samples/sample.gz')
print(file.read())
# 给 gzip 模块添加 seek/tell 支持
import gzip
class gzipFile(gzip.GzipFile):
"""adds seek/tell support to GzipFile"""
offset = 0
def read(self, size = 1000):
data = gzip.GzipFile.read(self, size)
self.offset = self.offset + len(data)
return data
def seek(self, offset, whence = 0):
# figure out new position (we can only seek forwards)
if whence == 0:
position = offset
elif whence == 1:
position = self.offset + offset
else:
raise(IOError, 'Illeagal argument')
if position < self.offset:
raise(IOError, 'Connot seek backwards')
while position > self.offset:
if not self.read(min(position - self.offset, 16384)):
break
def tell(self):
return self.offset
file = gzipFile('samples/sample.gz')
file.seek(80)
print(file.read())