Click here to Skip to main content
15,172,790 members
Articles / Desktop Programming / MFC
Posted 14 Nov 2005


98 bookmarked

DecompressLibrary - A General Library to Decompress a zip, gz, or tar.gz File into a Memory Buffer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (39 votes)
22 Feb 2018CPOL1 min read
A library that provides an easy to use class (CDecompressLibrary) that will detect and decompress into memory buffers an archive file (zip, gz, tar.gz).


I developed this class because I needed a general class that will detect internally if a file is an archive, and then read the compressed files contained in the archive. For Zip and gz archives, I found the source code on CodeProject, in the articles of Hans Dietrich and Jonathan de Halleux.

I also implemented in the class the possibility of detecting & reading from tar files.

Using the Code

The class provides methods for: detecting and opening an archive, returning the number of files in an archive, and reading files from archives based on file position. In the header file, we have the methods and their description.

Open a compressed file.
    pszCompressedFile - name of the file
Return values:
    TRUE - file opened
    FALSE - file could not be opened 
            (use GetErrorText() to find the error)\
    The archive file must have one of these extensions:
    - .zip
    - .gz
    - .tar.gz
    - .tgz
BOOL    OpenArchive(IN const char* pszArchiveFile);

Closes the archive, destroys all the internal objects
void    CloseArchive();

Get number of files in the archive
Return values:
    Number of files in the archive.
    -1 if there are errors.
int        GetCompressedFilesCount();

Get a buffer that contain the file in archive at a specific position
    nFileIndex - file position in archive
    pszFileBuffer - buffer containing the file data
    nFileSize - file size
    fIsFile - TRUE if this is a regular file, 
              FALSE otherwise (directory, link, etc)
    szFileName - name of the file
Return values:
    TRUE - file found and buffer filled correctly
    FALSE - some error (use GetErrorText() to find the error)
    All data buffers will be allocated/deleted internally in this class,
    they should not be changed from outside !
BOOL    GetArchiveFile( IN int nFileIndex, 
                        OUT char** pszFileBuffer, 
                        OUT int& nFileSize,
                        OUT BOOL& fIsFile,
                        OUT CString& szFileName);

Get last error if there were any problems
CString    GetErrorText();
BOOL    OpenArchive(IN const char* pszArchiveFile);

How to Use

Include the DecompressLibrary project in your workspace. Next, include the DecompressClass.h header file. Sample usage:

CDecompressClass testObj;
// open archive
// if we opened ok, read each file from the archive
for (int i=0; i < testObj.GetCompressedFilesCount(); i++)
    // will contain the file buffer
    char* pszFileBuffer;
    // will contain the file length
    int nFileLength;
    // will contain the file name
    CString szFileName;
    // flag if is a file or not (directory, link, etc)
    BOOL fIsFile;
    testObj.GetArchiveFile(i,    /*position in archive*/
                &pszFileBuffer,  /*will point to the memory buffer 
                                   containing the file content*/
                nFileLength,     /*will contain the file length*/
                fIsFile,         /*will show if it's a file or not*/
                szFileName       /*name of the file*/


  • Version 1.0 - 2005 Nov 14, first implementation of the class

Demo App

You will find a test dialog used to test the class:



This software is created by Marius Daniel Ciorecan at and is released into the public domain. You are free to use it in any way you like. If you modify it or extend it, please consider posting new code here for everyone to share. This software is provided "as is" with no expressed or implied warranty. I accept no liability for any damage or loss of business that this software may cause.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marius Daniel Ciorecan
Founder elapseit
Romania Romania
Old time developer, now managing partner at elapseit (

Comments and Discussions

Questionvery good job! Pin
skyformat99@gmail.com14-Mar-18 18:01
Memberskyformat99@gmail.com14-Mar-18 18:01 
Bugbug when parsing extended timestamp [with fix] Pin
Member 1103698726-Aug-14 3:31
MemberMember 1103698726-Aug-14 3:31 
GeneralMy vote of 5 Pin
skyformat99@gmail.com21-Nov-13 22:26
Memberskyformat99@gmail.com21-Nov-13 22:26 
GeneralThank you ! Pin
fanmhai14-Apr-11 6:43
Memberfanmhai14-Apr-11 6:43 
GeneralProblem with tar files Pin
vshany14-Oct-07 5:55
Membervshany14-Oct-07 5:55 
GeneralRe: Problem with tar files Pin
robertbiro9-Dec-11 15:35
Memberrobertbiro9-Dec-11 15:35 
GeneralRe: Problem with tar files Pin
Le@rner12-Dec-11 23:34
MemberLe@rner12-Dec-11 23:34 
GeneralRe: Problem with tar files Pin
chauchauvoi8-May-14 23:35
Memberchauchauvoi8-May-14 23:35 
Generali have a problem Pin
josip cagalj30-Apr-07 0:13
Memberjosip cagalj30-Apr-07 0:13 
Generala url path Pin
dreamwj12-Oct-06 17:49
Memberdreamwj12-Oct-06 17:49 
GeneralRe: a url path Pin
Marius Daniel Ciorecan13-Oct-06 5:13
MemberMarius Daniel Ciorecan13-Oct-06 5:13 
GeneralNeed a little help compiling... Pin
stretchcoder5-Sep-06 6:00
Memberstretchcoder5-Sep-06 6:00 
GeneralRe: Need a little help compiling... Pin
Marius Daniel Ciorecan6-Sep-06 2:35
MemberMarius Daniel Ciorecan6-Sep-06 2:35 
GeneralThank you very much ^_^ Pin
rexarrds15-Jul-06 23:48
Memberrexarrds15-Jul-06 23:48 
GeneralGood example when using zlib and XZip Pin
C-codist27-Nov-05 9:02
MemberC-codist27-Nov-05 9:02 
Generalgood job Pin
Maze26-Nov-05 7:54
MemberMaze26-Nov-05 7:54 
GeneralRe: good job Pin
Marius Daniel Ciorecan27-Nov-05 7:56
MemberMarius Daniel Ciorecan27-Nov-05 7:56 
GeneralExcellent staff Pin
Mircea Puiu22-Nov-05 11:18
MemberMircea Puiu22-Nov-05 11:18 
GeneralRe: Excellent staff Pin
Marius Daniel Ciorecan22-Nov-05 20:50
MemberMarius Daniel Ciorecan22-Nov-05 20:50 
GeneralRe: Excellent staff Pin
kla_boot23-Nov-05 23:29
Memberkla_boot23-Nov-05 23:29 

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.