Assuming you send your files via http, I suggest that you allow the web server to handle this by sending a file encoded with gzip content.
If, for example, you use Apache, you can enable mod_deflate
If for some reason you cannot change the configuration of your web server, php also has a built-in gzip handler that you could use instead. See: ob_gzhandler
Edit:
As a client to server, it looks like this is directly supported by any implementation of XmlHttpRequest. Perhaps you can find your own gzip compression algorithm for Javascript, and then set the request header to indicate that it is compressed. Thus, it becomes a transparently decoded web server, and you do not need to do anything special in php.
See this page: JavaScript implementation of Gzip .
source share