|
|
@@ -27,6 +27,7 @@ import shutil
|
|
|
import stat as stat_module
|
|
|
import urllib
|
|
|
|
|
|
+from bz2 import decompress
|
|
|
from datetime import datetime
|
|
|
from cStringIO import StringIO
|
|
|
from gzip import GzipFile
|
|
|
@@ -687,6 +688,8 @@ def read_contents(codec_type, path, fs, offset, length):
|
|
|
if path.endswith('.gz') and detect_gzip(contents):
|
|
|
codec_type = 'gzip'
|
|
|
offset = 0
|
|
|
+ elif (path.endswith('.bz2') or path.endswith('.bzip2')) and detect_bz2(contents):
|
|
|
+ codec_type = 'bz2'
|
|
|
elif path.endswith('.avro') and detect_avro(contents):
|
|
|
codec_type = 'avro'
|
|
|
elif detect_parquet(fhandle):
|
|
|
@@ -702,6 +705,8 @@ def read_contents(codec_type, path, fs, offset, length):
|
|
|
|
|
|
if codec_type == 'gzip':
|
|
|
contents = _read_gzip(fhandle, path, offset, length, stats)
|
|
|
+ elif codec_type == 'bz2':
|
|
|
+ contents = _read_bz2(fhandle, path, offset, length, stats)
|
|
|
elif codec_type == 'avro':
|
|
|
contents = _read_avro(fhandle, path, offset, length, stats)
|
|
|
elif codec_type == 'parquet':
|
|
|
@@ -790,6 +795,16 @@ def _read_gzip(fhandle, path, offset, length, stats):
|
|
|
return contents
|
|
|
|
|
|
|
|
|
+def _read_bz2(fhandle, path, offset, length, stats):
|
|
|
+ contents = ''
|
|
|
+ try:
|
|
|
+ contents = decompress(fhandle.read(length))
|
|
|
+ except Exception, e:
|
|
|
+ logging.exception('Could not decompress file at "%s": %s' % (path, e))
|
|
|
+ raise PopupException(_("Failed to decompress file."))
|
|
|
+ return contents
|
|
|
+
|
|
|
+
|
|
|
def _read_simple(fhandle, path, offset, length, stats):
|
|
|
contents = ''
|
|
|
try:
|
|
|
@@ -806,6 +821,11 @@ def detect_gzip(contents):
|
|
|
return contents[:2] == '\x1f\x8b'
|
|
|
|
|
|
|
|
|
+def detect_bz2(contents):
|
|
|
+ '''This is a silly small function which checks to see if the file is Bz2'''
|
|
|
+ return contents[:3] == 'BZh'
|
|
|
+
|
|
|
+
|
|
|
def detect_avro(contents):
|
|
|
'''This is a silly small function which checks to see if the file is Avro'''
|
|
|
# Check if the first three bytes are 'O', 'b' and 'j'
|