From f2e2e41d0436bdb8a0a0115c5ca6e46f0a37b90c Mon Sep 17 00:00:00 2001 From: Romuald Juchnowicz-Bierbasz Date: Tue, 16 Apr 2019 10:24:32 +0200 Subject: [PATCH] SDK-2760: Add tools module --- galaxy/api/tools.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 galaxy/api/tools.py diff --git a/galaxy/api/tools.py b/galaxy/api/tools.py new file mode 100644 index 0000000..3996d25 --- /dev/null +++ b/galaxy/api/tools.py @@ -0,0 +1,20 @@ +import io +import os +import zipfile +from glob import glob + +def zip_folder(folder): + files = glob(os.path.join(folder, "**"), recursive=True) + files = [file.replace(folder + os.sep, "") for file in files] + files = [file for file in files if file] + + zip_buffer = io.BytesIO() + with zipfile.ZipFile(zip_buffer, mode="w", compression=zipfile.ZIP_DEFLATED) as zipf: + for file in files: + zipf.write(os.path.join(folder, file), arcname=file) + return zip_buffer + +def zip_folder_to_file(folder, filename): + zip_content = zip_folder(folder).getbuffer() + with open(filename, "wb") as archive: + archive.write(zip_content)