Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
3.11/5 (22 votes)
23 Apr 20033 min read 139.5K   5.1K   33   30
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSincere thanks! Pin
Member 472261230-Oct-08 2:43
Member 472261230-Oct-08 2:43 
GeneralUncompress with missing file Pin
neilsolent24-May-08 23:36
neilsolent24-May-08 23:36 
Great post, very easy to use.

One problem I have - if I run Uncompress() and the file to be uncompressed does not exist - instead of getting some kind of error message / exception - I find the thread seems to just hang !

Do you get the same behaviour ?

Whilst I endeavour to ensure my program only uncompresses files that exist Unsure | :~ it is good programming practice to expect the unexpected - and another process beyond my control *may* have deleted the file in exceptional circumstances.

thanks,
Neil


cheers,
Neil

GeneralRe: Uncompress with missing file Pin
neilsolent25-May-08 0:09
neilsolent25-May-08 0:09 
QuestionRecommend similar class for multiple files? Pin
HumanJHawkins11-Aug-06 18:00
HumanJHawkins11-Aug-06 18:00 
QuestionUnicode compliant? Pin
MartyP9-Sep-05 14:47
MartyP9-Sep-05 14:47 
GeneralCompress more than one file Pin
jerko10-Oct-03 2:33
jerko10-Oct-03 2:33 
GeneralRe: Compress more than one file Pin
K+K12-Dec-06 16:42
K+K12-Dec-06 16:42 
GeneralRead/write Zip files in C# Pin
Salvador Torres24-Sep-03 6:46
Salvador Torres24-Sep-03 6:46 
GeneralDoes not compile in VS.NET Pin
Eugene Plokhov6-Jun-03 18:57
Eugene Plokhov6-Jun-03 18:57 
GeneralPerhaps you could elaborate more... Pin
Jason Henderson25-Apr-03 2:56
Jason Henderson25-Apr-03 2:56 
GeneralRe: Perhaps you could elaborate more... Pin
LarryLeonard25-Apr-03 4:31
LarryLeonard25-Apr-03 4:31 
GeneralCompress more than one file Pin
jerko10-Oct-03 1:49
jerko10-Oct-03 1:49 
GeneralRe: Compress more than one file Pin
LarryLeonard10-Oct-03 1:57
LarryLeonard10-Oct-03 1:57 
GeneralRe: Compress more than one file Pin
jerko10-Oct-03 2:35
jerko10-Oct-03 2:35 
GeneralWorth Posting... Pin
patnsnaudy24-Apr-03 11:15
patnsnaudy24-Apr-03 11:15 
Generalpoor Pin
dog_spawn24-Apr-03 8:34
dog_spawn24-Apr-03 8:34 
GeneralRe: Troll Pin
LarryLeonard24-Apr-03 8:46
LarryLeonard24-Apr-03 8:46 
GeneralRe: Troll Pin
dog_spawn24-Apr-03 9:36
dog_spawn24-Apr-03 9:36 
GeneralRe: You see,Timmy... Pin
LarryLeonard24-Apr-03 9:58
LarryLeonard24-Apr-03 9:58 
GeneralRe: You see,Timmy... Pin
dog_spawn24-Apr-03 10:29
dog_spawn24-Apr-03 10:29 
GeneralRe: Okay... Pin
LarryLeonard24-Apr-03 11:39
LarryLeonard24-Apr-03 11:39 
GeneralGive us a break, dog_spawn Pin
thorek25-Apr-03 4:02
thorek25-Apr-03 4:02 
GeneralRe: Give us a break, dog_spawn Pin
dog_spawn26-Apr-03 12:35
dog_spawn26-Apr-03 12:35 
GeneralRe: Troll Pin
Ravi Bhavnani24-Apr-03 12:46
professionalRavi Bhavnani24-Apr-03 12:46 
GeneralRe: Troll Pin
Frank De prins25-Apr-03 0:39
Frank De prins25-Apr-03 0: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.