Click here to Skip to main content
13,294,840 members (64,152 online)
Click here to Skip to main content


153 bookmarked
Posted 9 Mar 2006

LiteZip and LiteUnzip

, 7 Aug 2008
Easy to use, small-footprint DLLs to let your app create zip archives, and extract the contents of them. Useful for C, C++, VB, and other languages. Works for Win32 and Linux
BODY, P, TD { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt }
H2,H3,H4,H5 { color: #ff9900; font-weight: bold; }
H2 { font-size: 13pt; }
H3 { font-size: 12pt; }
H4 { font-size: 10pt; color: black; }
PRE { BACKGROUND-COLOR: #FBEDBB; FONT-FAMILY: "Courier New", Courier, mono; WHITE-SPACE: pre; }
CODE { COLOR: #990000; FONT-FAMILY: "Courier New", Courier, mono; }
BODY, P, H1, H2, H3, H4, H5, H6, LI, TD, TH, DD, DT
	font-family: Verdana, Helvetica, Arial, sans-serif;
	font-size: 10pt;
	color: black;
	background-color: #ffffff;
	color: #000000;
	/*Scrollbar-face-color: #ffcc99; 
	Scrollbar-arrow-color: black;*/
	list-style-image: none;
H1, H2, H3, H4, H5, TH
	font-weight: bold;
H2, H3, H5
	color: #ff9900;
	font-size: 16pt;
	font-size: 13pt;
	font-family: Arial, sans-serif;
	font-size: 11pt;
	font-size: 10pt;
	margin-bottom: 2px;
	font-size: 9pt;
	margin-bottom: 2px;
	color: #626262;
	font-size: 65%;
	font-weight: normal;
	margin-left: 20px;
	background-color: #FBEDBB;
	padding: 7pt;
	background-image: url(/images/codebg.gif);
	font: 9pt "Courier New", Courier, mono;
	white-space: pre;
	width: 100%;
	/*overflow: auto;*/
	color: #990000;
	font-family: "Courier New", Courier, mono;
a:link    { text-decoration:none;      }
a:visited { text-decoration:none;      }
a:active  { text-decoration:underline; }
a:hover   { text-decoration:underline; }
.cpp-comment,.cs-comment,.vb-comment	{ COLOR: green; FONT-STYLE: italic; }
.cpp-keyword,.cs-keyword, .vb-function	{ COLOR: blue; }
.cpp-preprocessor, .cs-preprocessor		{ COLOR: navy; }
.cpp-string,.cs-string,.vb-string		{ COLOR: purple; }
.cpp-literal,.cs-literal,.vb-literal	{ COLOR: midnightblue }
.vb-keyword,.vb-statement	{ COLOR: blue; font-weight:normal;}
<body bgcolor="#FFFFFF" color=#000000>


<P>LiteZip.dll and LiteUnzip.dll are two Win32 Dynamic Link libraries. The former has functions to create a ZIP archive (ie, compress numerous files into a ZIP file). The latter has functions to extract the contents of a ZIP archive. The source code can also be compiled under Linux to create Linux shared libraries that are compatible with the Win32 DLLs.

<P>This project is largely based upon work by Lucian Wischik, who in turn based his work on gzip 1.1.4, zlib, and info-zip which are by by Jean-Loup Gailly and Mark Adler. Lucian's code has been reworked to be written in plain C, using only the Win32 API, and packaged into 2 DLLs. (Also some improvements to error-checking, some added functionality, and code-reduction/stream-lining was accomplished. Furthermore, Linux support was added).

<P>The primary benefits of these 2 DLLs are as follows:

<LI>You can unzip (extract the contents) from a zip archive that's in a diskfile, memory-buffer, or pipe. And, you can unzip those contents into a diskfile, memory-buffer, pipe, or even a combination of the preceding.

<P>The same applies to creating a zip archive. You can create the zip archive on disk, in memory, or to a pipe. And, the contents of this zip archive can come from diskfiles, memory-buffers, pipes, or even a combination of any/all of the preceding.

<P>Given this flexibility, you're not required to write out your files to a temporary directory before using them. One noteworthy feature is that you can <EM>unzip directly from an embedded resource</EM> into a memory buffer or into a diskfile, which is great for installers. Another useful feature is the ability to create your zip in dynamically growable memory backed by the system pagefile. (ie, You don't need to guess the allocation size of a memory buffer before zipping some stuff into that memory buffer. You can let the DLL grow the memory buffer on-the-fly, as needed, on your behalf).</LI>

<P><LI>The Zip and Unzip functions are each in their own DLL. So for example, if you need only to extract files from a ZIP archive, but not create ZIP archives, then you can use only LiteUnzip.dll and do not need to waste resources for code that you don't need (in LiteZip.dll).</LI>

<P><LI>The DLLs can be used by any program written in any language (that can load and call a standard DLL). This includes C or C++ compiled code, Visual Basic, etc. Although the source code for the DLLs is written in plain C, the compiled DLLs are included for those people who just want to use the DLLs with any language. A text file (Vb.txt) is supplied with function definitions to use the DLLs from Visual Basic.

<P>And an update to the DLL means that all programs using it automatically obtain the update without needing to be recompiled.</LI>

<P><LI> The DLLs themselves have a small footprint. For example, LiteZip.dll is only around 40K. One copy of a DLL's code can be shared among many running programs. Read-only data in the DLL is also shared (ie, some of the large "lookup tables"). On systems with limited memory, this can be a valuable feature.</LI>

<P><LI>The DLLs support password-based zip encryption.</LI>

<P><LI>The one set of DLLs support both unicode and ANSI strings. You don't need separate DLLs for unicode versus ANSI, nor do you need to recompile the DLLs.</LI>

<P><LI>The DLLs should work under all versions of Windows, including CE.</LI>

<P><LI>Having Linux versions of the DLLs means that you don't have to rewrite your app when porting to Linux.</LI>

<P><LI>All human readable strings are in each DLL's resources, facilitating the easy creation of non-english language versions of these DLLs.</LI>

<P><LI>The DLLs also support reading and writing GZIP format (single file only -- not a tar ball).</LI>

<P>The limitations of these DLLs are:
<UL><LI>Doesn't support disk-spanning. So, the DLLs are limited to working with files whose sizes are less than 2 gig.</LI>

<H2>Using the code in a C/C++ program</H2>

<P>To allow your C/C++ code to create a zip archive, add the file <CODE>litezip.lib</CODE> to your project, and <CODE><span class='cpp-preprocessor'>#include "LiteZip.h"</span></CODE> to your source code.

<P>To allow your C/C++ code to unzip an archive, add the file <CODE>liteunzip.lib</CODE> to the project and <CODE><span class='cpp-preprocessor'>#include "LiteUnzip.h"</span></CODE> to your source code.

<P>Zip and unzip can co-exist happily in a single application. Or, you can use only the one you need if you're trying to reduce size.

<P>Of course, you must distribute LiteZip.dll and/or LiteUnzip.dll with your application.

<P>The following code snippets show how to use zip/unzip. They use ANSI, but #define'ing UNICODE will use the unicode version of the functions instead. Error checking has been omitted for brevity.

<H3>Example 1 - create a zipfile (on disk) from existing files</H3>

<P>To take some existing files on disk, and create a zip archive on disk, do the following:

<P><LI>Call ZipCreateFile. You pass a handle to where you wish ZipCreateFile to supply an HZIP to you. You do not need to know the particulars of this handle. You'll simply pass it to other functions in LiteZip.dll just like you may pass a CreateFile() handle to ReadFile(). You also pass the filename for the zip archive you would like to create on disk. The name can include the full path such as "C:\My Directory\". If you wish to use encryption, you also pass the password string. If not using encryption, pass a 0 instead.

<P>If successful, ZipCreateFile will create an (empty) zip archive on disk, and fill in your HZIP handle.

<P><LI>Call ZipAddFile for each file you wish to add to the zip archive. You'll pass the HZIP handle supplied to you by ZipCreateFile, the filename of the file to be added to the archive, and the name you wish it to have inside of the zip archive. (You may wish to trim off some of the directories on the latter name if you want a relative directory hierarchy in the zip. Or you may wish to prepend directory names to create some directory hierarchy within the zip archive).

<P><LI>After you're done adding items to the zip archive, call ZipClose to finish up. You'll pass the HZIP handle supplied to you by ZipCreateFile.

<P>Here's an example of the above. Assume that we have two files on disk, named "simple.bmp" and "simple.txt". We wish to zip them up into a zip archive named "".

  <span class='cpp-preprocessor'>#include</span> &lt;Windows.h&gt;
  <span class='cpp-preprocessor'>#include</span> <span class='cpp-string'>"LiteZip.h"</span>

  HZIP hz;

  ZipCreateFile(&hz, <span class='cpp-string'>""</span>, <span class='cpp-literal'>0</span>);
  ZipAddFile(hz, <span class='cpp-string'>"simple.bmp"</span>);
  ZipAddFile(hz, <span class='cpp-string'>"simple.txt"</span>);

The downloaded example ZipFile contains a similiar example, with error-checking, and also dynamic linking to LiteZip.dll. (With dynamic linking, you don't add LiteZip.lib to your project. And LiteZip.dll is not loaded when your app first starts. It is loaded only when you call LoadLibrary).

<H3>Example 2 - unzip a zipfile (on disk) to files on disk</H3>

<P>To take a zip archive on disk, and unzip its contents to disk, do the following:

<P><LI>Call UnzipOpenFile. You pass a handle to where you wish UnzipOpenFile to supply an HUNZIP to you. You do not need to know the particulars of this handle. You'll simply pass it to other functions in LiteUnzip.dll. You also pass the filename for the zip archive you would like to unzip. The name can include the full path. If the zip is encrypted. you also pass the needed password string. If not using encryption, pass a 0 instead.

<P>If successful, UnzipOpenFile will open the zip archive on disk, and fill in your HUNZIP handle.

<P><LI>Call UnzipGetItem to determine how many items (files) are in the zip archive. You supply a ZIPENTRY struct to UnzipGetItem. (This is defined in LiteUnzip.h). You can allocate this struct using a memory function such as malloc, or declare it on the stack, or declare it as a global variable, etc. (If you allocate it, you're responsible for freeing it). Set the ZIPENTRY's Index field to -1 before you pass it to UnzipGetItem. You'll also pass the HUNZIP handle supplied to you by UnzipOpenFile.

<P>UnzipGetItem will set the ZIPENTRY's Index field to how many items are inside the zip archive.

<P><LI>Loop around calls to UnzipGetItem and UnzipItemToFile to extract each item from the archive, and save it on disk.

<P>To extract an item, you first set the ZIPENTRY's Index field to which item you wish to extract (where 0 is the first item, 1 is the second item, 2 is the third item, etc).

<P>Pass your ZIPENTRY, and the HUNZIP handle supplied by UnzipOpenFile, to UnzipGetItem. UnzipGetItem will fill in the ZIPENTRY with information about that item. This includes its name, its uncompressed size, its modification date, etc. If you want to extract only a particular item, rather than calling UnzipGetItem, you can fill in your ZIPENTRY's Name field with the desired item's name, and pass your ZIPENTRY to UnzipFindItem to fill in your ZIPENTRY with other information about that item.

<P>Finally, call UnzipItemToFile to extract that item to a disk file. Pass your ZIPENTRY, the HUNZIP handle supplied by UnzipOpenFile, and the filename you wish the item to be saved to. (You can use the ZIPENTRY's Name field if you want to use the same name it had within the archive). UnzipItemToFile will extract the item and save it to disk, creating any needed directories.

<P><LI>After you're done extracting items from the zip archive, call UnzipClose to finish up. You'll pass the HUNZIP handle supplied to you by ZipOpenFile.

<P>Here's an example of the above. Assume that we have a zip archive on disk named "". We'll extract all its items, using the same filenames as within the archive. No encryption is used.

  <span class='cpp-preprocessor'>#include</span> &lt;Windows.h&gt;
  <span class='cpp-preprocessor'>#include</span> <span class='cpp-string'>"LiteUnzip.h"</span>

  HUNZIP   huz;
  DWORD    numitems;

  ZipOpenFile(&huz, <span class='cpp-string'>""</span>, <span class='cpp-literal'>0</span>);

  ze.Index = (DWORD)<span class='cpp-literal'>-1</span>;
  UnzipGetItem(huz, &ze);
  numitems = ze.Index;

  <span class='cpp-keyword'>for</span> (ze.Index = <span class='cpp-literal'>0</span>; ze.Index < numitems; ze.Index++)
     UnzipGetItem(huz, &ze);
     UnzipItemToFile(huz, ze.Name, &ze);


The downloaded example UnzipFile contains a similiar example, with error-checking, and also dynamic linking to LiteUnzip.dll. (With dynamic linking, you don't add LiteUnzip.lib to your project. And LiteUnzip.dll is not loaded when your app first starts. It is loaded only when you call LoadLibrary).

<P>Here's an example of extracting only the item named "readme.txt" from the same zip archive:

  HUNZIP   huz;

  ZipOpenFile(&huz, <span class='cpp-string'>""</span>, <span class='cpp-literal'>0</span>);
  lstrcpy(, <span class='cpp-string'>"readme.txt"</span>);
  UnzipFindItem(huz, &ze, <span class='cpp-literal'>0</span>); <span class='cpp-comment'>// Pass a 1 for case-insensitive find</span>
  UnzipItemToFile(huz, ze.Name, &ze);

<H3>Example 3- unzip from resource directly into memory</H3>

<P>This technique is useful for small games, where you want to keep all data files bundled up inside the executable, but reduce their size by zipping them first. It may also be useful for an installer, where the files to be installed are zipped into an archive that is embedded in the installer exe's resource.

<P>Assume our project has a .RC file with the line<BR>
<CODE><span class='cpp-literal'>1</span> RCDATA <span class='cpp-string'>""</span></CODE><BR>
to embed the zipfile as a resource. Let's also assume that this zip archive contains an item named "sample.jpg", and we wish to unzip that one item into a memory buffer.

<P>The technique is very similiar to the above unzip example, except:

<P><LI>We call UnzipOpenBuffer instead of UnzipOpenFile. After all, the zip archive is not a separate zip file on disk. Rather, it is part of our EXE's resources. The third arg is the resource id number. We used the number 1 in our RCDATA statement above.</LI>

<P><LI>We call UnzipItemToBuffer instead of UnzipItemToFile. After all, we want to unzip the item to a memory buffer we create, rather than a file on disk. Note that UnzipFindItem fills in the ZIPENTRY's UncompressedSize field with the size of the buffer we'll need. So we allocate that with GlobalAlloc (or in C++ you can use new).</LI>

  HUNZIP   huz;
  <span class='cpp-keyword'>char</span>    *buffer;

  UnzipOpenBuffer(&huz, <span class='cpp-literal'>0</span>, <span class='cpp-literal'>1</span>, <span class='cpp-literal'>0</span>)

  lstrcpy(, <span class='cpp-string'>"sample.jpg"</span>);
  UnzipFindItem(huz, &ze, <span class='cpp-literal'>0</span>);

  buffer = (<span class='cpp-keyword'>char</span> *)GlobalAlloc(GMEM_FIXED, ze.UncompressedSize);

  UnzipItemToBuffer(huz, buffer, ze.UncompressedSize, &ze)

  <span class='cpp-comment'>// Here you would do something with the contents of buffer.</span>


The downloaded example UnzipResource shows how an installer EXE may unzip the entire contents of an archive embedded in its resources.

<H3>Other examples</H3>

<P>You can also zip up some existing file into an archive that is created in a memory buffer. You can either supply your own memory buffer (and make sure its big enough to accomodate the resulting archive), or you can simply let LiteZip.dll allocate the buffer from system paged memory. In the latter case, the DLL can automatically grow the buffer on-the-fly as needed.

<P>Furthermore, you can add the contents of some memory buffer

<P>The downloaded example ZipMemory shows the zipping the contents of a memory buffers into an archive created in memory. The example lets the DLL allocate system paged memory for the resulting archive. It's similiar to the zip example above except:

<P><LI>We call zipCreateBuffer instead of ZipCreateFile. After all, the zip archive is not going to be created on disk. Rather, it is going to be created in memory. The third arg is the maximum limit to the growable size. You can make this a very large number because memory will be only reserved, but not actually committed, until needed.</LI>

<P><LI>We call ZipAddBuffer instead of ZipAddFile. After all, we want to zip up a memory buffer's contents rather than some existing file on disk.</LI>

<P><LI>Because we let the DLL allocate growable memory, rather than supplying our own buffer, we have to call ZipGetMemory to retrieve the buffer that the DLL creates the zip archive in. We don't need to call ZipClose because ZipGetMemory does that for us. We're also responsible for freeing that memory.</LI>

<P>Sometimes, you may need to zip up some data with the resulting archive not having a ZIP header, nor ZIP "central directory" in it. I'll refer to this as a "raw" zip. For example, this is the case with a compressed ID3 tag. For this purpose, LiteZip offers a few functions to add data to a raw archive: ZipAddFileRaw, ZipAddHandleRaw, ZipAddPipeRaw, and ZipAddBufferRaw. Only one item can be added to such an archive. To later unzip the data item from this archive, you will need to use one of LiteUnzip's functions to open a raw archive: UnzipOpenFileRaw, UnzipOpenBufferRaw, or nzipOpenHandleRaw. You can then unzip the one item by calling UnzipGetItem, but first you will have to know both the compressed size of the archive, and also the size of the item when it is compressed. You stuff these two values in the ZIPENTRY's CompressedSize and UncompressedSize fields, respectively, before you call UnzipGetItem. The example ZipMemoryRaw shows how to create a raw archive. And the example UnzipMemRaw shows how to extract the one item from that same raw archive.


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Jeff Glatt
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 8 Aug 2008
Article Copyright 2006 by Jeff Glatt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid