Creating a TAR file in C++

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <cstdlib> | |
#include "lindenb/io/tarball.h" | |
int main(int argc,char** argv) | |
{ | |
/* open file for writing */ | |
std::fstream out("archive.tar",std::ios::out); | |
if(!out.is_open()) | |
{ | |
std::cerr << "Cannot open out" << std::endl; | |
return EXIT_FAILURE; | |
} | |
/* create the tar file */ | |
lindenb::io::Tar tarball(out); | |
/* add item 1 */ | |
tarball.put("myfiles/item1.txt","Hello World 1\n"); | |
/* add item 2 */ | |
tarball.put("myfiles/item2.txt","Hello World 2\n"); | |
/* add a file */ | |
tarball.putFile("tarfile.cpp","myfiles/code.cpp"); | |
/* finalize the tar file */ | |
tarball.finish(); | |
/* close the file */ | |
out.close(); | |
/* we're done */ | |
return EXIT_SUCCESS;; | |
} |
Example
~> g++ -Wall -I ${PATH_TO_SRC}/cclindenb/src/core
tarfile.cpp
~> ./a.out
~> tar tvf archive.tar
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item1.txt
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item2.txt
-rw-r--r-- pierre/users 692 2010-08-03 20:50 myfiles/code.cpp
tar xvf archive.tar --to-stdout myfiles/item1.txt 2> /dev/null
Hello World 1
tarfile.cpp
~> ./a.out
~> tar tvf archive.tar
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item1.txt
-rw-r--r-- pierre/users 14 2010-08-03 20:50 myfiles/item2.txt
-rw-r--r-- pierre/users 692 2010-08-03 20:50 myfiles/code.cpp
tar xvf archive.tar --to-stdout myfiles/item1.txt 2> /dev/null
Hello World 1
That's it,
Pierre
5 comments:
Very good program
Pretty useful, thanks!
Pretty useful, thanks!
Thank you so much. Saved my time....
Thank you for posting your code!
Post a Comment