How to unzip a tar.gz file using a C ++ program

In my linux program, I want to unzip the contents of the tar.gz file to a specific directory. Is there any system call or any C ++ class available in C / C ++ to extract the contents of a file from a tar.gz file?

+6
source share
4 answers

There is an excellent libarchive library that supports access to several archive formats using a compatible API. You can follow these examples on how to use it.

If you are running Ubuntu, you can easily install this library using the sudo apt-get install libarchive-dev . On other platforms, you may need to download the source code and compile this library yourself.

One of the benefits of using libarchive vs. using system() calls is independent of system utilities, and should also work faster because it is not fork.

+3
source

If you include <cstdlib> , you can call:

 system("your tar or gz command"); 
0
source

You can use multiple libs such as libtar.

Or you can use a system call, as you mentioned:

 system("tar -zxf /your/file.tar.gz") 
0
source

http://www.cplusplus.com/reference/cstdlib/system/

You can do it as follows:

  system("tar -zxvf yourfile"); 

You can also see this:

0
source

Source: https://habr.com/ru/post/971697/


All Articles