Android / iOS



Posted: May 2005


Kunzip is a free library under the LGPL license for decompressing ZIP archives. Kunzip started out as a test program I wrote for decompressing .zip files through a command line program. I eventually added hooks to make it a .so or DLL that can be used from Unix or Windows programs. My plan for the kunzip library was to make it as small (currently under 40k) and simple to use as possible so it can even be used from VisualBasic or such. It should be fully compatible with zipfiles created by InfoZip, PkZip, and WinZip. You can download it from the link below and view the prototypes for different languages that can call the kunzip library.

Related Projects

Graphics: SSE Image Processing, GIF, TIFF, BMP/RLE, JPEG, AVI, kunzip, gif2avi, Ringtone Tools, yuv2rgb, RTSP


kunzip-2015-02-09.tar.gz (Source Code) (Windows binary DLL, kunzip.dll)
kunzip-2006-11-14.tar.gz (Source Code) (Windows binary DLL, kunzip.dll)

Visual Basic 6 function defs
INNO prototypes
C/C++ prototypes
Delphi/Pascal prototypes (Thanks Vitaly Kravchenko!)

kunzip_test-2005-12-21.tar.gz (Small test program that calls functions in the for Linux/Unix-like operating systems)


The November 14, 2006 version of kunzip changes the license to LGPL. Nothing more.

The April 25, 2006 version of kunzip fixes bugs in the kunzip_get_offset_by_name() function.

The January 20, 2006 version of kunzip fixes bugs in the kunzip_get_modtime() and kunzip_get_filesize() functions.

I guess for languages such as Russian, a file name in a zipfile is stored using a different encoding method. Right now on a Windows computer kunzip.dll does not handle this correctly. I'm working on fixing that.

The dates ignore daylight savings time. Therefore the uncompressed dates could be an hour off depending on your timezone. I think this is actually a common problem with ZIP unarchivers, but I could be wrong.

Some Boring History About Kunzip

I originally started this code to be able to decompress (and maybe even compress) PNG graphic files. Since this is the same algorithm used in gzip and zip (WinZip, InfoZip, etc), I decided to make it work with .zip files instead. Unfortunately, all the specs I had for this compression were for PNG and Zip is slightly different, so I had some problems getting it working :).

My first version kunzip (which built the huffman tables using arrays instead of trees and did a lot of fseeking) ran painfully slow, but my latest version runs quite well. One interesting thing I found, when I did static huffman decompression for TIFF, I did it using lookup tables instead of trees. This actually ran very fast, although I'm not sure of the speed difference if I used trees instead. On this project, I started out doing all 3 huffman sets using lookup tables. After I saw how slow it ran, I switched the length huffman codes to a tree and the decompression ran almost double the speed. When I changed the distance huffman codes to trees, the decompression slowed down by a few seconds. So I left length/distance huffman codes as trees and kept the first huffman codes as lookup tables. I may try and improve on the speed even more later. Not only can you download the source code which can be compiled on both Unix/Linux and Windows, but I've also included a Windows DLL I built for doing unzipping. I think it is quite easy to use, so if you need an unzip library for Windows, feel free to use this (feel free to make a donation to my PayPal account too if you like :).

Btw, this code was written to be compiled on Unix (Linux,FreeBSD,etc) systems. If you want to build the DLL, you can download the source code and type: make dll. The mingw C compiler needs to be used. I'm 99% sure I did some things that don't work in Visual C++.

The original May 6, 2005 version of kunzip decompresses a 120meg file in about 3min40sec on my AMD Athlon XP box. Since then, I put some major optomizations in and it now decompresses the same file in 20 seconds.

How DEFLATE works

DEFLATE compresses data by first running an LZ77 compression scheme on a chunk of binary data and then taking the LZ77 codes and compressing those with huffman. LZ77 compresses data in the following way: If while writing a file, the compressor sees that a combination of characters have already been written to the file, the compressor will instead write out a distance code (the number of bytes to search backwards in the uncompessed file) and a length code (the number of bytes to copy from that point in the file). So for example, if you had a string of text that looks like this:

i am not a number, i am a free man

LZ77 would compress that to: i am not a number, (19,5)free man

Where 19 is the distance backwards and 5 is the number of characters that are similar. In DEFLATE, a series of codes (values from 0 to 285) tell the decompressor whether to output a single byte (0 to 255), to stop decompressing (256), or the length of bytes to copy with a distance code following (257-285). These codes are huffman encoded to make the compressed data more compact.

Copyright 1997-2019 - Michael Kohn