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

CGZip, a C++ wrapper for gzip methods

By , 9 Dec 2002
 

Description

This documents present CGZip, C++ class wrapper for the gzip methods, which are included in the zlib library. The intention of this class is to have a simple class for zipping-unzipping buffers.

The main features of the class are:

  • Compress, decompress LPCTSTR to file
  • Compress, decompress memory buffer to file
  • Non-MFC
  • Hides zlib so you don't have to distribute the zlib headers
  • UNICODE compliant,

Examples

In the examples, we shall consider the following code snippet to be present on top:

// CGZip is in the zlib namespace
using namespace zlib;
CGZip gzip;

Zipping a memory buffer or a string

First of all, let's open a file for writing:

if(!gzip.Open(_T("myFile.gz"), CGZip::ArchiveModeWrite)))
{
        // the file could not be opened...
}

As you can see, Open returns true if success and false otherwise. All the methods (almost) have this behavior.

Now we can send data and strings to the file:

Strings:

LPCTSTR szString = _T("This is a test string\n");
// writing string
gzip.WriteString(szString);

Buffers:

void* pBuffer; // a memory buffer
int len;       // size in bytes of the memory buffer
// writing buffer
gzip.WriteBuffer(pBuffer,len);

When done, you must close the file:

gzip.Close();

Note that if gzip goes out of scope, it is automatically closed.

Unzipping a buffer or a string

The reading behaves pretty much like the writing. First of all, open a file for reading:

if(!gzip.Open(_T("myFile.gz"), CGZip::ArchiveModeRead)))
{
        // the file could not be opened...
}

Now, you can read a buffer with fixed length or the whole file:

The whole file:

void* pBuffer=NULL;
int len=0;
// reads and unzip the hole file, len contains the resulting size of pBuffer
gzip.ReadBuffer(&pBuffer,&len);
// pBuffer now contains len read bytes
...
// don't forget to delete pBuffer
if (pBuffer)
   delete[] pBuffer;

A fixed length buffer:

  int len;
char* pBuffer[len];
// reads and unzip the hole file, len contains the resulting size of pBuffer
gzip.ReadBufferSize(pBuffer,len);

Class reference

A detailed class reference is available as compressed HTML (generated by Doxygen).

GZip license

This piece of code is totally, utterly free for commercial and non-commercial use. However, it uses zlib so you should check the zlib license before using it. It is included in the source code but here it is:

//zlib.h -- interface of the 'zlib' general purpose compression library
//version 1.1.4, March 11th, 2002

//Copyright (C) 1995-2002 Jean-loup Gailly and Mark Adler

//This software is provided 'as-is', without any express or implied
//warranty.  In no event will the authors be held liable for any damages
//arising from the use of this software.

//Permission is granted to anyone to use this software for any purpose,
//including commercial applications, and to alter it and redistribute it
//freely, subject to the following restrictions:

//1. The origin of this software must not be misrepresented; you must not
//   claim that you wrote the original software. If you use this software
//   in a product, an acknowledgment in the product documentation would be
//   appreciated but is not required.
//2. Altered source versions must be plainly marked as such, and must not be
//   misrepresented as being the original software.
//3. This notice may not be removed or altered from any source distribution.

//Jean-loup Gailly        Mark Adler
//jloup@gzip.org          madler@alumni.caltech.edu


//The data format used by the zlib library is described by RFCs (Request for
//Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
//(zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).

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

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionerror LNK2019:member_R13_20-Feb-12 1:27 
use with #pragma comment(lib,"zlib/libs/zlib")
 
have
xxxxxx.obj : error LNK2019: public: bool __thiscall zlib::CGZip::Open(wchar_t const *,enum zlib::CZipper::EArchiveMode)" (?Open@CGZip@zlib@@QAE_NPB_WW4EArchiveMode@CZipper@2@@Z)"
xxxxxx.obj : error LNK2001: public: virtual bool __thiscall zlib::CGZip::WriteString(wchar_t const *)" (?WriteString@CGZip@zlib@@UAE_NPB_W@Z)"

