Commit fb2b07f6 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'bz2download' into 'master'

Fixed issue with bz2 files

See merge request !83
parents b5c85f8b 1934da87
Pipeline #20900 passed with stages
in 8 minutes and 27 seconds
......@@ -14,13 +14,26 @@ def _unzip(zip_file, directory):
myzip.extractall(directory)
def _untar(tar_file, directory, mode):
def _untar(tar_file, directory, ext):
if ext in [".bz2" or ".tbz2"]:
mode = "r:bz2"
elif ext in [".gz" or ".tgz"]:
mode = "r:gz"
else:
mode = "r"
import tarfile
with tarfile.open(name=tar_file, mode='r:'+mode) as t:
with tarfile.open(name=tar_file, mode=mode) as t:
t.extractall(directory)
def _unbz2(bz2_file):
import bz2
with bz2.BZ2File(bz2_file) as t:
open(os.path.splitext(bz2_file)[0], 'wb').write(t.read())
def download_file(url, out_file):
"""Downloads a file from a given url
......@@ -90,12 +103,16 @@ def download_and_unzip(urls, filename):
# Uncompressing if it is the case
ext = os.path.splitext(filename)[-1].lower()
header = os.path.splitext(filename)[0].lower()
if ext == ".zip":
logger.info("Unziping in {0}".format(filename))
_unzip(filename, os.path.dirname(filename))
elif ext in [".gz", ".bz2"]:
elif header[-4:] == ".tar" or ext in [".tgz", ".tbz2"]:
logger.info("Untar/gzip in {0}".format(filename))
_untar(filename, os.path.dirname(filename), mode=ext[1:])
_untar(filename, os.path.dirname(filename), ext)
elif ext == ".bz2":
logger.info("Unbz2 in {0}".format(filename))
_unbz2(filename)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment