Skip to content

Commit d80ac30

Browse files
committed
create bytes_read and bytes_written properties
closes #112
1 parent 871aa27 commit d80ac30

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

README.rst

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,15 @@ source code or run ``help(libarchive.entry.ArchiveEntry)`` in a Python shell.
7777
Displaying progress
7878
~~~~~~~~~~~~~~~~~~~
7979

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/>`_::
80+
If your program processes large archives, you can keep track of its progress
81+
with the ``bytes_read`` attribute. Here's an example of a progress bar using
82+
`tqdm <https://pypi.org/project/tqdm/>`_::
8183

8284
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+
libarchive.file_reader(archive_path) as archive:
8586
for entry in archive:
8687
...
87-
pbar.update(file.tell() - pbar.n)
88-
89-
Of course this is only useful if your program processes large archives.
88+
pbar.update(archive.bytes_read - pbar.n)
9089

9190
Creating archives
9291
-----------------

libarchive/ffi.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def get_write_filter_function(filter_name):
161161

162162
errno = ffi('errno', [c_archive_p], c_int)
163163
error_string = ffi('error_string', [c_archive_p], c_char_p)
164+
ffi('filter_bytes', [c_archive_p, c_int], c_longlong)
164165

165166
# archive_entry
166167

libarchive/read.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ def __iter__(self):
2727
return
2828
yield entry
2929

30+
@property
31+
def bytes_read(self):
32+
return ffi.filter_bytes(self._pointer, -1)
33+
3034

3135
@contextmanager
3236
def new_archive_read(format_name='all', filter_name='all', passphrase=None):

libarchive/write.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ def new_archive_write(format_name, filter_name=None, options='', passphrase=None
184184
ffi.write_free(archive_p)
185185
raise
186186

187+
@property
188+
def bytes_written(self):
189+
return ffi.filter_bytes(self._pointer, -1)
190+
187191

188192
@contextmanager
189193
def custom_writer(

0 commit comments

Comments
 (0)