In my linux program, I want to decompress a tar.gz file contents to a specific directory. Is there any system call or any C++ class available in C/C++ to extract file contents from tar.gz file?
Asked
Active
Viewed 2.4k times
9
-
1[tar](http://stackoverflow.com/questions/4812837/c-library-to-make-tar-files) and [gz](http://stackoverflow.com/questions/624250/how-do-i-read-write-gzipped-files) – Unda Jul 03 '14 at 06:56
-
possible duplicate of [How to read a .gz file line-by-line in C++?](http://stackoverflow.com/questions/3201538/how-to-read-a-gz-file-line-by-line-in-c) – bobah Jul 03 '14 at 06:58
-
@bobah: this is not a duplicate. linked question talks about single .gz file, while this one is about .tar.gz archive with many files in it – mvp Jul 03 '14 at 07:11
-
You should ask a specific question for a particular programming problem. Since Stack Overflow hides the Close reason from you: *"Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."* – jww Jul 30 '18 at 12:17
2 Answers
13
There is excellent library libarchive, which supports accessing multiple archive formats using consistent API. You can follow these examples on how to use it.
If you are on Ubuntu, you can easily install this library using command sudo apt-get install libarchive-dev
. On other platforms, you may need to download source code and compile this library yourself.
One advantage of using libarchive vs. using system()
calls is not depending on system utilities, and also it should work faster because it does not fork.
-
-
5
-
error: a storage class can only be specified for objects and functions. I used codeblocks and only works in c . – Samuel Mar 02 '16 at 07:03
-
no, I am trying to find any tutorial about it. I never use wrapper before. THX. – Samuel Mar 02 '16 at 07:13
-
-
libarchive unpacks unicode paths incorrectly on Windows! Please do not recommend this library as a universal solution. It is a good Linux library, but it is a big trap for people who write multiplatform applications on Linux. – Number47 Dec 06 '19 at 20:35
-
@Clare, instead of complaining, perhaps you should consider submitting a patch to fix libarchive file path handling - it's how open source works. – mvp Dec 07 '19 at 05:49
-5
You can use several libs like libtar.
Or you can use a system call like you already mentioned:
system("tar -zxf /your/file.tar.gz")

BenMorel
- 34,448
- 50
- 182
- 322

Nicolai W.
- 24
- 4