Click here to Skip to main content
Licence 
First Posted 23 Apr 2003
Views 96,156
Bookmarked 30 times

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

By | 23 Apr 2003 | Article
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

About the Author

LarryLeonard

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSincere thanks! PinmemberMember 47226122:43 30 Oct '08  
GeneralUncompress with missing file Pinmemberneilsolent23:36 24 May '08  
GeneralRe: Uncompress with missing file Pinmemberneilsolent0:09 25 May '08  
QuestionRecommend similar class for multiple files? PinmemberHumanJHawkins18:00 11 Aug '06  
QuestionUnicode compliant? PinmemberMartyP14:47 9 Sep '05  
GeneralCompress more than one file Pinmemberjerko2:33 10 Oct '03  
GeneralRe: Compress more than one file PinmemberK+K16:42 12 Dec '06  
GeneralRead/write Zip files in C# PinmemberSalvador Torres6:46 24 Sep '03  
GeneralDoes not compile in VS.NET PinmemberEugene Plokhov18:57 6 Jun '03  
GeneralPerhaps you could elaborate more... PinmemberJason Henderson2:56 25 Apr '03  
GeneralRe: Perhaps you could elaborate more... PinmemberLarryLeonard4:31 25 Apr '03  
GeneralCompress more than one file PinmemberJerko1:49 10 Oct '03  
GeneralRe: Compress more than one file PinmemberLarryLeonard1:57 10 Oct '03  
GeneralRe: Compress more than one file Pinmemberjerko2:35 10 Oct '03  
GeneralWorth Posting... Pinmemberpatnsnaudy11:15 24 Apr '03  
Generalpoor Pinmemberdog_spawn8:34 24 Apr '03  
GeneralRe: Troll PinmemberLarryLeonard8:46 24 Apr '03  
GeneralRe: Troll Pinmemberdog_spawn9:36 24 Apr '03  
GeneralRe: You see,Timmy... PinmemberLarryLeonard9:58 24 Apr '03  
GeneralRe: You see,Timmy... Pinmemberdog_spawn10:29 24 Apr '03  
GeneralRe: Okay... PinmemberLarryLeonard11:39 24 Apr '03  
GeneralGive us a break, dog_spawn Pinmemberthorek4:02 25 Apr '03  
GeneralRe: Give us a break, dog_spawn Pinmemberdog_spawn12:35 26 Apr '03  
GeneralRe: Troll PinmemberRavi Bhavnani12:46 24 Apr '03  
GeneralRe: Troll PinmemberFrank De prins0:39 25 Apr '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 24 Apr 2003
Article Copyright 2003 by LarryLeonard
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid