Created
March 16, 2018 02:56
-
-
Save Avlyssna/93b344344dc353d66cf3e59f2ab5be9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Standard library imports | |
import base64 | |
import io | |
import pathlib | |
import zipfile | |
def pack_files(file_names): | |
zip_buffer = io.BytesIO() | |
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_LZMA) as zip_file: | |
for file_name in file_names: | |
with zip_file.open(file_name, 'w') as copy: | |
with open(file_name, 'rb') as source: | |
copy.write(source.read()) | |
return base64.b64encode(zip_buffer.getvalue()).decode() | |
def pack_file(file_name): | |
return pack_files([file_name]) | |
def pack_directory(directory): | |
file_names = [str(item) for item in pathlib.Path(directory).glob('**/*') if item.is_file()] | |
return pack_files(file_names) | |
def unpack_files(packed_files): | |
zip_buffer = io.BytesIO(base64.b64decode(packed_files.encode())) | |
with zipfile.ZipFile(zip_buffer, 'r', zipfile.ZIP_LZMA) as zip_file: | |
zip_file.extractall() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment