46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
import gzip
|
|
import os
|
|
|
|
# 定义文件路径
|
|
web_folder = "web"
|
|
output_header = "esp8266/web.h"
|
|
files_to_compress = ["index.html", "styles.css", "script.js"]
|
|
|
|
# 压缩文件并生成 C 常量
|
|
def compress_file(input_path, output_path):
|
|
with open(input_path, 'rb') as f_in:
|
|
with gzip.open(output_path, 'wb') as f_out:
|
|
f_out.writelines(f_in)
|
|
|
|
with open(output_path, 'rb') as f: # 修复 os.open 为 open
|
|
compressed_data = f.read()
|
|
|
|
return compressed_data
|
|
|
|
# 生成 web.h 文件
|
|
def generate_header():
|
|
with open(output_header, 'w', encoding='utf-8') as header_file:
|
|
header_file.write("#ifndef WEB_H\n#define WEB_H\n\n")
|
|
|
|
for file_name in files_to_compress:
|
|
input_path = os.path.join(web_folder, file_name)
|
|
output_path = input_path + ".gz"
|
|
|
|
if os.path.exists(input_path):
|
|
compressed_data = compress_file(input_path, output_path)
|
|
constant_name = file_name.replace('.', '_').upper()
|
|
|
|
header_file.write(f"const char {constant_name}[] = {{\n")
|
|
header_file.write(",\n".join(
|
|
[
|
|
", ".join(f"0x{byte:02x}" for byte in compressed_data[i:i+16])
|
|
for i in range(0, len(compressed_data), 16)
|
|
]
|
|
))
|
|
header_file.write("\n};\n\n")
|
|
|
|
header_file.write("#endif // WEB_H\n")
|
|
|
|
if __name__ == "__main__":
|
|
generate_header()
|
|
print(f"Header file '{output_header}' generated successfully.") |