ARCHIVE_READ(3)
π ARCHIVE_READ(3) π
okay bestie, welcome to the libarchive streaming reader page β the manual name is ARCHIVE_READ and yes we're doing the whole thing together, no skimming, no skipping. grab a boba. π§
π LIBRARY π
Streaming Archive Library (libarchive, -larchive) β she's iconic, she's linked with -larchive, periodt. β¨
β¨ SYNOPSIS β¨
#include <archive.h>
that's it. that's the include. she's low maintenance. π
π¦ DESCRIPTION π¦
These functions hand you a complete API for reading streaming archives. The whole vibe is: first you create the struct archive object, set your options, initialize the reader, then you iterate over the archive headers and the data that comes with them, and finally you close the archive and release all the resources. That's the whole arc, bestie. πΈ
π Create archive object
See archive_read_new(3).
To read an archive you literally have to first get yourself an initialized struct archive object from archive_read_new(). No object, no reading. it's giving "prerequisite". π
π₯ Enable filters and formats
See archive_read_filter(3) and archive_read_format(3).
Then you can tweak that object for whatever you wanna do with the various archive_read_set_XXX() and archive_read_support_XXX() functions. And ngl, you will need to call the right archive_read_support_XXX() functions to turn on the compression and format support you want.
β οΈ Important thing to know: those support functions do two separate jobs β they pull the corresponding support code into your program (linking), AND they switch on the corresponding auto-detect code. Two jobs, one function, multitasking queen. π
Unless you've got specific constraints, you're gonna want to call archive_read_support_filter_all() and archive_read_support_format_all() so that auto-detect is on for every format and compression type the library currently supports. slay. π
π Set options
See archive_read_set_options(3).
π« Open archive
See archive_read_open(3).
Once your struct archive object is all prepped, you call archive_read_open() to actually open the archive and get it ready for reading. There are several variants of this function, and the most basic one wants you to hand over pointers to several functions that can produce blocks of bytes from the archive. But there are convenience forms too β ones that let you just specify a filename, a file descriptor, a FILE * object, or a block of memory to read the archive data from. π«Ά
π Note: the core library makes no assumptions about how big the blocks being read are. Your callback functions are free to read whatever block size is most appropriate for the medium. your callback, your rules. β¨
π Consume archive
See archive_read_header(3), archive_read_data(3) and archive_read_extract(3).
Every archive entry is a header followed by a certain amount of data. You get the next header with archive_read_next_header(), which returns a pointer to a struct archive_entry structure holding info about the current archive element. If that entry is a regular file, the header is followed by the file data. π
You can read that data from the archive with archive_read_data() β which works a lot like the read(2) system call β or with archive_read_data_block(), which gives you a slightly more efficient interface. ngl you might prefer the higher-level options instead:
archive_read_data_skip()β reads and discards the data for this entryarchive_read_data_into_fd()β copies the data to the file descriptor you providearchive_read_extract()β recreates the specified entry on disk and copies the data from the archive
π Lowkey important:archive_read_extract()uses thestruct archive_entrystructure you provide it, which may differ from the entry that was just read from the archive. Many applications will want to override the pathname, the file permissions, or the ownership. main character energy: you decide who you are. π
π§ Release resources
See archive_read_free(3).
Once you're done reading data from the archive, you should call archive_read_close() to close the archive, and then call archive_read_free() to release all the resources, including all the memory the library allocated. clean up after yourself, bestie, the library is not your mom. π
β EXAMPLES β
The following illustrates basic usage of the library. In this example, the callback functions are simply wrappers around the standard open(2), read(2), and close(2) system calls. copy it, run it, obsessed. π₯
void
list_archive(const char *name)
{
struct mydata *mydata;
struct archive *a;
struct archive_entry *entry;
mydata = malloc(sizeof(struct mydata));
a = archive_read_new();
mydata->name = name;
archive_read_support_filter_all(a);
archive_read_support_format_all(a);
archive_read_open(a, mydata, myopen, myread, myclose);
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
printf("%s\n",archive_entry_pathname(entry));
archive_read_data_skip(a);
}
archive_read_free(a);
free(mydata);
}
la_ssize_t
myread(struct archive *a, void *client_data, const void **buff)
{
struct mydata *mydata = client_data;
*buff = mydata->buff;
return (read(mydata->fd, mydata->buff, 10240));
}
int
myopen(struct archive *a, void *client_data)
{
struct mydata *mydata = client_data;
mydata->fd = open(mydata->name, O_RDONLY);
return (mydata->fd >= 0 ? ARCHIVE_OK : ARCHIVE_FATAL);
}
int
myclose(struct archive *a, void *client_data)
{
struct mydata *mydata = client_data;
if (mydata->fd > 0)
close(mydata->fd);
return (ARCHIVE_OK);
}
π SEE ALSO π
tar(1), archive_read_data(3), archive_read_extract(3), archive_read_filter(3), archive_read_format(3), archive_read_header(3), archive_read_new(3), archive_read_open(3), archive_read_set_options(3), archive_util(3), libarchive(3), tar(5)
the whole friend group, fr fr. π«
πΈ HISTORY πΈ
The libarchive library first appeared in FreeBSD 5.3. a true OG. π
π AUTHORS π
The libarchive library was written by Tim Kientzle <kientzle@acm.org>. thank you king, we owe you everything πβ¨
π©· BUGS π©·
Many traditional archiver programs treat empty files as valid empty archives. For example, many implementations of tar(1) let you append entries to an empty file. But of course β and this is the whole problem β it is impossible to determine the format of an empty file by inspecting the contents. So this library treats empty files as having a special "empty" format. no cap, that's just how it is. π
and that's the page bestie!! π this one came from the Debian crew, dated February 2, 2012 β yes she's a 2012 baddie and she still eats. mwah, go read some archives, ily, bye!! π¦π β¨
π¦ where this page comes from
- arch rolling: libarchive 3.8.9-1
- centos-stream 10: libarchive-devel 3.7.7-11.el10 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- debian trixie: libarchive-dev 3.7.4-4+deb13u1 Β· BSD-2-Clause
- fedora 43: libarchive-devel 3.8.1-3.fc43 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 43: libarchive-devel 3.8.1-3.fc43 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 43: libarchive-devel 3.8.4-1.fc43 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 43: libarchive-devel 3.8.4-1.fc43 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 44: libarchive-devel 3.8.4-2.fc44 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 44: libarchive-devel 3.8.4-2.fc44 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 44: libarchive-devel 3.8.7-1.fc44 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- fedora 44: libarchive-devel 3.8.7-1.fc44 Β· BSD-2-Clause AND FSFULLR AND GPL-2.0-or-later WITH Libtool-exception AND BSD-3-Clause AND FSFUL
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixos 26.05: libarchive 3.8.9
- nixpkgs unstable: libarchive 3.8.9
- opensuse-leap 16.0: libarchive-devel 3.8.1-160000.4.1 Β· BSD-2-Clause
- opensuse-leap 16.0: libarchive-devel 3.8.1-160000.4.1 Β· BSD-2-Clause
- opensuse-leap 16.0: libarchive-devel 3.8.1-160000.4.1 Β· BSD-2-Clause
- opensuse-leap 16.0: libarchive-devel 3.8.1-160000.4.1 Β· BSD-2-Clause
- ubuntu questing: libarchive-dev 3.7.7-0ubuntu3
- ubuntu questing: libarchive-dev 3.7.7-0ubuntu3.2
- ubuntu resolute: libarchive-dev 3.8.5-1ubuntu2 Β· BSD-2-Clause
- ubuntu resolute: libarchive-dev 3.8.5-1ubuntu2.3 Β· BSD-2-Clause
- ubuntu stonking: libarchive-dev 3.8.9-1 Β· BSD-2-Clause
- Copyright (c) 2003-2007 Tim Kientzle
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms