Click here to Skip to main content
Click here to Skip to main content

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.

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
No Biography provided

Comments and Discussions

 
GeneralSincere thanks! PinmemberMember 472261230-Oct-08 2:43 
GeneralUncompress with missing file Pinmemberneilsolent24-May-08 23:36 
GeneralRe: Uncompress with missing file Pinmemberneilsolent25-May-08 0:09 
QuestionRecommend similar class for multiple files? PinmemberHumanJHawkins11-Aug-06 18:00 
QuestionUnicode compliant? PinmemberMartyP9-Sep-05 14:47 
GeneralCompress more than one file Pinmemberjerko10-Oct-03 2:33 
GeneralRe: Compress more than one file PinmemberK+K12-Dec-06 16:42 
GeneralRead/write Zip files in C# PinmemberSalvador Torres24-Sep-03 6:46 
GeneralDoes not compile in VS.NET PinmemberEugene Plokhov6-Jun-03 18:57 
GeneralPerhaps you could elaborate more... PinmemberJason Henderson25-Apr-03 2:56 
GeneralRe: Perhaps you could elaborate more... PinmemberLarryLeonard25-Apr-03 4:31 
GeneralCompress more than one file PinmemberJerko10-Oct-03 1:49 
GeneralRe: Compress more than one file PinmemberLarryLeonard10-Oct-03 1:57 
GeneralRe: Compress more than one file Pinmemberjerko10-Oct-03 2:35 
GeneralWorth Posting... Pinmemberpatnsnaudy24-Apr-03 11:15 
Generalpoor Pinmemberdog_spawn24-Apr-03 8:34 
GeneralRe: Troll PinmemberLarryLeonard24-Apr-03 8:46 
GeneralRe: Troll Pinmemberdog_spawn24-Apr-03 9:36 
GeneralRe: You see,Timmy... PinmemberLarryLeonard24-Apr-03 9:58 
GeneralRe: You see,Timmy... Pinmemberdog_spawn24-Apr-03 10:29 
GeneralRe: Okay... PinmemberLarryLeonard24-Apr-03 11:39 
GeneralGive us a break, dog_spawn Pinmemberthorek25-Apr-03 4:02 
GeneralRe: Give us a break, dog_spawn Pinmemberdog_spawn26-Apr-03 12:35 
GeneralRe: Troll PinmemberRavi Bhavnani24-Apr-03 12:46 
GeneralRe: Troll PinmemberFrank De prins25-Apr-03 0:39 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 24 Apr 2003
Article Copyright 2003 by LarryLeonard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid