Click here to Skip to main content
13,258,055 members (50,099 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 23 Apr 2003

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

, 23 Apr 2003
Rate this:
Please Sign up or sign in to vote.
Create a WinZip-readable ZIP file in three lines of your MFC code.


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.)


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.


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralSincere thanks! Pin
Member 472261230-Oct-08 3:43
memberMember 472261230-Oct-08 3:43 
GeneralUncompress with missing file Pin
neilsolent25-May-08 0:36
memberneilsolent25-May-08 0:36 
GeneralRe: Uncompress with missing file Pin
neilsolent25-May-08 1:09
memberneilsolent25-May-08 1:09 
QuestionRecommend similar class for multiple files? Pin
HumanJHawkins11-Aug-06 19:00
memberHumanJHawkins11-Aug-06 19:00 
QuestionUnicode compliant? Pin
MartyP9-Sep-05 15:47
memberMartyP9-Sep-05 15:47 
GeneralCompress more than one file Pin
jerko10-Oct-03 3:33
memberjerko10-Oct-03 3:33 
GeneralRe: Compress more than one file Pin
K+K12-Dec-06 17:42
memberK+K12-Dec-06 17:42 
GeneralRead/write Zip files in C# Pin
Salvador Torres24-Sep-03 7:46
memberSalvador Torres24-Sep-03 7:46 
GeneralDoes not compile in VS.NET Pin
Eugene Plokhov6-Jun-03 19:57
memberEugene Plokhov6-Jun-03 19:57 
GeneralPerhaps you could elaborate more... Pin
Jason Henderson25-Apr-03 3:56
memberJason Henderson25-Apr-03 3:56 
GeneralRe: Perhaps you could elaborate more... Pin
LarryLeonard25-Apr-03 5:31
memberLarryLeonard25-Apr-03 5:31 
GeneralCompress more than one file Pin
Jerko10-Oct-03 2:49
memberJerko10-Oct-03 2:49 
GeneralRe: Compress more than one file Pin
LarryLeonard10-Oct-03 2:57
memberLarryLeonard10-Oct-03 2:57 
GeneralRe: Compress more than one file Pin
jerko10-Oct-03 3:35
memberjerko10-Oct-03 3:35 
GeneralWorth Posting... Pin
patnsnaudy24-Apr-03 12:15
memberpatnsnaudy24-Apr-03 12:15 
Generalpoor Pin
dog_spawn24-Apr-03 9:34
memberdog_spawn24-Apr-03 9:34 
GeneralRe: Troll Pin
LarryLeonard24-Apr-03 9:46
memberLarryLeonard24-Apr-03 9:46 
GeneralRe: Troll Pin
dog_spawn24-Apr-03 10:36
memberdog_spawn24-Apr-03 10:36 
GeneralRe: You see,Timmy... Pin
LarryLeonard24-Apr-03 10:58
memberLarryLeonard24-Apr-03 10:58 
GeneralRe: You see,Timmy... Pin
dog_spawn24-Apr-03 11:29
memberdog_spawn24-Apr-03 11:29 
GeneralRe: Okay... Pin
LarryLeonard24-Apr-03 12:39
memberLarryLeonard24-Apr-03 12:39 
GeneralGive us a break, dog_spawn Pin
thorek25-Apr-03 5:02
memberthorek25-Apr-03 5:02 
GeneralRe: Give us a break, dog_spawn Pin
dog_spawn26-Apr-03 13:35
memberdog_spawn26-Apr-03 13:35 
GeneralRe: Troll Pin
Ravi Bhavnani24-Apr-03 13:46
memberRavi Bhavnani24-Apr-03 13:46 
GeneralRe: Troll Pin
Frank De prins25-Apr-03 1:39
memberFrank De prins25-Apr-03 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 24 Apr 2003
Article Copyright 2003 by LarryLeonard
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid