Files
cmg-commands/split_zip_into_under_5GB_chunks.py
2024-12-12 09:38:18 -06:00

50 lines
1.7 KiB
Python
Executable File

#!/usr/bin/python3
import zipfile
import os
from io import BytesIO
def get_zip_path():
while True:
zip_path = input("Enter the path to the zipped file: ")
if os.path.isfile(zip_path):
return zip_path
else:
print(f"The file at {zip_path} does not exist. Please try again.")
def split_zip(input_zip_path, output_folder, max_group_size=4.5 * 1024 * 1024 * 1024):
# Ensure the output folder exists
base = os.path.basename(input_zip_path)
file_name = os.path.splitext(base)[0]
if not os.path.exists(output_folder):
os.makedirs(output_folder)
with zipfile.ZipFile(input_zip_path, 'r') as source_zip:
members = source_zip.namelist()
group_size = 0
group_number = 1
for member in members:
# Read the file content from the zip archive
with BytesIO(source_zip.read(member)) as file_content:
# Check if adding this file would exceed the maximum group size
if (group_size + len(file_content.getvalue())) > max_group_size:
group_number += 1
group_size = 0
output_zip_path = os.path.join(output_folder, f'{file_name}-group_{group_number}.zip')
with zipfile.ZipFile(output_zip_path, 'a') as target_zip:
target_zip.writestr(member, file_content.getvalue())
# Update the size of the current group
group_size += len(file_content.getvalue())
print(f'Successfully split {input_zip_path} into {group_number} groups.')
# Example usage
# input_zip_path = 'path/to/your/large.zip'
input_zip_path = get_zip_path()
output_folder = 'output'
split_zip(input_zip_path, output_folder)