tarlib - Windows TAR Library

Biblioteca C++ pentru lucrul cu arhive TAR.
Post Reply
User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

tarlib - Windows TAR Library

Post by Marius Bancila »

tarlib is a C++ open-source library used for handling TAR (Tape Archive) files on Windows operating systems. The library, provided as a pack of source code files, is intended to be used in applications that need to handle TAR files.

Requirements
  • enable reading the content of a TAR file
  • extract the content of TAR files
  • archive files (and folders) in a TAR file (*)
  • support different versions of TAR files
Note: (*) not available in the current version

Platforms
  • requires minimum Windows XP (uses file API not available on earlier versions)
Tehnologies
  • Langauge: C++
  • IDE: Visual Studio
Version History
  • v1.0, 2012.09.13
    • Initial release
    • supports extracting content from a TAR archive
  • v1.1, 2012.09.14
    • improved tarEntry::read method
    • additional example for reading and processing TAR entries
Creator License Downloads TAR documentation


Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog
User avatar
Marius Bancila
Fondator
Fondator
Posts: 2344
Joined: 11 Jul 2007, 11:45
Judet: Timiş
Location: Timisoara
Contact:

Re: tarlib - Windows TAR Library

Post by Marius Bancila »

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);
      }
   }
}
Marius Bancila
Fondator Codexpert, Microsoft MVP VC++
Site personal | Blog
SeregaZ
Junior
Junior
Posts: 1
Joined: 26 Oct 2013, 01:40

Re: tarlib - Windows TAR Library

Post by SeregaZ »

now i have another problem - some kind of garbage data at the end of file. as i understand tar file have blocks 512b. first 512 have all information about file name, size, and etc. after this 512b and more - file it self. than next file header, file it self, than empty. i though this is the end of file... but if i see this file in notepad after this last file lay some data part of first file, part of second... some part of header... they are mixed. what this mess means? this additional information for restore data, when file is damaged? i must to be sure when i get empty, after second file context - it is end of archive, and last part can be ignored.
Post Reply