Libarchive to extract to a specified folder?

2019-02-18 14:46发布

Anybody can help show examples of using libarchive to extract ZIP files to a specified folder? It looks like the sample programs provided (untar.c, tarfilter.c and minitar) all extracts the archive to the current working directory. Is there a way to say "extract to this folder and below" to libarchive and not clobber the program's active folder?

One of the main drivers is that the extraction code will be run in a background thread, and thus changing the program working directory may create problems. Also this will be used in an iOS application (iPhone, iPad), which is picky on what folders that the application can write to.

Thanks in advance.

2条回答
趁早两清
2楼-- · 2019-02-18 15:12

untar.c can be easily modified to do this by prepending the path you want to use to the pathname that is passed to create_dir() and create_file() from untar().

Note that untar.c is a standalone decompressor, seperate from libarchive.

The same effect is easily achievable with libarchive - it gives you full control over the creation of files and directories when unarchiving.

查看更多
The star\"
3楼-- · 2019-02-18 15:32

You can rewrite each archive_entry's pathname before calling archive_read_extract. For example:

const char* path = archive_entry_pathname( entry );
char newPath[PATH_MAX + 1];
snprintf( newPath, PATH_MAX, "/SomeOtherDirectory/%s", path );
archive_entry_set_pathname( entry, newPath );
查看更多
登录 后发表回答