Advertisement
Python253

split_zip_merger

Mar 5th, 2024
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Filename: split_zip_merger.py
  3. # Author: Jeoi Reqi
  4.  
  5. """
  6. Split File Merger Script
  7.  
  8. This Python script facilitates the merging of split files commonly used for large file distribution.
  9. The script prompts the user to provide details about the split files, such as the common prefix, file extension, and the desired output folder.
  10.  
  11. Usage:
  12. 1. The user provides the common prefix of the split files (e.g., 'LG-TRANS1_2.zip.').
  13. 2. The file extension of the split files is specified (e.g., '.001').
  14. 3. The user selects the folder containing the split parts.
  15. 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.
  16. 5. The contents of the combined ZIP file are then extracted into a subfolder within the output directory.
  17.  
  18. Requirements:
  19. - Python 3
  20. - tkinter: For the graphical user interface and file dialog
  21. - shutil: For file operations
  22. - os: For working with file paths and directories
  23.  
  24. This script is particularly useful when dealing with files that have been split into smaller parts for distribution.
  25. It simplifies the process of merging these parts back into the original file, making it easier to manage and work with the contents.
  26.  
  27. """
  28.  
  29. import os
  30. import shutil
  31. import tkinter as tk
  32. from tkinter import filedialog
  33.  
  34. # Dependencies
  35. # - tkinter: For the graphical user interface and file dialog
  36. # - shutil: For file operations
  37. # - os: For working with file paths and directories
  38.  
  39. def combine_and_extract(folder_path, file_prefix, file_extension, output_folder):
  40.     # Change to the specified folder
  41.     os.chdir(folder_path)
  42.  
  43.     # Get a list of split parts in the folder
  44.     split_parts = sorted([file for file in os.listdir() if file.startswith(file_prefix) and file.endswith(file_extension)])
  45.  
  46.     if not split_parts:
  47.         print(f"No split parts found with the specified prefix '{file_prefix}' and extension '{file_extension}' in the specified folder.")
  48.         return
  49.  
  50.     # Combine split parts into a single ZIP file
  51.     combined_zip = os.path.join(output_folder, f"{file_prefix}_combined{file_extension}")
  52.     with open(combined_zip, 'wb') as combined_file:
  53.         for part in split_parts:
  54.             with open(part, 'rb') as part_file:
  55.                 shutil.copyfileobj(part_file, combined_file)
  56.  
  57.     print(f"Combined split parts into {combined_zip}")
  58.  
  59.     # Extract the contents of the ZIP file
  60.     extraction_path = os.path.join(output_folder, f"{file_prefix}_extracted_contents")
  61.     shutil.unpack_archive(combined_zip, extract_dir=extraction_path)
  62.  
  63.     print(f"Contents extracted to '{extraction_path}' folder.")
  64.  
  65. def select_folder():
  66.     root = tk.Tk()
  67.     root.withdraw()  # Hide the main window
  68.  
  69.     # Prompt the user to edit the filenames based on their requirements
  70.     file_prefix = input("Enter the common prefix of split files (e.g., 'LG-TRANS1_2.zip.'): ")
  71.     file_extension = input("Enter the file extension of split files (e.g., '.001'): ")
  72.     output_folder = input("Enter the folder where combined and extracted contents should be stored: ")
  73.  
  74.     # Validate the output folder
  75.     if not os.path.exists(output_folder):
  76.         os.makedirs(output_folder)
  77.  
  78.     folder_path = filedialog.askdirectory(title="Select Folder Containing Split Parts")
  79.  
  80.     if folder_path:
  81.         combine_and_extract(folder_path, file_prefix, file_extension, output_folder)
  82.     else:
  83.         print("Folder selection canceled.")
  84.  
  85. if __name__ == "__main__":
  86.     select_folder()
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement