Open SamuelMarks opened 2 years ago
The "Examples" page in the Wiki walks through a series of increasingly-complex uses of libarchive, culminating in a "Complete Extractor" that is a good place to start from: https://github.com/libarchive/libarchive/wiki/Examples
In your first code above, I think you want archive_read_support_format_all
instead of format_raw
. Remember that libarchive is broken up into "format" modules that handle particular archive types and "filter" modules that deal with various encodings of the resulting archive. Your example code above enables all filters (which are generally used only by tar and cpio archives) but only the "raw" archive format (which is a specialized tool for handling things that are not actually archives at all).
You're also writing the resulting data to stdout via write(1, buff, size)
which is why you're seeing binary data on your terminal. The "Complete Extractor" example shows how to use the "archive_write_disk" tools to push the data into a directory hierarchy. In essence, "archive_write_disk" treats a directory on disk as if it were an archive: "creating an entry" becomes "creating a file", etc. So the basic outline for extraction is to copy entries (and their data) from the input archive (the Zip or Tar file you want to read) to another (the directory tree you want to create). In between these, you can modify the "entry" in any way you wish (alter filenames, permissions, etc).
Similarly, examples/untar.c
currently only enables archive_read_support_format_tar
so it only handles tar format, which is why you're seeing checksum failures trying to extract something that is not a tar archive. You can change that to archive_read_support_format_zip
to handle only zip archives or archive_read_support_format_all
to enable (almost) all of the formats that libarchive supports.
You might find the examples/minitar
example program a more useful starting point, as it is more complete than examples/untar.c
Thanks @kientzle; I've started incorporating that. Found a few type discrepancies in your wiki, will start to edit sometime today or tomorrow.
The "Examples" page in the Wiki walks through a series of increasingly-complex uses of libarchive, culminating in a "Complete Extractor" that is a good place to start from: https://github.com/libarchive/libarchive/wiki/Examples
In your first code above, I think you want
archive_read_support_format_all
instead offormat_raw
. Remember that libarchive is broken up into "format" modules that handle particular archive types and "filter" modules that deal with various encodings of the resulting archive. Your example code above enables all filters (which are generally used only by tar and cpio archives) but only the "raw" archive format (which is a specialized tool for handling things that are not actually archives at all).You're also writing the resulting data to stdout via
write(1, buff, size)
which is why you're seeing binary data on your terminal. The "Complete Extractor" example shows how to use the "archive_write_disk" tools to push the data into a directory hierarchy. In essence, "archive_write_disk" treats a directory on disk as if it were an archive: "creating an entry" becomes "creating a file", etc. So the basic outline for extraction is to copy entries (and their data) from the input archive (the Zip or Tar file you want to read) to another (the directory tree you want to create). In between these, you can modify the "entry" in any way you wish (alter filenames, permissions, etc).Similarly,
examples/untar.c
currently only enablesarchive_read_support_format_tar
so it only handles tar format, which is why you're seeing checksum failures trying to extract something that is not a tar archive. You can change that toarchive_read_support_format_zip
to handle only zip archives orarchive_read_support_format_all
to enable (almost) all of the formats that libarchive supports.You might find the
examples/minitar
example program a more useful starting point, as it is more complete thanexamples/untar.c
Which command in minitar
is to extract files into a specific folder?
@dandingol03 In my aforementioned project there's:
int extract_archive(enum Archive archive, const char *archive_filepath, const char *output_folder);
How do I use libarchive to extract any archive it supports, and inflate to a specific directory on disk?
Attempts:
0.
contrib/untar.c
, butverify_checksum
always fails on my archive (.zip). Is there averify_checksum
somewhere in libarchive that I should be using, that verifies differently depending on archive format?Also in both examples I get a whole lot of binary text dumped to my stdout.
Thanks for any suggestions