AnswerRe: error LNK2019:member_R13_20-Feb-12 2:58 
fix it - copy gzip.cpp and gzip.h to my project.
At first I thought I could get from the library of a class CGZip )
GeneralMy vote of 5membermanoj kumar choubey15-Feb-12 23:16 
Nice
GeneralMy vote of 5memberchaau3-Nov-11 14:48 
I like it. Its very well written, and I am going to use it in my project
Questionunzip gzip data in memorymemberrmusiy28-Jun-11 5:16 
Can I use CGZip class to unzip gzip data in memory? For example i get gzip data from somewhere (HTTPS request in my case). I can not save data to file because security reasons.
GeneraldirectorymemberJohnWallis4227-Aug-10 16:30 
There are two file in an archive and I need to get to one of them.
How can I do this?
QuestionWhat is the point of this?memberThisIsANameOK19-Nov-09 17:01 
This is by-far not unicode compliant. I am very surprised no one has brought this up.
 
Try opening a file with Unicode characters in the file name. It fails.
 
When you call magical "Wide to Mutibyte" API, you lose details about the wide string.
 
My work-around for this is to call GetShortPathName() on the filename before sending to gZip. GetShortPathName() will convert the unicode characters to short-path syntax so when you use your Wide2MB API it will return a correct path.
 
Obviously for writing, this can be a problem, but i am sure you can figure it out.
QuestionUnZipping problemmemberMember 264226931-Oct-08 3:38 
Hi Jonathan,
 
I am using the GZipWrapper in my MFC application. When i am trying to unzip the output text is appended at the end some I'I'I'I'I'I'I'I'I'I'..................
 
I am unable to trance where is the problem....
 
Can you help on this issue.
 
Thanks&Regards,
 
Ravi Sankar,
ravi_yadavalli@yahoo.co.in
 
ravi sankar

GeneralUncompressed != plain textmemberMartin Beckett13-Nov-07 15:45 
Note setting nocompression does not write plain text files.
About every 64K there is a block of binary to mark a new buffer.
 

GeneralLimited supportmemberkuroyume016120-Dec-06 22:36 
I agree with the Linux guy - I need support that works with Windows and MacOS (CodeWarrior and XCode). This is a limited solution that is only good for Windows and also expects that you are using Windows-specific code (LPCTSTR). I'm using a C++ plugin SDK - and I am definitely not using anything Windows-specific (ever!).
 
Why does zlib live in the 80s/early-90s? Do they not think that filesystems use UNICODE and other multibyte character systems for file/folder naming these days?
 
I'll keep looking for a real solution...
GeneralUsing CGZip on linuxmemberktbstudio24-Oct-06 7:22 
Hello, my question is, how can i compile the cgzip on linux?
 
It needs atl what is in windows, but i don't found atl in linux.
GeneralHave a fatal erro:LNK1104 cannot open file "zlib.lib"memberluoyi_iim2-Jan-06 1:53 
when I have done follow the introduction and to link my project there have an erro "fatal erro:LNK1104 cannot open file "zlib.lib".I don't know how to solve it.
QuestionWhere is unzip-on-the-fly functionality?sussru4me21-Oct-05 18:48 
I have a 100mb gzip file. Unzipping the whole thing will take up 2GB memory. So unzipping on the fly is prefered. How would I go about doing that using this gzip wrapper?
 

GeneralBad Compressed Filesussgrandamoca15-Sep-05 22:51 
I have this problem, that occurs only sometimes:
 
-i compress a file (with no errors)
-then i decompress it (also with no errors)
 
the problem is that the decompressed file is diferent (not in size, but the content is different) from the original file.
Note that this problem only occurs rarely, but has very negative impact on the application that i'm building. Is there any way to prevent this? Can you help me? Sigh | :sigh:
 
Renato S. Gaio
GeneralLibrary to decompress ZIP filessussAnonymous14-Sep-05 5:58 
Hello,
 
Does anyone knows a library to decompress zip files.
I am trying to decompres files compressed with win zip .
Do you know if I can still use CGZip ?
GeneralRe: Library to decompress ZIP filesmemberRavi Bhavnani14-Sep-05 6:37 
Here's[^] one.
 
/ravi
 
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
Generalfilesize getting biggermemberMervick5-Jul-05 23:27 
is it only me that feels that the filesize is getting bigger each time i create a new file? was wondering if the header appended at the front and back of the file is being removed during the deflation or is there anything wrong with this code
 
if (!gzip.Open(_T("test"), CGZip::ArchiveModeRead))
{
printf(_T("Cannot read file\n"));
exit(-1);
}
 
void * pBuffer=NULL;
 
unsigned int size;
 
