Guest User

split_dat.py

a guest
Nov 28th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | Software | 0 0
  1. import os
  2. import argparse
  3.  
  4. def split_dat_file(input_file):
  5.     try:
  6.         # Open the .DAT file in binary mode
  7.         with open(input_file, 'rb') as file:
  8.             data = file.read()
  9.        
  10.         # Define headers and initialize variables
  11.         headers = [b'VAGp', b'XVAG']
  12.         positions = []
  13.        
  14.         # Search for all occurrences of headers
  15.         for header in headers:
  16.             start = 0
  17.             while (index := data.find(header, start)) != -1:
  18.                 positions.append((index, header))
  19.                 start = index + 1
  20.  
  21.         # Sort the positions by order of appearance
  22.         positions.sort()
  23.        
  24.         # Extract files based on headers
  25.         for i in range(len(positions)):
  26.             start_index, header = positions[i]
  27.             end_index = positions[i + 1][0] if i + 1 < len(positions) else len(data)
  28.            
  29.             # Extract file data
  30.             file_data = data[start_index:end_index]
  31.             extension = '.VAG' if header == b'VAGp' else '.XVAG'
  32.            
  33.             # Create output file name
  34.             output_file = f'{os.path.splitext(input_file)[0]}_{i + 1}{extension}'
  35.            
  36.             # Write the extracted data to a new file
  37.             with open(output_file, 'wb') as out_file:
  38.                 out_file.write(file_data)
  39.            
  40.             print(f"Created file: {output_file}")
  41.    
  42.     except Exception as e:
  43.         print(f"Error: {e}")
  44.  
  45. if __name__ == "__main__":
  46.     parser = argparse.ArgumentParser(description="Split a .DAT file into multiple .VAG and .XVAG files.")
  47.     parser.add_argument("input_file", help="Path to the input .DAT file")
  48.     args = parser.parse_args()
  49.  
  50.     split_dat_file(args.input_file)
  51.  
Add Comment
Please, Sign In to add comment