Zip/UnZip C/C++ Source Code is a simple way to add zip/unzip
functionality to your windows programs, both win32 and windows-ce. Zip/UnZip
C/C++ source code package includes sample projects to show how to add zip/unzip
functionality to your programs.
Clean packaging: There's one pair of files zip.cpp, zip.h to add to your project if you want zip. Another pair unzip.cpp, unzip.h if you want unzip (or both if you want both!). There are no additional libraries or DLLs to worry about.
Clean API: Most other APIs around zip/unzip are terrible. This one is the best. The API is short, clean, and in a familiar Win32 style. Most other APIs wrap things up in classes, which is ugly overkill for such a small problem and always turn out to be too inflexible. Mine doesn't. See the code snippets below.
Flexibility: With this code, you can unzip from a zip that's in a disk file, memory-buffer, pipe. You can unzip into a disk file, memory-buffer or pipe. The same for creating Zip files. This means that at last you don't need to write out your files to a temporary directory before using them! One noteworthy feature is that you can unzip directly from an embedded resource into a memory buffer or onto a disk file, which is great for installers. Another is the ability to create your Zip in dynamically growable memory backed by the system page file. Despite all this power, the API remains clean and simple. The power didn't come from just writing wrappers around other people's code. It came from restructuring the internals of zlib and info-zip source code. My code is unique in what it does here.
Encryption: This version supports password-based Zip encryption. Passwords are absent from many other Zip libraries, including gzip.
Unicode: This version supports Unicode filenames.
Windows CE: This version works as it is under Windows CE. No need to alter makefiles or #defines, or worry about compatibility of any LIB/DLL.
Zip/UnZip C/C++ Source Code can be compiled in Microsoft VC++ 6, 8, 9, 10, 11, 12, 13 and later IDEs.
Zip/UnZip C/C++ Source Code is a best selling Zip compression component with loads of features and extremely easy to use.
* ZIP64 format extensions supported. No limitations on size for files within a .zip, or the total size of a .zip.
* No limitations on the number of files that may be contained within a single .zip (this is a ZIP64 feature).
* Can read/write Zips with WinZip-compatible AES strong encryption.
* Supports Unicode filename zip file format extensions.
* Events for zip/unzip progress monitoring.
* Zip an entire directory tree.
* Append entries to a Zip from in-memory data.
* Create or open in-memory Zips.
* Create self-extracting executables.
* Create password-protected Zip files.
* Unzip only files that match a filename pattern.
* Option to discard path information when zipping.
* Option to append a path prefix when zipping.
* Option to create self-extracting EXEs with or without interfaces.
* Ability to customize icon and interface when creating an EXE.
* Ability to specify an auto-run file when creating an EXE.
* Ability to specify a pre-defined unzip directory for an EXE.
* Handles Unicode filenames.
* Provides in-memory access to compressed or uncompressed data.
* Easy FirstEntry/NextEntry methods for iterating over a Zip.
* Can embed Zips in your own EXEs.
* OpenMyEmbedded opens the Zip that has been embedded in the caller's EXE.
* Set exclusion patterns when zipping.
* Option to specify a target directory and discard path information when unzipping.
* Can unzip only files that are newer.
* Read/write comments within a Zip.
* Ability to inflate directly to in-memory byte array or string.
* Includes a Bz2 object/class to create or decompress the .bz2 file format.
* Includes a GZip object/class to create or decompress the .gz file format.
* Includes a Unix compress object/class to create or decompress the .Z file format using LZW compression.
* Thread safe.
Using the Code
To add zip functionality to your code, add the file zip.cpp to your project, and #include "zip.h" to your source code.
Similarly for unzipping, add the file unzip.cpp to the project and #include "unzip.h" to your source code. Zip and unzip can co-exist happily in a single application. Or you can omit one or the other if you're trying to save space.
The following code snippets show how to use zip/unzip. They are taken from one of the demo applications included in the download. It also has project files for Visual Studio .NET and Borland C++ Builder6 and Embedded Visual C++ 3. The code snippets here use ASCII. But the functions all take arguments of type TCHAR* rather than char*, so you can use it fine under Unicode.
Example 1 - Create a Zip File from Existing Files
// We place the file "simple.bmp" inside, but inside
// the zipfile it will actually be called "znsimple.bmp".
// Similarly the textfile.
HZIP hz = CreateZip("simple1.zip",0);
Example 2 - Unzip a Zip File Using the Names It Has Inside It
HZIP hz = OpenZip("D:\\simple1.zip",0);
ZIPENTRY ze; GetZipItem(hz,-1,&ze);
// -1 gives overall information about the zipfile
for (int zi=0; zi[numitems; zi++)
ZIPENTRY ze; GetZipItem(hz,zi,&ze); // fetch individual details
UnzipItem(hz, zi, ze.name); // e.g. the item's name.
Example 3- Unzip from Resource Directly into Memory
This technique is useful for small games, where you want to keep all resources bundled up inside the executable, but restricting the size.
Suppose we used a .rc with 1 RCDATA "file.zip" to embed the zip file as a resource.
HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
HANDLE hglob = LoadResource(hInstance,hrsrc);
void *zipbuf = LockResource(hglob);
unsigned int ziplen = SizeofResource(hInstance,hrsrc);
hz = OpenZip(zipbuf, ziplen, 0);
ZIPENTRY ze; int i; FindZipItem(hz,"sample.jpg",true,&i,&ze);
// that lets us search for an item by filename.
// Now we unzip it to a membuffer.
char *ibuf = new char[ze.unc_size];
UnzipItem(hz,i, ibuf, ze.unc_size);
// note: no need to free resources obtained through Find/Load/LockResource
Example 4 - Unzip Chunk by Chunk to a membuffer
Normally when you call UnzipItem(...), it gives the return-code ZR_OK. But if you gave it too small a buffer so that it couldn't fit it all in, then it returns ZR_MORE.
unsigned long totsize=0;
zr = UnzipItem(hz,i, buf,1024);
unsigned long bufsize=1024;
//maybe write the buffer to a disk file here
How to add/remove files from an existing Zip file? The zip_utils currently only allows you to OpenZip() for unzipping, or CreateZip() for adding, but don't allow you to mix the two. To modify an existing zip (e.g.: adding or removing a file), you need to create a new zip and copy all the existing items from the old into the new. One of the included examples, "modify", shows how to do this. It defines two functions:
ZRESULT RemoveFileFromZip(const TCHAR *zip, const TCHAR *name);
ZRESULT AddFileToZip(const TCHAR *zip, const TCHAR *name, const TCHAR *fn);
// eg. AddFileToZip("c:\\archive.zip","znsimple.txt","c:\\docs\\file.txt");
// If the zipfile already contained that thing (case-insensitive), it is removed.
// These two functions are defined in "modify.cpp"
Tags: zip library, unzip library, zip source code, unzip source code, C++ zip, C++ unzip, zip decompress, compress zip, zip lib, lib archive, zip archive, add file to zip, remove file from zip, libzip