You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+67-12Lines changed: 67 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,32 +36,87 @@ Import::
36
36
37
37
import libarchive
38
38
39
-
To extract an archive to the current directory::
39
+
Extracting archives
40
+
-------------------
40
41
42
+
To extract an archive, use the ``extract_file`` function::
43
+
44
+
os.chdir('/path/to/target/directory')
41
45
libarchive.extract_file('test.zip')
42
46
43
-
``extract_memory`` extracts from a buffer instead, and ``extract_fd`` extracts
44
-
from a file descriptor.
47
+
Alternatively, the ``extract_memory`` function can be used to extract from a buffer,
48
+
and ``extract_fd`` from a file descriptor.
49
+
50
+
The ``extract_*`` functions all have an integer ``flags`` argument which is passed
51
+
directly to the C function ``archive_write_disk_set_options()``. You can import
52
+
the ``EXTRACT_*`` constants from the ``libarchive.extract`` module and see the
53
+
official description of each flag in the ``archive_write_disk(3)`` man page.
54
+
55
+
By default, when the ``flags`` argument is ``None``, the ``SECURE_NODOTDOT``,
56
+
``SECURE_NOABSOLUTEPATHS`` and ``SECURE_SYMLINKS`` flags are passed to
57
+
libarchive, unless the current directory is the root (``/``).
58
+
59
+
Reading archives
60
+
----------------
45
61
46
-
To read an archive::
62
+
To read an archive, use the ``file_reader`` function::
47
63
48
64
with libarchive.file_reader('test.7z') as archive:
49
65
for entry in archive:
50
66
for block in entry.get_blocks():
51
67
...
52
68
53
-
``memory_reader`` reads from a memory buffer instead, and ``fd_reader`` reads
54
-
from a file descriptor.
69
+
Alternatively, the ``memory_reader`` function can be used to read from a buffer,
70
+
``fd_reader`` from a file descriptor, ``stream_reader`` from a stream object
71
+
(which must support the standard ``readinto`` method), and ``custom_reader``
72
+
from anywhere using callbacks.
55
73
56
-
To create an archive::
74
+
To learn about the attributes of the ``entry`` object, see the ``libarchive/entry.py``
75
+
source code or run ``help(libarchive.entry.ArchiveEntry)`` in a Python shell.
57
76
58
-
with libarchive.file_writer('test.tar.gz', 'ustar', 'gzip') as archive:
59
-
archive.add_files('libarchive/', 'README.rst')
77
+
Displaying progress
78
+
~~~~~~~~~~~~~~~~~~~
79
+
80
+
The ``stream_reader`` function and the standard `tell <https://docs.python.org/3/library/io.html#io.IOBase.tell>`_ method can be used to estimate how much of an archive has been read by your program. Here's an example of a progress bar using `tqdm <https://pypi.org/project/tqdm/>`_::
60
81
61
-
``memory_writer`` writes to a memory buffer instead, ``fd_writer`` writes to a
62
-
file descriptor, and ``custom_writer`` sends the data to a callback function.
82
+
with tqdm(total=os.stat(archive_path).st_size, unit='bytes') as pbar, \
83
+
open(archive_path, 'rb') as file, \
84
+
libarchive.stream_reader(file) as archive:
85
+
for entry in archive:
86
+
...
87
+
pbar.update(file.tell() - pbar.n)
88
+
89
+
Of course this is only useful if your program processes large archives.
90
+
91
+
Creating archives
92
+
-----------------
93
+
94
+
To create an archive, use the ``file_writer`` function::
63
95
64
-
You can also find more thorough examples in the ``tests/`` directory.
96
+
from libarchive.entry import FileType
97
+
98
+
with libarchive.file_writer('test.tar.gz', 'ustar', 'gzip') as archive:
99
+
# Add the `libarchive/` directory and everything in it (recursively),
0 commit comments