gzip.ReadBuffer( &pBuffer, size );
 
gzip.Close();
 
if (!gzip.Open("test2", CGZip::ArchiveModeWrite))
{
printf(_T("Cannot create file\n"));
exit(-1);
}
 
gzip.SetCompression( CGZip::CompressionModeNoCompression );
 
gzip.WriteBuffer( pBuffer, size );
 
gzip.Close();
QuestionCGZip with zlib 1.2.2?membergrandamoca31-Mar-05 6:09 
Can´t someone provide me CGZip (C++ wrapper) using the latest zlib? (1.2.2)
 
grandamoca
GeneralA stupid question but..memberTommy2k8-Nov-04 5:22 
How do i use this code to decompress a .gz file stored on the internet?
 
Thnx...
GeneralUnicode support for CGZipmemberJxm96c23-Apr-04 6:14 
Dear Jonathan,
 
Firstly let me thank you for making this easy for us newbies. Your wrapper was easy to use and does a great job. Smile | :)
 
One thing I am having problem is though is that I am building XML Dom trees for SVG docs using MSXML4, it all works fine if the XML data does not contain any wchars, and the XML is written to an .svgz document using CGZIP with no problem. What I have found however is that when there are occasional unicode characters involved in the XML data, the gzip produced will truncate some data towards the end of the document generated when I check the source (SVG viewer supports svgz which is a gzipped XML doc). I experimented with changing the line where WriteString calls the method WriteBuffer and giving it instead of sizeof(TCHAR), a sizeof(WCHAR). This way the XML data is passed on all well but I find possibly binary characters (or null) running over the end of the XML data so effectively it's gzipping blanks after the XML data source runs out.
 
Therefore I wonder if there is anything that I can do to rid this problem? How can I make XML data containing occasional Unicode work with CGZip? Or is there a way (algorithm) to calculate the real byte length of the XML data string containing bits of Unicode chars.
 
Thanks,
Julius
GeneralRe: Unicode support for CGZipmembervmihalj7-Sep-05 23:55 

Have you defined appropriate Unicode macros which will user wchar_t for TCHAR, instead of (default) plain char?
 
