Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libarchive - Extract to specified directory

Tags:

c++

archive

I have a tar file I want to extract with libarchive to a specific directory. How can I make libarchive extract into to any directory I want? At the moment it always extracts into my program's working directory. I looked at this answer but all this does is change the location of an archive entry within the archive, i.e. it still extracts into my program's working directory just at a different sub-directory.

like image 555
oggmonster Avatar asked Dec 05 '11 10:12

oggmonster


2 Answers

I resolved this issue next way: (insert this code before calling 'archive_write_header' function)

    const char* currentFile = archive_entry_pathname(archiveEntry);
    const std::string fullOutputPath = destination + currentFile;
    archive_entry_set_pathname(archiveEntry, fullOutputPath.c_str());

where destination is output path. And it works.

like image 76
Alexandr Derkach Avatar answered Nov 17 '22 20:11

Alexandr Derkach


From the libarchive discussion boards:

"It depends, of course, on the archive being extracted.

Typically, you would chdir() to the directory where you want the output to go, then use code similar to that in the Wiki Examples page:

A Complete Extractor Example

or in the untar.c sample program:

untar Example

Of course, if the tar file you're extracting has interesting filenames (such as "c:\someotherdirectory"), then you'll need to play with the filenames as you extract.

Note that the examples all use archive_read_next_header() to get an entry object from the input archive describing the next entry; you are then free to edit that entry description in any way you wish -- in particular, you can change the name, owner, or permissions -- before calling archive_write_header() to recreate the entry on disk.

The Examples page in the Wiki above is probably the best place to start."

like image 22
oggmonster Avatar answered Nov 17 '22 20:11

oggmonster