Created
April 6, 2023 01:37
-
-
Save ImanCol/3034d15b714d0f991b073a139ff0d486 to your computer and use it in GitHub Desktop.
Revisions
-
ImanCol created this gist
Apr 6, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ import os import struct import progressbar import tkinter as tk from tkinter import filedialog def choose_file(): root = tk.Tk() root.withdraw() file_path = filedialog.askopenfilename(filetypes=[("CFG files", "!archive.cfg")]) return file_path def pack_all_cfgs(root_dir): num_files = sum(len(filenames) for _, _, filenames in os.walk(root_dir) if filenames) widgets = [ progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA(), ' | Processing: ', progressbar.Variable(name='current_folder', value='Folder name') ] with progressbar.ProgressBar(max_value=num_files, widgets=widgets, redirect_stdout=True) as bar: i = 0 for dirpath, _, filenames in os.walk(root_dir): for filename in filenames: if filename.endswith(('!archive.cfg')): in_file = os.path.join(dirpath, filename) rel_dir = os.path.relpath(dirpath, root_dir) out_dir = os.path.join(os.path.dirname(dirpath)) out_file = os.path.join(out_dir, f"{os.path.basename(dirpath)}.EXP") os.makedirs(out_dir, exist_ok=True) with open(in_file, 'r') as f: file_data = f.readlines() array = bytearray(b'FORM\x00\x00\x00\x00') list_data = [] list_data.append(array) for line in file_data: filepath = os.path.join(dirpath, line.strip()) with open(filepath, 'rb') as f: file_content = f.read() filename = os.path.basename(filepath) name_length = len(filename) - 4 filename = filename[:name_length] list_data.append(filename.encode('utf-8')) list_data.append(struct.pack('>I', len(file_content))) list_data.append(file_content) i += 1 bar.update(i, current_folder=rel_dir) data = b''.join(list_data) data_length = len(data) - 8 data_length_bytes = struct.pack('>I', data_length) data = bytearray(data) data[4:8] = data_length_bytes with open(out_file, 'wb') as f: f.write(data) root_dir = filedialog.askdirectory() if root_dir: pack_all_cfgs(root_dir) else: print("No folder selected.") print("Empaquetado completado.")