Embedding files in code

I just stumbled on the *nix xxd -i command and thought it might be useful to folks to know how I'm using it. I was already aware that you could embed binary data for things like images into a character array. It occurred to me today that the same could be done with any file.

I've already played around with the SD web server and on that was toying with using .gz format (compressing text files like javascript, css and html with gzip -9). On the ESP8266, their web server automatically puts a Content-Encoding header if the filename ends with .gz, so what I did was modified the paths so the various types were under directories matching the actual type all placed under a gz directory (e.g. gz/htm/, gz/js/, gz/css, etc) then set up my routing to set the proper data type based on the directory they were in, but the files themselves had .gz extensions for the web server to trigger the content encoding.

But, depending on the server implementation, you can generally set the encoding yourself as well manually. Which means you can use gzipped files converted to char arrays to put the files right in the code. (mind you, this is for small website needs. They do use up memory - but on the ESP8266 there's more memory to work with, so I figured I would try this for the smaller web server implementations)

Plus, since you can throw them into a separate header, you can script the process of converting your changes in your web stuffs. I find they do tend to get smaller if you minify first before gzipping. So just replace the minify with your favorite minification scripts:

minify_html_script.sh index.htm | gzip -c9 > index.htm.gz
xxd -i index.htm.gz > index_htm_gz.h

Then you can just #include "index_htm_gz.h" and the variable will be assigned to a char array index_htm_gz with a length helper index_htm_gz_len