Hypthetical zlib (DotNetZip) implementation for libz

Using alternative compression algorithm requires you to do two things: implementing the encoder and plugging it in into libz tool and implementing the decoder and add it to your application.
Note: you can download libz sources (not nuget package) to see implementation for LZ4 codec.


You need to implement a class implementing ICodec (LibZ.Tool.Interfaces) interface and expose it to MEF:

namespace LibZ.Codec.ZLib
	public class ZLibCodec: ICodec
		public void Initialize() { /* no need for it */ }
		public string Name { get { return "zlib"; } }
		public byte[] Encode(byte[] inputData) { 
			return ZlibStream.CompressBuffer(inputData); 
		public byte[] Decode(byte[] inputData, int outputLength) { 
			return ZlibStream.UncompressBuffer(inputData); 
Yup. That's it. Take a look at postbuild.cmd (for lz4 codec) now to see how it get's distributed:

libz add --libz zlib.libzcodec --include *.dll --codec deflate --move

Yup, again. It's just a LibZ container with all the assemblies. Please note, it uses deflate for compression. Chick and egg, again.
When you put your .libzcodec file next to libz.exe it will automatically discovered.


You need zlib referenced from your main assembly. And (almost) first thing you need to do it to register new codec.

using LibZ.Bootstrap;

static int Main(string[] args)
    LibZResolver.RegisterDecoder("zlib", (b, l) => ZlibStream.UncompressBuffer(b));
    LibZResolver.Startup(() => {
        // here goes your code
As before, bootstrapper needs to be outside container:

libz inject-dll --assembly MyApplication.exe --include LibZ.Bootstrap.dll --move

You also need Ionic.Zlib.dll to be accessible before zlib codec is initialized (chink and egg, remember?). You can achive this in two ways.
By embedding it in the main assembly:

libz inject-dll --assembly MyApplication.exe --include Ionic.Zlib.dll --move

or by adding it to container but encoded with deflate:

libz add --libz Container.libz --include Ionic.Zlib.dll --codec deflate --move

the rest can be added using zlib:

libz add --libz Container.libz --include *.dll --codec zlib --move

Last edited Apr 27, 2013 at 10:30 PM by Krashan, version 2


zerowalker Aug 10, 2013 at 9:44 AM 
can you show how to use it with LZ4HC:)?