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

CFileTar 1.0 - pack and unpack file archives

, 18 Nov 2001 CPOL
Rate this:
Please Sign up or sign in to vote.
A class for packing and unpacking file archives.


CFileTar is a C++ class [written using plain C++ and a little Win API] that allows you to tar and untar files. People who are familiar with Unix will surely have used the tar archiving utility. This class attempts to provide a rather simple version for Win 32 programmers. Please note that this class does not produce unix-style tar files. It uses it's own custom format to tar and untar the files.


  • MFC not required
  • Tar multiple files into a single archive
  • UnTar files either singly, or as a whole from the archive
  • Query a tar file for information
  • Works with text and binary files


Tarring files

Tarring several files into a single tar archive can be easily achieved through the following steps.

CFileTar ft;
ft.SetHeaderDescription("This archive contains several pdf/doc files");

//Let's add some doc files
ft.AddFile("Proposal.doc","First project proposal");
ft.AddFile("ProjectPlan.doc","Project plan");
ft.AddFile("ProposalFinal.doc","Final proposal");

//Now some PDFs
ft.AddFile("SRS.pdf","SRS doc template");
ft.AddFile("DDD.pdf","Detailed design doc template");

//Let's tar it now...

Querying tar files

Sometimes, you might want to retrieve information about a tar file. CFileTar provides a couple of static functions just for this purpose.

CFileTar::TarHeader t;
cout << "Number of archived files : " << t.GetCount() << "\r\n";
cout << "Description of tar file : " << t.GetDescription() << "\r\n";

You can also retrieve information about individual files.

CFileTar::TarIndex ti;
for (int y=1;y<=t.GetCount();y++)
    cout << "File Index : " << y << "\r\n";
    cout << "File Name : " << ti.FileName << "\r\n";
    cout << "File desc : " << ti.Description << "\r\n";
    cout << "Size : " << ti.Size << "\r\n";

Un-Tarring files

Un-Tarring individual files :-



Un-Tarring all files in a tar file :-


Function Reference



Remarks :- Creates a CFileTar object.


BOOL SetHeaderDescription(char *strdesc);

Return Value :- This will return true if the header was successfully set, else it will return false.

Parameters :-

strdesc - This is the header description string

Remarks :- Use this function to set the tar file's header description. This will prove useful when querying tar files.


const char* GetHeaderDescription();

Return Value :- This will return the tar file's header description

Remarks :- This function can be used to retrieve a const char* to the header description.


void SetFilePath(char *path);

Parameters :-

path - This is the directory to search for the files being added to the tar archive.

Remarks :- Set the file path before adding files into the archive. You may change the file path any number of times, as you keep adding files from different folders.


int AddFile(char *fname, char *desc);

Return Value :- Returns 0 if the file was successfully added to the list of files, else it returns 1.

Parameters :-

fname - The filename of the file to add, excluding the directory where it resides

desc - A description which is saved as meta-information on a per-file basis.

Remarks :- This function won't actually add the files to the tar. Instead it will add the filename with meta-info to a list of files to be tarred. The physical archiving process only takes place when the CreateTar function is called.


int CreateTar(char *TarFName, char* TarPath=NULL);

Return Value :- Returns 0 if the tar was successfully created, else returns 1

Parameters :-

TarFName - The filename of the tar file to create, excluding the directory path.

TarPath - The directory to create the tar file in. If not specified, then the last tar-files directory set using SetFilePath will be used as the default directory.

Remarks :- This is the function that actually creates the tar archive. If several huge files are being tarred, this function will obviously take quite a bit of time.


static int GetTarInfo(char *TarFile, TarHeader *pTarHeader);

Return Value :- Returns 0 if successful, else returns 1.

Parameters :-

TarFile - Fully qualified path and filename of the tar file.

pTarHeader - A pointer to a TarHeader object, that receives the information

Remarks :- Use this function to retrieve the Tar file header and the total number of files in the tar archive.


int GetCount();

Return Value :- Returns the number of files in the tar archive.

Remarks :- Call this function on the TarHeader object that has been passed to a successful GetTarInfo function call.


const char* GetDescription();

Return Value :- Returns the tar header description.

Remarks :- Call this function on the TarHeader object that has been passed to a successful GetTarInfo function call.


static int GetFileInfo(char *TarFile, TarIndex *pTarIndex, int index);

Return Value :- Returns 0 if successful, else returns 1.

Parameters :-

TarFile - Fully qualified path and filename of the tar file.

pTarIndex - A pointer to a TarIndex object that will receive the information.

index - Index of file within the archive. The index is one-based.

Remarks :- The TarIndex object that we pass will contain the required information after a successful call to this function.


struct TarIndex
    long Start;
    long Size;
    char FileName[_MAX_FNAME];
    char Description[256];

Start - This is the start position of the file within the archive. The user need not bother with this field.

Size - This is the size of the file in bytes

FileName - This is the original filename

Description - This is the file description


  • static int UnTar(char *TarFile, char *dpath);
  • static int UnTar(char *TarFile, int index, char *fpath);

Return Value :- Returns 0 if successful, else returns 1.

Parameters :-

TarFile - Fully qualified path and filename of the tar file.

dpath - The directory to extract all files in the archive to. The original filenames will be used for each file in the archive.

index - Index of the file within the archive. One-based index.

fpath - Fully qualified path and filename to be used for the file to be extracted.

Remarks :- The first override of this function will extract all files in the tar archive to the directory specified. The default filenames will be used for each extracted file. The second override is used to extract individual files. For this version, you'll need to specify a fully qualified filename with path for the extracted file.


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


About the Author

Nish Nishant

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

GeneralMessage Automatically Removed Pinmemberom9-Jan-10 5:29 
GeneralSlight change [modified] Pinmembertomb3453618-Feb-07 9:18 
GeneralSome little changes PinmemberNyarlatotep22-Jan-07 6:06 
Generalfix FilePath is where bugs come from PinmemberMyronLi15-May-06 17:04 
GeneralHeader (revised) PinmemberMyronLi15-May-06 22:49 
GeneralBody (revised) PinmemberMyronLi15-May-06 22:50 
GeneralSome recommendations Pinmemberbababyt5-May-05 1:44 
Generalerror in compress file PinmemberLucas6929-Mar-04 21:49 
GeneralRe: error in compress file PinmemberSchubiMice3-May-05 2:28 
GeneralI've Tarred and UnTarred but... PinmemberMyron Belchmyer27-Oct-03 19:00 
GeneralRe: I've Tarred and UnTarred but... PinmemberKevMoore28-Nov-03 1:52 
QuestionHow can I set reference to files? Pinmemberstacxws17-Oct-03 5:34 
Questionhow do i execute? Pinmembervenkatsb@anz.com28-May-03 14:14 
GeneralBig Problem PinsussEric5423-May-03 10:16 
Generalstdafx.h PinsussStaj19-May-03 18:24 
GeneralRe: stdafx.h PineditorNishant S19-May-03 18:48 
Generalpassword protection Pinmembertomei17-Feb-03 5:47 
GeneralRe: password protection PineditorNishant S17-Feb-03 11:46 
GeneralA Bug! Pinmemberzzmgx16-Nov-02 17:32 
GeneralRe: A Bug! PineditorNishant S16-Nov-02 18:15 
GeneralRe: A Bug! PinmemberJose Cruz12-Feb-03 7:08 
GeneralRe: A Bug! PinsussDavid49326-Jun-03 8:41 
GeneralRe: A Bug! PinmemberRidgeley Yu1-Jul-03 2:13 
GeneralThasnk Nish PinmemberDavid Wulff 213-Apr-02 17:44 
GeneralRe: Thasnk Nish PinmemberNish [BusterBoy]14-Apr-02 21:02 
GeneralRe: Thasnk Nish PinmemberDavid Wulff14-Apr-02 21:10 
GeneralRe: Thasnk Nish PinmemberNish [BusterBoy]14-Apr-02 21:17 
GeneralGood job! PinmemberChris Amow25-Feb-02 15:48 
GeneralDon't be disheartened PinmemberJoel Holdsworth13-Jan-02 8:03 
GeneralRe: Don't be disheartened PinmemberNish [BusterBoy]13-Jan-02 8:31 
Generalstrange archive PinmemberAndrew Tyapuhin21-Nov-01 23:30 
GeneralRe: strange archive PinmemberNish [BusterBoy]22-Nov-01 17:19 
GeneralIs the file format same as UNIX tar Pinmemberzhangzq7119-Nov-01 19:04 
GeneralRe: Is the file format same as UNIX tar PinmemberJörgen Sigvardsson19-Nov-01 23:18 
GeneralRe: Is the file format same as UNIX tar PinmemberNish [BusterBoy]22-Nov-01 17:17 
GeneralRe: Is the file format same as UNIX tar PinmemberJörgen Sigvardsson22-Nov-01 22:55 
GeneralRe: Is the file format same as UNIX tar PinmemberNish [BusterBoy]22-Nov-01 17:16 

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 | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 19 Nov 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid