ESP8266_Treppenlicht/create_gz_files.py

38 lines
1022 B
Python
Raw Normal View History

2021-06-22 18:25:27 +00:00
#!/usr/bin/python3
2021-06-10 18:58:48 +00:00
import os, gzip
# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/
2021-06-10 18:58:48 +00:00
def convert_to_gzip(src, out, f):
input_file = f'{src}{f}'
output_file = f'{out}{f}.gz.h'
output_charp = f'{f.replace(".", "_")}_gz'
2021-06-10 18:58:48 +00:00
top = ''
with open(input_file, 'rb') as f_in:
gz = gzip.compress(f_in.read())
gzlen = len(gz)
2021-06-10 18:58:48 +00:00
top += f'// filename: {f}.gz.h\n'
top += f'#define {output_charp}_len {gzlen}\n'
top += f'const char {output_charp}[] = '
top += '{'
with open(output_file, 'wb') as f_out:
for i, b in enumerate(gz):
if not i%10:
top += '\n '
top += f'0x{b:02X}, '
top = top[:-2]
top += '};'
f_out.write(top.encode(encoding='utf-8'))
2021-06-10 18:58:48 +00:00
2021-06-23 14:18:54 +00:00
src='data/'
out='compress/'
2021-06-23 15:30:15 +00:00
if not 'compress' in os.listdir():
os.mkdir('compress')
for f in os.listdir(src):
convert_to_gzip(src, out, f)