Skip to main content
Email Password   helpLost your password?

zipping and unzipping in action!

Introduction

This source code shows how to add zip/unzip functionality to your programs. Lots of people have written their own wrappers around Zip, and indeed there are several articles on CodeProject that are based on earlier versions of my own code. How is this version different?

At its core, my code uses zlib and info-zip. See article end for acknowledgements.

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);
  ZipAdd(hz,"znsimple.bmp",  "simple.bmp");
  ZipAdd(hz,"znsimple.txt",  "simple.txt");
  CloseZip(hz);

Example 2 - unzip a Zip file using the names it has inside it

  HZIP hz = OpenZip("\\simple1.zip",0);
  ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
  // -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.

  }
  CloseZip(hz);

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);
  ...
  delete[] ibuf;
  CloseZip(hz);
  // 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.

  char buf[1024]; ZRESULT zr=ZR_MORE; unsigned long totsize=0;
  while (zr==ZR_MORE)
  { zr = UnzipItem(hz,i, buf,1024);
    unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
    ... maybe write the buffer to a disk file here
    totsize+=bufsize;
  }

Common Questions

STRICT? I think you should always compile with STRICT (in project-settings/preprocessor/defines), and full warnings turned on. Without STRICT, the HZIP handle becomes interchangeable with all other handles.

How to show a progress dialog? One of the included examples, "progress", shows how to do this.

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"

"fatal error C1010: unexpected end of file while looking for precompiled header directive". To fix this, select zip.cpp and unzip.cpp and change Project > Settings > C++ > PrecompiledHeaders to NotUsingPrecompiledHeaders.

Discussion

Efrat says: "I think the design is very bad", and so objects when I say that my API is clean and others are not. (Actually, he says my documentation is the most conceited he's seen and my design is the worst that he's seen!) I've reproduced his comments here, with my responses, so you can make a more informed decision whether to use my library.

In general, Efrat wants code to be a clean extensible framework. I don't; I want small compact code that works fine as it is. Furthermore, I think that "framework-isation" is the biggest source of bugs and code overruns in the industry.

Acknowledgements

This version of article was updated on 28th July 2005. Many thanks to the readers at CodeProject who found bugs and contributed fixes to an earlier version. There was one terrible bug where, after a large file had been unzipped, the next one might not work. Alvin77 spotted this bug.

My work is a repackaged form of extracts from the zlib code available at www.gzip.org by Jean-Loup Gailly and Mark Adler and others. Also from the info-zip source code at www.info-zip.org. Plus a bunch of my own changes. The original source code can be found at the two mentioned websites. Also the original copyright notices can be found there, and also inside the files zip.cpp and unzip.cpp of my code.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralI found a bug,you check it Pin
yyyzlf
16:08 17 Nov '09  
GeneralProblem with password Pin
Grigory Novikov
8:50 5 Nov '09  
GeneralCreating directories in zip? Pin
borisgz
7:27 21 Oct '09  
GeneralRe: Creating directories in zip? Pin
borisgz
8:35 21 Oct '09  
QuestionZip Directory Tree Pin
mike wainwright
23:29 14 Oct '09  
GeneralNew requirement Pin
Member 3420509
22:33 5 Oct '09  
Generalwhere are new sources? Pin
YuraQ
6:49 25 Sep '09  
GeneralThanks Pin
Ross Peralta
11:55 21 Sep '09  
GeneralZR_FLATE problem?? :( Pin
Arrin
6:52 17 Sep '09  
GeneralNewbie to c++, many errors Pin
sarcasteak
11:11 14 Sep '09  
GeneralMany Error LNK2005 Pin
Arrin
6:10 14 Sep '09  
GeneralMnior timestamp bug Pin
boris_l
8:37 12 Aug '09  
GeneralBUG (and fix) Pin
Sylvester Ziolkowski
21:41 22 Jul '09  
GeneralRe: BUG (and fix) Pin
maksymchuk
0:08 4 Aug '09  
QuestionWhat License ? Pin
CFC376
19:16 13 Jul '09  
AnswerRe: What License ? Pin
ljw1004
6:41 14 Jul '09  
GeneralRe: What License ? Pin
CFC376
21:18 14 Jul '09  
GeneralUnicode issue Pin
maksymchuk
7:34 6 Jul '09  
JokeThank your help!!! Pin
Simon Sung
0:34 1 Jul '09  
GeneralOOP Pin
barneyman
15:55 8 Jun '09  
GeneralRe: OOP Pin
ljw1004
17:01 8 Jun '09  
GeneralRe: OOP Pin
barneyman
18:08 8 Jun '09  
GeneralRe: OOP Pin
barneyman
18:14 8 Jun '09  
GeneralRe: OOP Pin
ljw1004
18:40 8 Jun '09  
GeneralRe: OOP Pin
barneyman
19:52 8 Jun '09  


Last Updated 1 Aug 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009