tar files and encode multiple folders in python

To tar and gzip multiple folders in Python, you can use the tarfile, gzip and shutil modules.

main.py
import tarfile
import gzip
import shutil
import os

# input folders
folders = ['/path/to/folder1', '/path/to/folder2']

# tar file name
tar_file_name = 'multiple_folders.tar.gz'

# create tar file
with tarfile.open(tar_file_name, 'w:gz') as tar:
    # loop through input folders and add them to the tar file
    for folder in folders:
        tar.add(folder)

# open tar file and gzip it
with open(tar_file_name, 'rb') as tar_file:
    with gzip.open(tar_file_name + '.gz', 'wb') as gzip_file:
        shutil.copyfileobj(tar_file, gzip_file)

# remove tar file
os.remove(tar_file_name)
586 chars
25 lines

To encode the contents of the tar file in base64, you can use the base64 module. Here's an updated code block:

main.py
import tarfile
import gzip
import shutil
import os
import base64

# input folders
folders = ['/path/to/folder1', '/path/to/folder2']

# tar file name
tar_file_name = 'multiple_folders.tar.gz'

# create tar file
with tarfile.open(tar_file_name, 'w:gz') as tar:
    # loop through input folders and add them to the tar file
    for folder in folders:
        tar.add(folder)

# open tar file and gzip it
with open(tar_file_name, 'rb') as tar_file:
    with gzip.open(tar_file_name + '.gz', 'wb') as gzip_file:
        shutil.copyfileobj(tar_file, gzip_file)

# remove tar file
os.remove(tar_file_name)

# encode gzip contents in base64
with open(tar_file_name + '.gz', 'rb') as gzip_file:
    file_content = gzip_file.read()
    encoded_content = base64.b64encode(file_content)

# remove gzip file
os.remove(tar_file_name + '.gz')
829 chars
34 lines

Note that this code will remove the original tar and gzip files after encoding the contents in base64. If you want to keep them, comment out the lines with os.remove().

related categories

gistlibby LogSnag