Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Easy Way to Read/Write Zip-Compatible Files Under MFC

0.00/5 (No votes)
23 Apr 2003 1  
Create a WinZip-readable ZIP file in three lines of your MFC code.

Introduction

Wouldn't it be nice if there was an MFC-friendly class that would let you create ZIP files that could be read by PKZip/WinZip? If this class were trivial to use? If you didn't have to become an expert on "deflate" and the arcana of Huffman encoding just to get it compile? If you could just plug the thing in, and it would work, so you could get on with your life? If you didn't have to spend a week googleing for "zlib (compress OR deflate) (zip OR winzip)", and then try to decipher a conversation between two Russian mathematics professors?

Well, here's your answer! The MyCompress class reads/writes a single file from/to a PKZip-compatible ZIP file. No, it doesn't support more than one file in an archive, spanning disks, encryption, or anything else. It does, however, compile cleanly under VC6 at Warning Level 4.

How to add the MyCompress class to your project

Here are the steps; follow them carefully:

  • Add MyCompress.cpp and MyCompress.h to your project in the usual folders.
  • Add MyCompressZip.c to your project, and use the "Project, Settings" menu item, "C/C++" tab, "Precompiler Headers" category, to set this one file to "Not using precompiled headers".
  • Add MyUncompressZip.c to your project, and use the "Project, Settings" menu item, "C/C++" tab, "Precompiler Headers" category, to set this one file to "Not using precompiled headers".
  • Be sure MyCompressZlib.lib is in your linker's include path, and the files MyCompressZlib.h and MyCompressZconf.h are where the compiler can find them.
  • Add /NODEFAULTLIB:LIBCD to your linker options if necessary. The linker will bark if it's necessary.

How to use the MyCompress class

To compress a file, all you say is:

#include "MyCompress.h"

. . . 
MyCompress mc;
mc.Compress(sUncompPath, sCompPath);

The CStrings sUncompPath and sCompPath are supposed to contain fully-qualified paths, like, c:\\Program Files\\Mumble\\TextFile.txt. The double-backslashes are not because I'm a bad typist. Also, the Compress( ) method returns a bool to indicate success or failure; if I were you, I'd check it.

(Uncompressing a file is just as trivial, and is left as an exercise for the reader. See the comments at the top of the MyCompress.h file if you can't guess the method's name.)

Disclaimer

I wrote virtually none of this code. The original authors' copyrights remain intact in their files; however, I did rename their files to begin with "MyCompress" just to make it easier to group all the files together. The word "MyCompress" should not be construed as ownership, for Pete's sake: I couldn't begin to write code like this in a million years. I am good, however, at hunting things down, beating them into submission until they compile and link, and putting a simple wrapper around them.

The original authors

The geniuses who wrote this code deserve an enormous amount of credit. All I did was make their work really easy to use in an MFC project.

  • The 'zlib' general purpose compression library, version 1.1.4, March 11th, 2002, is Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler.
  • The 'IO for compress/uncompress .zip files using zlib' source code, Version 0.15 beta, Mar 19th, 1998, is Copyright (C) 1998 Gilles Vollant.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here