5,691,626 members and growing! (13,944 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Compression     Intermediate

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

By Marius Daniel Ciorecan

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).
VC6, VC7.1, C++Windows, NT4, MFC, VS.NET2003, VS6, Visual Studio, Dev

Posted: 14 Nov 2005
Updated: 14 Nov 2005
Views: 35,039
Bookmarked: 50 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 6.92 Rating: 4.68 out of 5
1 vote, 3.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
5 votes, 16.7%
4
24 votes, 80.0%
5

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 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

Marius Daniel Ciorecan


Working in Timisoara (Romania) as a programmer for the last 6-7 years, last years as a project leader. Programming C++ on Win/Linux/UNIX platforms.
I'm using codeproject to find out good code usefull in my personal projects...
Occupation: Web Developer
Location: Romania Romania

Other popular Files and Folders articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
GeneralProblem with tar filesmembervshany5:55 14 Oct '07  
Generali have a problemmemberjosip cagalj0:13 30 Apr '07  
Generala url pathmemberdreamwj17:49 12 Oct '06  
GeneralRe: a url pathmemberMarius Daniel Ciorecan5:13 13 Oct '06  
GeneralNeed a little help compiling...memberstretchcoder6:00 5 Sep '06  
GeneralRe: Need a little help compiling...memberMarius Daniel Ciorecan2:35 6 Sep '06  
GeneralThank you very much ^_^memberrexarrds23:48 15 Jul '06  
GeneralGood example when using zlib and XZipmemberT-luv9:02 27 Nov '05  
Generalgood jobmemberMaze7:54 26 Nov '05  
GeneralRe: good jobmemberMarius Daniel Ciorecan7:56 27 Nov '05  
GeneralExcellent staffmemberMircea Puiu11:18 22 Nov '05  
GeneralRe: Excellent staffmemberMarius Daniel Ciorecan20:50 22 Nov '05  
GeneralRe: Excellent staffmemberAdrian Datcu23:29 23 Nov '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Nov 2005
Editor: Smitha Vijayan
Copyright 2005 by Marius Daniel Ciorecan
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project