Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # Filename: split_zip_merger.py
- # Author: Jeoi Reqi
- """
- Split File Merger Script
- This Python script facilitates the merging of split files commonly used for large file distribution.
- The script prompts the user to provide details about the split files, such as the common prefix, file extension, and the desired output folder.
- Usage:
- 1. The user provides the common prefix of the split files (e.g., 'LG-TRANS1_2.zip.').
- 2. The file extension of the split files is specified (e.g., '.001').
- 3. The user selects the folder containing the split parts.
- 4. The script combines the split parts into a single ZIP file, named based on the provided prefix, and stores it in the specified output folder.
- 5. The contents of the combined ZIP file are then extracted into a subfolder within the output directory.
- Requirements:
- - Python 3
- - tkinter: For the graphical user interface and file dialog
- - shutil: For file operations
- - os: For working with file paths and directories
- This script is particularly useful when dealing with files that have been split into smaller parts for distribution.
- It simplifies the process of merging these parts back into the original file, making it easier to manage and work with the contents.
- """
- import os
- import shutil
- import tkinter as tk
- from tkinter import filedialog
- # Dependencies
- # - tkinter: For the graphical user interface and file dialog
- # - shutil: For file operations
- # - os: For working with file paths and directories
- def combine_and_extract(folder_path, file_prefix, file_extension, output_folder):
- # Change to the specified folder
- os.chdir(folder_path)
- # Get a list of split parts in the folder
- split_parts = sorted([file for file in os.listdir() if file.startswith(file_prefix) and file.endswith(file_extension)])
- if not split_parts:
- print(f"No split parts found with the specified prefix '{file_prefix}' and extension '{file_extension}' in the specified folder.")
- return
- # Combine split parts into a single ZIP file
- combined_zip = os.path.join(output_folder, f"{file_prefix}_combined{file_extension}")
- with open(combined_zip, 'wb') as combined_file:
- for part in split_parts:
- with open(part, 'rb') as part_file:
- shutil.copyfileobj(part_file, combined_file)
- print(f"Combined split parts into {combined_zip}")
- # Extract the contents of the ZIP file
- extraction_path = os.path.join(output_folder, f"{file_prefix}_extracted_contents")
- shutil.unpack_archive(combined_zip, extract_dir=extraction_path)
- print(f"Contents extracted to '{extraction_path}' folder.")
- def select_folder():
- root = tk.Tk()
- root.withdraw() # Hide the main window
- # Prompt the user to edit the filenames based on their requirements
- file_prefix = input("Enter the common prefix of split files (e.g., 'LG-TRANS1_2.zip.'): ")
- file_extension = input("Enter the file extension of split files (e.g., '.001'): ")
- output_folder = input("Enter the folder where combined and extracted contents should be stored: ")
- # Validate the output folder
- if not os.path.exists(output_folder):
- os.makedirs(output_folder)
- folder_path = filedialog.askdirectory(title="Select Folder Containing Split Parts")
- if folder_path:
- combine_and_extract(folder_path, file_prefix, file_extension, output_folder)
- else:
- print("Folder selection canceled.")
- if __name__ == "__main__":
- select_folder()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement