unzip/gunzip/deflate with JavaScript

The open source library JSXGraph (http://jsxgraph.org) contains utilities to read ZLIB compressed files. That means, JSXGraph has a pure JavaScript implementation of deflate, unzip and base64_decode. This can be used for delivering compressed JavaScript inside of an HTML file. Another application is transmission of compressed XML or JSON data with AJAX. Of course, with todays browsers it depends on the transmission bandwidth if this is worthwile. If the web server does not support compression of data, then this tool may be an option.

One possibility to compress the data on server side is to use PHP. The code below writes the content of a JavaScript file as a compressed, base64 encoded string on-the-fly into the HTML. Then on client side this string can be accessed via the JavaScript variable jsxcompressed.

function jxgcompress($filename)
    if (file_exists($filename)) {
        $base64 = base64_encode(gzcompress(rawurlencode(file_get_contents($filename)),9));
        echo "var jxgcompressed = " . $base64 . ";\n";
    } else {
        throw new Exception("$filename not found");


To uncompress and run this code on client side, the following code has to be included in the HTML code:

<script src="./jsxcompressor.js" type="text/javascript"></script>
<script type="text/javascript">

Thats all! The compression rate should be fairly good, since the compression algorithm is essentially gzip. There is some additional overhead since the compressed output has additionally to be base64 encoded. Of course, this effort is useless, if the web server delivers the content already compressed with gzip.

The zip file jsxcompressor.zip contains two examples: testhelloworld.php and testjsxgraph.php.

The jsxcompressor code is open source and can be downloaded here. The source code of the minified JavaScript file jsxcompressor.js consists of a few files from the JSXGraph project:

The source code of the JSXGraph library can be downloaded from https://github.com/jsxgraph/jsxgraph.

JSXCompressor is released under the LGPL 3.0 or the Apache License 2.0.