Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement zopfli for a better gzip compression?

Google says Zopfli gives a better gzip-functionality and it's compatible with all browsers (decompression is the same, compression takes a lot longer, but you get an additional 5-10% smaller static files footprint)

So, i know you can do this for gzip compression of static files:

<system.webServer>
 <httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
 dynamicCompressionDisableCpuUsage="90"
 dynamicCompressionEnableCpuUsage="80"
maxDiskSpaceUsage="100" minFileSizeForComp="2700"
noCompressionForRange="true"
sendCacheHeaders="false"
staticCompressionDisableCpuUsage="100"
staticCompressionEnableCpuUsage="80"    >
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" dynamicCompressionLevel="4" 
          staticCompressionLevel="7" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="application/xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/rss+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="application/xml" enabled="true" />
    <add mimeType="image/svg+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>

</httpCompression>

...

But how would you implement Zopfli instead of the standard GZip-library in web.config?

like image 573
NicoJuicy Avatar asked Mar 01 '13 11:03

NicoJuicy


2 Answers

You can get pigz for static compression, which is a parallel gzip replacement that includes zopfli compression at level 11.

zopfli would likely not be beneficial for dynamic compression, since it will take much longer to compress out that extra few percent of data than it would take to simply transmit that extra few percent of data.

zopfli is intended for those cases where something is compressed once and then sent or stored many times and decompressed many times. Not for cases where something is compressed once and decompressed once.

like image 104
Mark Adler Avatar answered Oct 17 '22 00:10

Mark Adler


Zopfli compression is slow, so I would recommend compressing static files before uploading them, even better use a CDN and Zopfli for static files.

If you are using .net take a look at a library I recently published on github

https://github.com/echovoice/libzopfli-sharp

I derived the Stream class, usage is simple

using (MemoryStream compressStream = new MemoryStream())
using (ZopfliStream compressor = new ZopfliStream(compressStream, ZopfliFormat.ZOPFLI_FORMAT_DEFLATE))
{
    compressor.Write(uncompressed, 0, before);
    compressor.Close();
    compressed = compressStream.ToArray(); // here is the compressed data
}

This library is available on Nuget as libzopfli-sharp, https://www.nuget.org/packages/libzopfli-sharp

So it would be possible to use this class to create and register a filter for IIS, but that would be a bad idea and make website performance worse.

like image 5
artfulhacker Avatar answered Oct 16 '22 22:10

artfulhacker