Skip to content

Instantly share code, notes, and snippets.

@Avlyssna
Created March 16, 2018 02:56
Show Gist options
  • Save Avlyssna/93b344344dc353d66cf3e59f2ab5be9d to your computer and use it in GitHub Desktop.
Save Avlyssna/93b344344dc353d66cf3e59f2ab5be9d to your computer and use it in GitHub Desktop.
# 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