libarchive - Extract to specified directory

oggmonster picture oggmonster · Dec 5, 2011 · Viewed 7.7k times · Source

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.

Answer

Alexandr Derkach picture Alexandr Derkach · Sep 23, 2014

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.