To read an archive, you must first obtain an initialized struct archive object from
archive_read_new(). You can then modify this object for the desired operations with the various
archive_read_set_XXX() and
archive_read_support_XXX() functions. In particular, you will need to invoke appropriate
archive_read_support_XXX() functions to enable the corresponding compression and format support. Note that these latter functions perform two distinct operations: they cause the corresponding support code to be linked into your program, and they enable the corresponding auto-detect code. Unless you have specific constraints, you will generally want to invoke
archive_read_support_compression_all() and
archive_read_support_format_all() to enable auto-detect for all formats and compression types currently supported by the library.
Once you have prepared the struct archive object, you call
archive_read_open() to actually open the archive and prepare it for reading. There are several variants of this function; the most basic expects you to provide pointers to several functions that can provide blocks of bytes from the archive. There are convenience forms that allow you to specify a filename, file descriptor,
FILE * object, or a block of memory from which to read the archive data. Note that the core library makes no assumptions about the size of the blocks read; callback functions are free to read whatever block size is most appropriate for the medium.
Each archive entry consists of a header followed by a certain amount of data. You can obtain the next header with
archive_read_next_header(), which returns a pointer to an struct archive_entry structure with information about the current archive element. If the entry is a regular file, then the header will be followed by the file data. You can use
archive_read_data() (which works much like the
read(2) system call) to read this data from the archive. You may prefer to use the higher-level
archive_read_data_skip(), which reads and discards the data for this entry,
archive_read_data_to_buffer(), which reads the data into an in-memory buffer,
archive_read_data_to_file(), which copies the data to the provided file descriptor, or
archive_read_extract(), which recreates the specified entry on disk and copies data from the archive. In particular, note that
archive_read_extract() uses the struct archive_entry structure that you provide it, which may differ from the entry just read from the archive. In particular, many applications will want to override the pathname, file permissions, or ownership.
Once you have finished reading data from the archive, you should call
archive_read_close() to close the archive, then call
archive_read_finish() to release all resources, including all memory allocated by the library.
The
archive_read(3) manual page provides more detailed calling information for this API.