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

DecompressLibrary - a general library to decompress a zip, gz, or tar.gz file into a memory buffer

By , 14 Nov 2005
 

Introduction

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.
Parameters
    pszCompressedFile - name of the file
Return values:
    TRUE - file opened
    FALSE - file could not be opened 
            (use GetErrorText() to find the errror)\
Note:
    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
Params:
    nFileIndex - file position in archive
    pszFileBuffer - buffer containg 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)
Note:
    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
testObj.OpenArchive("archivefile.zip");
// 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*/
                );            
}

History

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

Demo app

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

TestDlg

Usage

This software 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 to 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.

License

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
Software Developer (Senior)
Romania Romania
Member
Programming C++on Win/Linux/UNIX platforms and lately ASP.NET with C#.

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThank you !memberfanmhai14 Apr '11 - 5:43 
GeneralProblem with tar filesmembervshany14 Oct '07 - 4:55 
GeneralRe: Problem with tar filesmemberrobertbiro9 Dec '11 - 14:35 
GeneralRe: Problem with tar filesmemberLe@rner12 Dec '11 - 22:34 
Generali have a problemmemberjosip cagalj29 Apr '07 - 23:13 
I tested the demo app and i couldn't open some .tar folder! I'ved traced the code in debug mode and i find where it craks! When you call 'OpenArchive' function than you '// check if it's zip...' if not '// check if it's a gzip' and then within test for a .tar file!
Well my .tar file (created on unix machine then using FTP transfered on a local Windows computer) didn't pass neither '// check if it's a gzip', and go out with "Could not determine archive type !" err message!
Is it a problem because of unix as creation place or what? Please help, thx.
Good stuff anyway!
Generala url pathmemberdreamwj12 Oct '06 - 16:49 
GeneralRe: a url pathmemberMarius Daniel Ciorecan13 Oct '06 - 4:13 
GeneralNeed a little help compiling...memberstretchcoder5 Sep '06 - 5:00 
GeneralRe: Need a little help compiling...memberMarius Daniel Ciorecan6 Sep '06 - 1:35 
GeneralThank you very much ^_^memberrexarrds15 Jul '06 - 22:48 
GeneralGood example when using zlib and XZipmemberT-luv27 Nov '05 - 8:02 
Generalgood jobmemberMaze26 Nov '05 - 6:54 
GeneralRe: good jobmemberMarius Daniel Ciorecan27 Nov '05 - 6:56 
GeneralExcellent staffmemberMircea Puiu22 Nov '05 - 10:18 
GeneralRe: Excellent staffmemberMarius Daniel Ciorecan22 Nov '05 - 19:50 
GeneralRe: Excellent staffmemberAdrian Datcu23 Nov '05 - 22:29 

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.130516.1 | Last Updated 14 Nov 2005
Article Copyright 2005 by Marius Daniel Ciorecan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid