Click here to Skip to main content
11,712,126 members (70,620 online)
Click here to Skip to main content

CFileTar 1.0 - pack and unpack file archives

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

Overview

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.

Features

  • 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

Usage

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.SetFilePath("D:\\nish\\worddocs");
ft.AddFile("Proposal.doc","First project proposal");
ft.AddFile("ProjectPlan.doc","Project plan");
ft.AddFile("ProposalFinal.doc","Final proposal");

//Now some PDFs
ft.SetFilePath("D:\\nish\\pdffiles");
ft.AddFile("SRS.pdf","SRS doc template");
ft.AddFile("DDD.pdf","Detailed design doc template");

//Let's tar it now...
ft.CreateTar("ProjFiles.tar");

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;
CFileTar::GetTarInfo("D:\\nish\\files.tar",&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++)
{
    CFileTar::GetFileInfo("D:\\nish\\files.tar",
        &ti,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 :-

CFileTar::UnTar("D:\\nish\\files.tar",3,
    "D:\\nish\\q1.xls");	

CFileTar::UnTar("D:\\nish\\files.tar",5,
    "D:\\nish\\sw34.doc");

Un-Tarring all files in a tar file :-

CFileTar::UnTar("D:\\nish\\files.tar",
    "D:\\nish\\temp");

Function Reference

CFileTar::CFileTar

CFileTar();

Remarks :- Creates a CFileTar object.

CFileTar::SetHeaderDescription

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.

CFileTar::GetHeaderDescription

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.

CFileTar::SetFilePath

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.

CFileTar::AddFile

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.

CFileTar::CreateTar

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.

CFileTar::GetTarInfo

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.

CFileTar::TarHeader::GetCount

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.

CFileTar::TarHeader::GetDescription

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.

CFileTar::GetFileInfo

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.

CFileTar::TarIndex

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

CFileTar::UnTar

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

License

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

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

 
GeneralRe: I've Tarred and UnTarred but... Pin
KevMoore28-Nov-03 0:52
memberKevMoore28-Nov-03 0:52 
QuestionHow can I set reference to files? Pin
stacxws17-Oct-03 4:34
memberstacxws17-Oct-03 4:34 
Questionhow do i execute? Pin
venkatsb@anz.com28-May-03 13:14
membervenkatsb@anz.com28-May-03 13:14 
GeneralBig Problem Pin
Eric5423-May-03 9:16
sussEric5423-May-03 9:16 
Generalstdafx.h Pin
Staj19-May-03 17:24
sussStaj19-May-03 17:24 
GeneralRe: stdafx.h Pin
Nishant S19-May-03 17:48
editorNishant S19-May-03 17:48 
Generalpassword protection Pin
tomei17-Feb-03 4:47
membertomei17-Feb-03 4:47 
GeneralRe: password protection Pin
Nishant S17-Feb-03 10:46
editorNishant S17-Feb-03 10:46 
GeneralA Bug! Pin
zzmgx16-Nov-02 16:32
memberzzmgx16-Nov-02 16:32 
GeneralRe: A Bug! Pin
Nishant S16-Nov-02 17:15
editorNishant S16-Nov-02 17:15 
GeneralRe: A Bug! Pin
Jose Cruz12-Feb-03 6:08
memberJose Cruz12-Feb-03 6:08 
GeneralRe: A Bug! Pin
David49326-Jun-03 7:41
sussDavid49326-Jun-03 7:41 
GeneralRe: A Bug! Pin
Ridgeley Yu1-Jul-03 1:13
memberRidgeley Yu1-Jul-03 1:13 
GeneralThasnk Nish Pin
David Wulff 213-Apr-02 16:44
memberDavid Wulff 213-Apr-02 16:44 
GeneralRe: Thasnk Nish Pin
Nish [BusterBoy]14-Apr-02 20:02
memberNish [BusterBoy]14-Apr-02 20:02 
GeneralRe: Thasnk Nish Pin
David Wulff14-Apr-02 20:10
memberDavid Wulff14-Apr-02 20:10 
GeneralRe: Thasnk Nish Pin
Nish [BusterBoy]14-Apr-02 20:17
memberNish [BusterBoy]14-Apr-02 20:17 
GeneralGood job! Pin
Chris Amow25-Feb-02 14:48
memberChris Amow25-Feb-02 14:48 
GeneralDon't be disheartened Pin
Joel Holdsworth13-Jan-02 7:03
memberJoel Holdsworth13-Jan-02 7:03 
GeneralRe: Don't be disheartened Pin
Nish [BusterBoy]13-Jan-02 7:31
memberNish [BusterBoy]13-Jan-02 7:31 
Generalstrange archive Pin
Andrew Tyapuhin21-Nov-01 22:30
memberAndrew Tyapuhin21-Nov-01 22:30 
GeneralRe: strange archive Pin
Nish [BusterBoy]22-Nov-01 16:19
memberNish [BusterBoy]22-Nov-01 16:19 
GeneralIs the file format same as UNIX tar Pin
zhangzq7119-Nov-01 18:04
memberzhangzq7119-Nov-01 18:04 
GeneralRe: Is the file format same as UNIX tar Pin
Jörgen Sigvardsson19-Nov-01 22:18
memberJörgen Sigvardsson19-Nov-01 22:18 
GeneralRe: Is the file format same as UNIX tar Pin
Nish [BusterBoy]22-Nov-01 16:17
memberNish [BusterBoy]22-Nov-01 16:17 

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
Web01 | 2.8.150819.1 | Last Updated 19 Nov 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid