Description
The API provides two main classes for working with the TAR archives
- tarFile: encapsulates a TAR file and provides functionality for extracting its content to a folder and iterating through its content
- tarEntry: represents an entry in a TAR file, each entry being described by a header. This class provides functionality for extracting the entry (file or folder) from the TAR archive.
Examples
Example 1: extract a TAR archive to a specified folder using the tarFile
Code: Select all
void extract1(std::string const &filename, std::string const &destination)
{
// create tar file with path and read mode
tarFile tarf(filename, tarModeRead);
// extract to folder
tarf.extract(destination);
}
Example 2: extract a TAR archive to a specified folder using a loop that iterates through the entries of the TAR archive
Code: Select all
void extract2(std::string const &filename, std::string const &destination)
{
// create tar file with path and read mode
tarFile tarf(filename, tarModeRead);
// get the first entry
tarEntry entry = tarf.get_first_entry();
do
{
// if the entry is a directory create the directory
if(entry.header.indicator == tarEntryDirectory)
{
createfolder(path_combine(destination, entry.header.filename));
}
// if the entry is a normal file create the file
else if(entry.header.indicator == tarEntryNormalFile ||
entry.header.indicator == tarEntryNormalFileNull)
{
entry.extractfile_to_folder(destination);
}
// get the next entry in the TAR archive
entry = tarf.get_next_entry();
} while(!entry.is_empty());
}
Example 3: a simplified version of the 2nd example
Code: Select all
void extract3(std::string const &filename, std::string const &destination)
{
// create tar file with path and read mode
tarFile tarf(filename, tarModeRead);
// get the first entry
tarEntry entry = tarf.get_first_entry();
do
{
// extract the current entry
entry.extract(destination);
// get the next entry in the TAR archive
entry = tarf.get_next_entry();
} while(!entry.is_empty());
}
Example 4: explicitly process the entries of a TAR file (no auto-extraction to disk, can be in memory processing)
Code: Select all
void extract4(std::string const& filename)
{
// create tar file with path and read mode
tarFile tarf(filename, tarModeRead);
std::list<tarEntry> entries;
// get the first entry
tarEntry entry = tarf.get_first_entry();
do
{
// add the entry to the list
entries.push_back(entry);
// get the next entry in the TAR archive
entry = tarf.get_next_entry();
} while(!entry.is_empty());
// iterate through the entries
for(std::list<tarEntry>::iterator it = entries.begin();
it != entries.end();
++it)
{
tarEntry& entry = *it;
// consider the files
if(entry.header.indicator == tarEntryNormalFile ||
entry.header.indicator == tarEntryNormalFileNull)
{
// position the TAR file cursor at the beginning of the entry
entry.rewind();
// read from the TAR file in a chunk
char chunk[8*1024];
size_t total = 0;
do
{
size_t readBytes = entry.read(chunk, sizeof(chunk));
// do something with the read buffer
// ...
total += readBytes;
}while(total < entry.header.filesize);
}
}
}