A Python interface to libarchive. It uses the standard ctypes_ module to
dynamically load and access the C library.
.. _ctypes: https://docs.python.org/3/library/ctypes.html
Installation
============
pip install libarchive-c
Compatibility
=============
python
------
python-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.
libarchive
----------
python-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. with ``brew install libarchive`` on MacOS) and use the ``LIBARCHIVE`` environment variable to point python-libarchive-c to it::
export LIBARCHIVE=/usr/local/Cellar/libarchive/3.3.3/lib/libarchive.13.dylib
Usage
=====
Import::
import libarchive
Extracting archives
-------------------
To extract an archive, use the ``extract_file`` function::
os.chdir('/path/to/target/directory')
libarchive.extract_file('test.zip')
Alternatively, the ``extract_memory`` function can be used to extract from a buffer,
and ``extract_fd`` from a file descriptor.
The ``extract_*`` functions all have an integer ``flags`` argument which is passed
directly to the C function ``archive_write_disk_set_options()``. You can import
the ``EXTRACT_*`` constants from the ``libarchive.extract`` module and see the
official description of each flag in the ``archive_write_disk(3)`` man page.
By default, when the ``flags`` argument is ``None``, the ``SECURE_NODOTDOT``,
``SECURE_NOABSOLUTEPATHS`` and ``SECURE_SYMLINKS`` flags are passed to
libarchive, unless the current directory is the root (``/``).
Reading archives
----------------
To read an archive, use the ``file_reader`` function::
with libarchive.file_reader('test.7z') as archive:
for entry in archive:
for block in entry.get_blocks():
...
Alternatively, the ``memory_reader`` function can be used to read from a buffer,
``fd_reader`` from a file descriptor, ``stream_reader`` from a stream object
(which must support the standard ``readinto`` method), and ``custom_reader``
from anywhere using callbacks.
To learn about the attributes of the ``entry`` object, see the ``libarchive/entry.py``
source code or run ``help(libarchive.entry.ArchiveEntry)`` in a Python shell.
Displaying progress
~~~~~~~~~~~~~~~~~~~
If your program processes large archives, you can keep track of its progress
with the ``bytes_read`` attribute. Here's an example of a progress bar using
`tqdm <https://pypi.org/project/tqdm/>`_::
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 archives
-----------------
To create an archive, use the ``file_writer`` function::
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, the ``memory_writer`` function can be used to write to a memory buffer,
``fd_writer`` to a file descriptor, and ``custom_writer`` to 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 in ``libarchive.ffi.WRITE_FORMATS``
and ``libarchive.ffi.WRITE_FILTERS``.
File metadata codecs
--------------------
By default, UTF-8 is used to read and write file attributes from and to archives.
A different codec can be specified through the ``header_codec`` arguments of the
``*_reader`` and ``*_writer`` functions. 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 (``pathname`` and ``linkpath``), the specified codec is
used to encode and decode user and group names (``uname`` and ``gname``).
License
=======
`CC0 Public Domain Dedication <http://creativecommons.org/publicdomain/zero/1.0/>`_
Raw data
{
"_id": null,
"home_page": "https://github.com/Changaco/python-libarchive-c",
"name": "libarchive-c",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "archive libarchive 7z tar bz2 zip gz",
"author": "Changaco",
"author_email": "changaco@changaco.oy.lc",
"download_url": "https://files.pythonhosted.org/packages/59/d6/eab966f12b33a97c78d319c38a38105b3f843cf7d79300650b7ac8c9d349/libarchive-c-5.0.tar.gz",
"platform": null,
"description": "A Python interface to libarchive. It uses the standard ctypes_ module to\ndynamically load and access the C library.\n\n.. _ctypes: https://docs.python.org/3/library/ctypes.html\n\nInstallation\n============\n\n pip install libarchive-c\n\nCompatibility\n=============\n\npython\n------\n\npython-libarchive-c is currently tested with python 3.8, 3.9, 3.10 and 3.11.\n\nIf you find an incompatibility with older versions you can send us a small patch,\nbut we won't accept big changes.\n\nlibarchive\n----------\n\npython-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. with ``brew install libarchive`` on MacOS) and use the ``LIBARCHIVE`` environment variable to point python-libarchive-c to it::\n\n export LIBARCHIVE=/usr/local/Cellar/libarchive/3.3.3/lib/libarchive.13.dylib\n\nUsage\n=====\n\nImport::\n\n import libarchive\n\nExtracting archives\n-------------------\n\nTo extract an archive, use the ``extract_file`` function::\n\n os.chdir('/path/to/target/directory')\n libarchive.extract_file('test.zip')\n\nAlternatively, the ``extract_memory`` function can be used to extract from a buffer,\nand ``extract_fd`` from a file descriptor.\n\nThe ``extract_*`` functions all have an integer ``flags`` argument which is passed\ndirectly to the C function ``archive_write_disk_set_options()``. You can import\nthe ``EXTRACT_*`` constants from the ``libarchive.extract`` module and see the\nofficial description of each flag in the ``archive_write_disk(3)`` man page.\n\nBy default, when the ``flags`` argument is ``None``, the ``SECURE_NODOTDOT``,\n``SECURE_NOABSOLUTEPATHS`` and ``SECURE_SYMLINKS`` flags are passed to\nlibarchive, unless the current directory is the root (``/``).\n\nReading archives\n----------------\n\nTo read an archive, use the ``file_reader`` function::\n\n with libarchive.file_reader('test.7z') as archive:\n for entry in archive:\n for block in entry.get_blocks():\n ...\n\nAlternatively, the ``memory_reader`` function can be used to read from a buffer,\n``fd_reader`` from a file descriptor, ``stream_reader`` from a stream object\n(which must support the standard ``readinto`` method), and ``custom_reader``\nfrom anywhere using callbacks.\n\nTo learn about the attributes of the ``entry`` object, see the ``libarchive/entry.py``\nsource code or run ``help(libarchive.entry.ArchiveEntry)`` in a Python shell.\n\nDisplaying progress\n~~~~~~~~~~~~~~~~~~~\n\nIf your program processes large archives, you can keep track of its progress\nwith the ``bytes_read`` attribute. Here's an example of a progress bar using\n`tqdm <https://pypi.org/project/tqdm/>`_::\n\n with tqdm(total=os.stat(archive_path).st_size, unit='bytes') as pbar, \\\n libarchive.file_reader(archive_path) as archive:\n for entry in archive:\n ...\n pbar.update(archive.bytes_read - pbar.n)\n\nCreating archives\n-----------------\n\nTo create an archive, use the ``file_writer`` function::\n\n from libarchive.entry import FileType\n\n with libarchive.file_writer('test.tar.gz', 'ustar', 'gzip') as archive:\n # Add the `libarchive/` directory and everything in it (recursively),\n # then the `README.rst` file.\n archive.add_files('libarchive/', 'README.rst')\n # Add a regular file defined from scratch.\n data = b'foobar'\n archive.add_file_from_memory('../escape-test', len(data), data)\n # Add a directory defined from scratch.\n early_epoch = (42, 42) # 1970-01-01 00:00:42.000000042\n archive.add_file_from_memory(\n 'metadata-test', 0, b'',\n filetype=FileType.DIRECTORY, permission=0o755, uid=4242, gid=4242,\n atime=early_epoch, mtime=early_epoch, ctime=early_epoch, birthtime=early_epoch,\n )\n\nAlternatively, the ``memory_writer`` function can be used to write to a memory buffer,\n``fd_writer`` to a file descriptor, and ``custom_writer`` to a callback function.\n\nFor each of those functions, the mandatory second argument is the archive format,\nand the optional third argument is the compression format (called \u201cfilter\u201d in\nlibarchive). The acceptable values are listed in ``libarchive.ffi.WRITE_FORMATS``\nand ``libarchive.ffi.WRITE_FILTERS``.\n\nFile metadata codecs\n--------------------\n\nBy default, UTF-8 is used to read and write file attributes from and to archives.\nA different codec can be specified through the ``header_codec`` arguments of the\n``*_reader`` and ``*_writer`` functions. Example::\n\n with libarchive.file_writer('test.tar', 'ustar', header_codec='cp037') as archive:\n ...\n with file_reader('test.tar', header_codec='cp037') as archive:\n ...\n\nIn addition to file paths (``pathname`` and ``linkpath``), the specified codec is\nused to encode and decode user and group names (``uname`` and ``gname``).\n\nLicense\n=======\n\n`CC0 Public Domain Dedication <http://creativecommons.org/publicdomain/zero/1.0/>`_\n",
"bugtrack_url": null,
"license": "CC0",
"summary": "Python interface to libarchive",
"version": "5.0",
"project_urls": {
"Homepage": "https://github.com/Changaco/python-libarchive-c"
},
"split_keywords": [
"archive",
"libarchive",
"7z",
"tar",
"bz2",
"zip",
"gz"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "41bac46872fcf96f4dc7752a33e7c18298faa8e01f19dba1c77284feae50fb3e",
"md5": "6267da94fc687252b1571f6ff162479f",
"sha256": "3ed7ee9b7d7d6fc200aecce63cee2084754cb6c00e946f6d007b80236e662bff"
},
"downloads": -1,
"filename": "libarchive_c-5.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "6267da94fc687252b1571f6ff162479f",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 15694,
"upload_time": "2023-07-04T08:54:39",
"upload_time_iso_8601": "2023-07-04T08:54:39.391053Z",
"url": "https://files.pythonhosted.org/packages/41/ba/c46872fcf96f4dc7752a33e7c18298faa8e01f19dba1c77284feae50fb3e/libarchive_c-5.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "59d6eab966f12b33a97c78d319c38a38105b3f843cf7d79300650b7ac8c9d349",
"md5": "e06c27b4377c77f4449ad9e8d47a8f44",
"sha256": "d673f56673d87ec740d1a328fa205cafad1d60f5daca4685594deb039d32b159"
},
"downloads": -1,
"filename": "libarchive-c-5.0.tar.gz",
"has_sig": false,
"md5_digest": "e06c27b4377c77f4449ad9e8d47a8f44",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 52186,
"upload_time": "2023-07-04T08:54:41",
"upload_time_iso_8601": "2023-07-04T08:54:41.818445Z",
"url": "https://files.pythonhosted.org/packages/59/d6/eab966f12b33a97c78d319c38a38105b3f843cf7d79300650b7ac8c9d349/libarchive-c-5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-07-04 08:54:41",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Changaco",
"github_project": "python-libarchive-c",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "libarchive-c"
}