ARCHIVE_WRITE(3)

πŸ“¦
version 3.8.9 Β· 3 versions
, in arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, freebsd 14.5, freebsd 15.1, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ NAME ✨

archive_write β€” functions for creating archives

okay so this is the ARCHIVE_WRITE(3) page from the Library Functions Manual, and bestie, it's literally the whole toolkit for making archives instead of reading them πŸ’–


πŸŽ€ LIBRARY πŸŽ€

Streaming Archive Library (libarchive, -larchive)


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <archive.h>

πŸ’… DESCRIPTION πŸ’…

These functions serve up a whole complete API for creating streaming archive files, no cap. The general process is:

  1. first create the struct archive object
  2. set any desired options
  3. initialize the archive
  4. append entries
  5. then close the archive and release all resources

🌸 Create archive object 🌸

See archive_write_new(3).

To write an archive, you must first obtain an initialized struct archive object from archive_write_new(). Like, that's step zero, you can't skip it 🫢


🌈 Enable filters and formats, configure block size and padding 🌈

See archive_write_filter(3), archive_write_format(3) and archive_write_blocksize(3).

You can then modify this object for the desired operations with the various archive_write_set_XXX() functions. In particular, you will need to invoke appropriate archive_write_add_XXX() and archive_write_set_XXX() functions to enable the corresponding compression and format support. That's the part where you tell it what kind of archive you're making and how it gets squished ✨


πŸ“ Set options πŸ“

See archive_write_set_options(3).


πŸ’œ Open archive πŸ’œ

See archive_write_open(3).

Once you have prepared the struct archive object, you call archive_write_open() to actually open the archive and prepare it for writing. 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 write the archive data.


🧚 Produce archive 🧚

See archive_write_header(3) and archive_write_data(3).

Individual archive entries are written in a three-step process: You first initialize a struct archive_entry structure with information about the new entry. At a minimum, you should set the pathname of the entry and provide a struct stat with a valid st_mode field, which specifies the type of object and st_size field, which specifies the size of the data portion of the object.


🌟 Release resources 🌟

See archive_write_free(3).

After all entries have been written, use the archive_write_free() function to release all resources. Don't be a resource-leaker, bestie 😭


πŸ”₯ EXAMPLES πŸ”₯

The following sketch illustrates basic usage of the library. In this example, the callback functions are simply wrappers around the standard open(2), write(2), and close(2) system calls.

#ifdef __linux__
#define _FILE_OFFSET_BITS 64
#endif
#include <sys/stat.h>
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

struct mydata {
  const char *name;
  int fd;
};

int
myopen(struct archive *a, void *client_data)
{
  struct mydata *mydata = client_data;

  mydata->fd = open(mydata->name, O_WRONLY | O_CREAT, 0644);
  if (mydata->fd >= 0)
    return (ARCHIVE_OK);
  else
    return (ARCHIVE_FATAL);
}

la_ssize_t
mywrite(struct archive *a, void *client_data, const void *buff, size_t n)
{
  struct mydata *mydata = client_data;

  return (write(mydata->fd, buff, n));
}

int
myclose(struct archive *a, void *client_data)
{
  struct mydata *mydata = client_data;

  if (mydata->fd > 0)
    close(mydata->fd);
  return (0);
}

void
write_archive(const char *outname, const char **filename)
{
  struct mydata *mydata = malloc(sizeof(struct mydata));
  struct archive *a;
  struct archive_entry *entry;
  struct stat st;
  char buff[8192];
  int len;
  int fd;

  a = archive_write_new();
  mydata->name = outname;
  /* Set archive format and filter according to output file extension.
   * If it fails, set default format. Platform depended function.
   * See supported formats in archive_write_set_format_filter_by_ext.c */
  if (archive_write_set_format_filter_by_ext(a, outname) != ARCHIVE_OK)  {
    archive_write_add_filter_gzip(a);
    archive_write_set_format_ustar(a);
  }
  archive_write_open(a, mydata, myopen, mywrite, myclose);
  while (*filename) {
    stat(*filename, &st);
    entry = archive_entry_new();
    archive_entry_copy_stat(entry, &st);
    archive_entry_set_pathname(entry, *filename);
    archive_write_header(a, entry);
    if ((fd = open(*filename, O_RDONLY)) != -1) {
      len = read(fd, buff, sizeof(buff));
      while (len > 0) {
        archive_write_data(a, buff, len);
        len = read(fd, buff, sizeof(buff));
      }
      close(fd);
    }
    archive_entry_free(entry);
    filename++;
  }
  archive_write_free(a);
}

int main(int argc, const char **argv)
{
  const char *outname;
  argv++;
  outname = *argv++;
  write_archive(outname, argv);
  return 0;
}

πŸ‘‘ SEE ALSO πŸ‘‘

tar(1), archive_write_set_options(3), libarchive(3), cpio(5), mtree(5), tar(5)


⭐ HISTORY ⭐

The libarchive library first appeared in FreeBSD 5.3.


🫢 AUTHORS 🫢

The libarchive library was written by Tim Kientzle <kientzle@acm.org>.


πŸ’€ BUGS πŸ’€

There are many peculiar bugs in historic tar implementations that may cause certain programs to reject archives written by this library. For example, several historic implementations calculated header checksums incorrectly and will thus reject valid archives; GNU tar does not fully support pax interchange format; some old tar implementations required specific field terminations.

The default pax interchange format eliminates most of the historic tar limitations and provides a generic key/value attribute facility for vendor-defined extensions. One oversight in POSIX is the failure to provide a standard attribute for large device numbers. This library uses SCHILY.devminor and SCHILY.devmajor for device numbers that exceed the range supported by the backwards-compatible ustar header. These keys are compatible with Joerg Schilling's star archiver. Other implementations may not recognize these keys and will thus be unable to correctly restore device nodes with large device numbers from archives created by this library.


ngl the historic-tar drama is real πŸ’€ those old implementations are out here rejecting perfectly valid archives because of checksum math and field termination beef. not our fault, but we do have to live with it.

and that's the whole page, bestie πŸŽ€ this Debian build of the page is dated February 2, 2012, so if you're reading it in a terminal somewhere in the future: hi, hope your archive wrote cleanly, ily, mwah πŸ’‹

πŸ“¦ where this page comes from