diff --git a/.gitignore b/.gitignore index 437849a..eaa2e0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .pio .vscode compress +data_gz platformio.ini include/wifi_credentials.h include/*.gz.h \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 4eaa233..93ad4be 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -48,5 +48,6 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp" - } + }, + "python.pythonPath": "C:\\Users\\simon\\AppData\\Local\\Programs\\Python\\Python37\\python.exe" } \ No newline at end of file diff --git a/create_gz_c_arr.py b/create_gz_c_arr.py new file mode 100644 index 0000000..ca2548c --- /dev/null +++ b/create_gz_c_arr.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +import os, gzip +# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/ + +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' + + top = '' + with open(input_file, 'rb') as f_in: + gz = gzip.compress(f_in.read()) + gzlen = len(gz) + + 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')) + + +src='data/' +out='compress/' + +if not 'compress' in os.listdir(): + os.mkdir('compress') + +for f in os.listdir(src): + convert_to_gzip(src, out, f) \ No newline at end of file diff --git a/create_gz_files.py b/create_gz_files.py index ca2548c..258fcdb 100644 --- a/create_gz_files.py +++ b/create_gz_files.py @@ -1,38 +1,15 @@ #!/usr/bin/python3 import os, gzip -# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/ +# https://stackoverflow.com/questions/8156707/gzip-a-file-in-python -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' +src_dir='data' +out_dir='data_gz' - top = '' - with open(input_file, 'rb') as f_in: - gz = gzip.compress(f_in.read()) - gzlen = len(gz) +if not 'data_gz' in os.listdir(): + os.mkdir('data_gz') - 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')) - - -src='data/' -out='compress/' - -if not 'compress' in os.listdir(): - os.mkdir('compress') - -for f in os.listdir(src): - convert_to_gzip(src, out, f) \ No newline at end of file +for f in os.listdir(src_dir): + with open(f'{src_dir}/{f}', 'rb') as f_in: + with gzip.open(f'{out_dir}/{f}.gz', 'wb') as f_out: + f_out.writelines(f_in) \ No newline at end of file diff --git a/data/compress.html b/data/compress.html new file mode 100644 index 0000000..e806476 --- /dev/null +++ b/data/compress.html @@ -0,0 +1,12 @@ + + + +
+