This post is as always a dump of my rough notes to do what pops up occasionally and sure enough the Internet comes to the rescue. Although my requirements, were a little different from what’s described here but sufficient for most cases:
tar -xzf file.tgz
has multiple parts
$ tar -xzf file.tgz -
$ # it is functionally equivalent to
$ gunzip file.tgz
$ # this results in file.tar
$ tar xf file.tar -C /tmp
To achieve this we can use the following multi-step process in C++ programmatically:
Unzip or inflate the compcompressed archive
This is achieved by using the zlib
project.
#define BUF_SZ 16384
void my_gunzip(gzFile_s *src, FILE *dest) {
unsigned char buf[BUF_SZ];
for (auto sz = gzread(src, buf, BUF_SZ); sz > 0; sz = gzread(src, buf, BUF_SZ) {
std::fwrite(buf, 1, BUF_SZ, dest);
}
}
Untar file
I found this implementation pretty helpful in doing this. Although, it appears that this can be converted into C++ from the current C-style.
References:
My Podcast!
If you like topics such as this then please consider subscribing to my podcast. I talk to some of the stalwarts in tech and ask them what their favorite productivity hacks are:
Available on iTunes Podcast
Visit Void Star Podcast’s page on iTunes Podcast Portal. Please Click ‘Subscribe’, leave a comment.