I think that all you need is "#define _UNICODE" or include "_UNICODE" in the compiler option list (exclude MBCS if it's defined there).
 
Take a look at MSDN docs on TCHAR or CString (which is of TCHAR type, so is treated as char or wchar_t).
 
--
"If you think that knowledge/education is expensive, would you try ignorance?"
 
Vatroslav 'Ziggy' Mihalj, B.Sc.Computer Science
Member of HUPRO, ACM, IEEE, IEEE Computer Soc. and Communications Soc.
GeneralRe: Unicode support for CGZipmemberPeter Weyzen10-Aug-06 19:05 
there's a problem here...
 
Even though it compiles UNICODE, it doesn't support it.
 
When you:
m_gzf = gzopen(T2CA(szFileName),"rb");
 
You downgrade a unicode string to an ansi string. If the filename contains international characters, then it doesn work!
 
gzopen doesn't even su
 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Peter Weyzen
Staff Engineer
SoonR Inc.

GeneralRe: Unicode support for CGZipmemberDavid Pritchard9-Mar-09 4:22 
Yep, the zlib code is unchanged, as far as I can see. The real problem is the non-compliant underlying zlib code. The only trick I know to get around this, on NT systems that still create short names (don't have them turned off), is to convert long (Unicode) paths to short (ANSI) paths. This will ensure the file is found and avoids name mangling.
 
- Pfft. Coddled kids. In my day, we used to telnet to port 80, then render the page with pencil and paper-- and that's the way we liked it!
- Pshaw! Youngster. Your UID barely fits inside 16 bits. In _my_ day we had to whistle the 1's and 0's through an acoustic coupler!

QuestionCan not set compress modememberVickyLiu22-Sep-03 20:38 
:(I call the function SetCompression() to set the compress mode,but the result is the same with using the default compress mode,Cry | :(( How can I do?
GeneralConflicts with MFC programmemberHillrider31-Jul-03 11:52 
I've bee trying to integrate the CGZip class with an MFC based application in Visual C++ v6.0. I run into confilcts with standard MFC libraries as seen below:
 
LINK : warning LNK4049: locally defined symbol "_sprintf" imported
LINK : warning LNK4049: locally defined symbol "_fclose" imported
LINK : warning LNK4049: locally defined symbol "_fprintf" imported
LINK : warning LNK4049: locally defined symbol "_fopen" imported
LINK : warning LNK4049: locally defined symbol "__close" imported
LINK : warning LNK4049: locally defined symbol "__open" imported
LINK : warning LNK4049: locally defined symbol "__setmbcp" imported
AboutDlg.obj : error LNK2001: unresolved external symbol __imp___getdiskfree
AboutDlg.obj : error LNK2001: unresolved external symbol __imp___getdrive
 
I'm not very good at linking projects, are there any project settings that I can set to prevent the conflict??
 
Rick OMG | :OMG:
 

GeneralRe: Conflicts with MFC programmemberHillrider31-Jul-03 12:16 
I managed to get the code to compile and work within my application by including the zlib.lib file. I tried downloading the original ZLIB source file to generate my own library file by was dismally unsuccessful. Can anyone offer any advice as to:
-what a simple procedure is for creating library files
-should i be concerned that my link settings for my new project look completely different from the link settings in project code
 
Thanks in advance folks,
Rick
Wink | ;)
GeneralRe: Conflicts with MFC programmemberJonathan de Halleux31-Jul-03 12:20 
Make sure you link to the same MFC libraries: Single or multi threaded, DLL or Static MFC. Check Project properties general tab.
 
Moreover, CGZip is a bit outdated. I advise you to use zipstream which is far better ( see http://www.codeproject.com/vcpp/stl/zipstream.asp?target=zipstream )
 
Jonathan de Halleux.

GeneralRe: Conflicts with MFC programmemberrjahrman6-Aug-03 18:17 
Huh?
GeneralRe: Conflicts with MFC programmemberJonathan de Halleux6-Aug-03 23:07 
Confused | :confused: Confused | :confused: Confused | :confused:
 
Jonathan de Halleux.

GeneralArchive multiple filesmemberadityad12-Jun-03 9:18 
I need something that is platform independent(linux and windows is the requirement) to archive files. I think this is what I need but how do I do that with multiple files. i.e I want to group 5 files that I know the locations of into 1 archive and later ungroup them. Can someone show me how to do that like how u showed in the demo to create a new txt file and compress it.
GeneralRe: Archive multiple filesmemberJonathan de Halleux12-Jun-03 9:30 
Try the article about XZIP which implements multiple file handling...
 
Jonathan de Halleux.

GeneralJust great!memberEugene Plokhov7-Jun-03 21:29 
Thank you, Jonathan. Very useful code!
 
Best regards,
Eugene

GeneralRe: Just great!memberJonathan de Halleux31-Jul-03 12:23 
You check out my new article zipstream where zipping is done through STL streams. Smile | :)
 
Jonathan de Halleux.

GeneralArchiving Multiple FilesmemberAnimesh Pandeya27-Jan-03 2:19 
Hi
I saw the CGZip class and it is exactly what I need. Thanks. I have one question .. how do I put multiple files in a single gz archive.

 
Animesh
GeneralTarmemberJonathan de Halleux27-Jan-03 2:53 
The easiest method is to tar and then compress the tar file...

 
Jonathan de Halleux.

Generalbuffer to buffer treatmentmemberrowby14-Jan-03 7:23 
I want to be able to compress/decompress from one buffer to another. Is this possible? I have no file system to work with. further more, is it possible to unzip a certain number of bytes only?
GeneralRe: buffer to buffer treatmentmemberJonathan de Halleux15-Jan-03 22:46 

rowby wrote:
I want to be able to compress/decompress from one buffer to another.
 
CGZip is wrapper around the gz methods provided with gzip.
 
Ultimately, I (or someone else) will go deeper into the gzip library and bypass these gz methods. At that time it will be possible to compress-decrompress from IStream to IStream...
 
Until then, the answer is no.
 

rowby wrote:
It possible to unzip a certain number of bytes only?
Yes, just modify the read buffer method. If you look in it you will see the gzread is called. gzread takes the number of byte to read Smile | :)

 
Jonathan de Halleux.

GeneralRe: buffer to buffer treatmentmemberJonathan de Halleux27-Jun-03 1:38 
This feature is built-in in zlib. See compress and decompress functions.
 
Jonathan de Halleux.

QuestionWhere is the code?memberpeter.ward@chasseral.com13-Jan-03 1:45 
I feel very silly Confused | :confused: asking this question, but where is the implementation, not the demo?
This class seems exactly what I'm looking for...
 
Peter
AnswerCode = gzip.h, gzip.cppmemberJonathan de Halleux13-Jan-03 2:41 
The class CGZip is implemented in the file gzip.h, gzip.cpp.
 
The files are to be found in the demo.
 
Jonathan de Halleux.

GeneralRe: Code = gzip.h, gzip.cppmemberEvilTed14-Aug-03 8:13 
Actually, they aren't included Smile | :)
gzip.h is available in the help file, but no sign of gzip.cpp.
Can you please update the demo project to include them?
 
Thanks
 
Colin
QuestionHow can I unzip a file which tarred ?membervanta8-Jan-03 21:09 
Hello everyone,
I used gzip class.
I have a file : aaa.tar.gz.
I do as example above, the result I get is pBuffer = "aaa.tar". So I dont know what I have to do for decompress that file ?
Please give me any ideas.
Thanks
 
E-Mail vthieuanh@yahoo.com

AnswerTar filesmemberJonathan de Halleux15-Jan-03 22:41 
What you get is a tar file. You have (it's logical) to untar it Smile | :)
 
I think untar is part of GNU. So you dig in their site and you'll find it Smile | :)
 
Jonathan de Halleux.

GeneralRe: Tar filesmembervanta15-Jan-03 23:06 
Hello,
Thks for your reply.
I know, untar ispart of GNU. and I haved a look in their sites already.
 
BUT :
My application is to extract the files in TAR,GZ,GZIP formats.
My application is available on Windows OS, NOT UNIX.
So, I want to have an example ( of Visual C, Windows OS) to decompress the file which was compressed in TAR,GZ formats.
 
For example : I have a file aaa.tar.gz and I want to extract that file.
your demo can extract that file ???
 
Please give me any ideas.
I am waiting for your reply.
Best regards.
 
THIEU Anh-Van
E-Mail vthieuanh@yahoo.com

GeneralRe: Tar filesmemberJonathan de Halleux15-Jan-03 23:40 
vanta wrote:
For example : I have a file aaa.tar.gz and I want to extract that file.
your demo can extract that file ???

 
The demo will extract the file to aaa.tar and then it's up to you to untar it.
 
My idea:
 
Get the source at GNU site and compile it.
Read carefully the license and see if it is ok with the usage you want to do with your application.
 
Jonathan de Halleux.

GeneralBetter CGzipFilesussPaul Andreassen2-Jan-03 17:30 
I've a better CGzipFile that can be used interchangeably with CFile and CStdioFile.   It also compresses MFC serialized document data.
Check it out at http://www.users.bigpond.net.au/pinnacleplus/gzipfile.html
 


GeneralRe: Better CGzipFilememberPaul Selormey2-Jan-03 18:01 
Please find a better way to advertise your product.
 
Best regards,
Paul.

 
Jesus Christ is LOVE! Please tell somebody.
QuestionSo, GZIP class can gzip,ungzip the SINGLE file ?membervanta29-Dec-02 19:55 
Hello everyone,
As somebody discussed and the source code I downloaded, I think this class can gzip,gunzip the SINGLE file only. For example, If I have a file : xxx.TAR.gz. Before compresing,the xxx is one folder which has some files. So, how can I know what files included in that folder ?
 
How I can Untar the file ? Please tell me if you can.
Thank you.
vanta

 
THIEU Anh-Van
Programmer - VNTeam Company
Room No 408,123 Truong Dinh st,Dist. 3,HCM City,Viet Nam
Tel: +84 (8) 932 1045
E-Mail vthieuanh@yahoo.com

AnswerUntarmemberJonathan de Halleux15-Jan-03 22:42 
Untarring a file is another problem (untar is part of GNU)
 
Sorry, I do not have the time to look at it...
 
Jonathan de Halleux.

GeneralGZip and GUnzipmemberPaul Selormey23-Dec-02 3:21 
Is there no way to simply gzip and gunzip a file? I do not wish to work with the buffer just zip and unzip a file.
 
Something like CGZip::Compress(OldfileName.tar, fileName.gz);
and CGZip::UnCompress(fileName.gz, NewFileName.tar)
 
Best regards,
Paul.

 
Jesus Christ is LOVE! Please tell somebody.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 10 Dec 2002
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid