Package: libarchive-c
A Python interface to libarchive. It uses the standardctypesmodule to
dynamically load and access the C library.Installationpip install libarchive-cCompatibilitypythonpython-libarchive-c is currently tested with python 3.8, 3.9, 3.10 and 3.11.If you find an incompatibility with older versions you can send us a small patch,
but we won’t accept big changes.libarchivepython-libarchive-c may not work properly with obsolete versions of libarchive such as the ones included in MacOS. In that case you can install a recent version of libarchive (e.g. withbrew install libarchiveon MacOS) and use theLIBARCHIVEenvironment variable to point python-libarchive-c to it:export LIBARCHIVE=/usr/local/Cellar/libarchive/3.3.3/lib/libarchive.13.dylibUsageImport:import libarchiveExtracting archivesTo extract an archive, use theextract_filefunction:os.chdir('/path/to/target/directory')
libarchive.extract_file('test.zip')Alternatively, theextract_memoryfunction can be used to extract from a buffer,
andextract_fdfrom a file descriptor.Theextract_*functions all have an integerflagsargument which is passed
directly to the C functionarchive_write_disk_set_options(). You can import
theEXTRACT_*constants from thelibarchive.extractmodule and see the
official description of each flag in thearchive_write_disk(3)man page.By default, when theflagsargument isNone, theSECURE_NODOTDOT,SECURE_NOABSOLUTEPATHSandSECURE_SYMLINKSflags are passed to
libarchive, unless the current directory is the root (/).Reading archivesTo read an archive, use thefile_readerfunction:with libarchive.file_reader('test.7z') as archive:
    for entry in archive:
        for block in entry.get_blocks():
            ...Alternatively, thememory_readerfunction can be used to read from a buffer,fd_readerfrom a file descriptor,stream_readerfrom a stream object
(which must support the standardreadintomethod), andcustom_readerfrom anywhere using callbacks.To learn about the attributes of theentryobject, see thelibarchive/entry.pysource code or runhelp(libarchive.entry.ArchiveEntry)in a Python shell.Displaying progressIf your program processes large archives, you can keep track of its progress
with thebytes_readattribute. Here’s an example of a progress bar usingtqdm:with tqdm(total=os.stat(archive_path).st_size, unit='bytes') as pbar, \
     libarchive.file_reader(archive_path) as archive:
    for entry in archive:
        ...
        pbar.update(archive.bytes_read - pbar.n)Creating archivesTo create an archive, use thefile_writerfunction:from libarchive.entry import FileType

with libarchive.file_writer('test.tar.gz', 'ustar', 'gzip') as archive:
    # Add the `libarchive/` directory and everything in it (recursively),
    # then the `README.rst` file.
    archive.add_files('libarchive/', 'README.rst')
    # Add a regular file defined from scratch.
    data = b'foobar'
    archive.add_file_from_memory('../escape-test', len(data), data)
    # Add a directory defined from scratch.
    early_epoch = (42, 42)  # 1970-01-01 00:00:42.000000042
    archive.add_file_from_memory(
        'metadata-test', 0, b'',
        filetype=FileType.DIRECTORY, permission=0o755, uid=4242, gid=4242,
        atime=early_epoch, mtime=early_epoch, ctime=early_epoch, birthtime=early_epoch,
    )Alternatively, thememory_writerfunction can be used to write to a memory buffer,fd_writerto a file descriptor, andcustom_writerto a callback function.For each of those functions, the mandatory second argument is the archive format,
and the optional third argument is the compression format (called “filter” in
libarchive). The acceptable values are listed inlibarchive.ffi.WRITE_FORMATSandlibarchive.ffi.WRITE_FILTERS.File metadata codecsBy default, UTF-8 is used to read and write file attributes from and to archives.
A different codec can be specified through theheader_codecarguments of the*_readerand*_writerfunctions. Example:with libarchive.file_writer('test.tar', 'ustar', header_codec='cp037') as archive:
    ...
with file_reader('test.tar', header_codec='cp037') as archive:
    ...In addition to file paths (pathnameandlinkpath), the specified codec is
used to encode and decode user and group names (unameandgname).LicenseCC0 Public Domain Dedication