Files
DeautherX-SRAciJa/web_interface/gzipArraysMakerHTML.py

64 lines
2.0 KiB
Python

import os
import gzip
import tarfile
import glob
def create_gz_archive(html_files, output_name='all_html.tar.gz'):
"""Creates a .gz archive of all .html files."""
with tarfile.open(output_name, 'w:gz') as tar:
for html_file in html_files:
tar.add(html_file)
def create_byte_array(gz_file_path, array_name):
"""Creates a text file with a byte array from the .gz file."""
with open(gz_file_path, 'rb') as f:
data = f.read()
# Format bytes in hex, 16 per line
hex_lines = []
for i in range(0, len(data), 16):
line = ', '.join(f'0x{byte:02X}' for byte in data[i:i+16])
hex_lines.append(line)
# Create file content
content = f'const char {array_name}[] PROGMEM = {{\n'
content += ',\n'.join(hex_lines)
content += '\n};\n'
# Write to file
txt_file = gz_file_path.replace('.gz', '.txt')
with open(txt_file, 'w') as f:
f.write(content)
def main():
# Find all .html files in the current folder
html_files = glob.glob('*.html')
if not html_files:
print("No .html files found in the current folder.")
return
# Create a combined .gz archive of all .html files
create_gz_archive(html_files)
print(f"Created archive: all_html.tar.gz")
# For each .html file, create a separate .gz and byte array
for html_file in html_files:
# .gz file name: html_file + .gz, e.g., attack.html.gz
gz_file = f"{html_file}.gz"
# Array name: base name without extension + 'html', e.g., attackhtml
base_name = os.path.splitext(html_file)[0].replace('.', '') + 'html'
# Compress .html to .gz
with open(html_file, 'rb') as f_in:
with gzip.open(gz_file, 'wb') as f_out:
f_out.writelines(f_in)
# Create byte array
create_byte_array(gz_file, base_name)
print(f"Created: {gz_file} and {gz_file.replace('.gz', '.txt')}")
if __name__ == "__main__":
main()