Skip to content

Commit c792410

Browse files
committed
support gzip
1 parent 86a8cf5 commit c792410

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

ipdb/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99

1010
from .util import bytes2long
11+
from .util import read_file
1112
from .exceptions import NoSupportIPv4Error, NoSupportIPv6Error, NoSupportLanguageError, DatabaseError, IPNotFound
1213

1314

@@ -31,9 +32,8 @@ class Reader:
3132
_v4offset = 0
3233
_v6offsetCache = {}
3334

34-
def __init__(self, name):
35-
file = open(name, "rb")
36-
self.data = file.read()
35+
def __init__(self, name, compression=None):
36+
self.data = read_file(name, compression=compression)
3737
self._file_size = len(self.data)
3838

3939
meta_length = bytes2long(self.data[0], self.data[1], self.data[2], self.data[3])

ipdb/util.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
:copyright: ©2018 by IPIP.net
44
"""
55

6-
6+
import gzip
77
import sys
88

99

@@ -18,3 +18,23 @@ def convert(v):
1818
return v
1919
else:
2020
return ord(v)
21+
22+
23+
def read_file(path, compression=None):
24+
if compression is None:
25+
return _read_file_default(path)
26+
elif compression == 'gzip':
27+
return _read_file_gzip(path)
28+
raise Exception('unsupported compression type: {}'.format(compression))
29+
30+
31+
def _read_file_default(path):
32+
with open(path, "rb") as f:
33+
content = f.read()
34+
return content
35+
36+
37+
def _read_file_gzip(path):
38+
with gzip.open(path, "rb") as f:
39+
content = f.read()
40+
return content

0 commit comments

Comments
 (0)