Skip to content

Commit 4375f15

Browse files
authored
Merge pull request #85 from MartinFalatic/mff-fix-add-file-from-memory
2 parents 8999c11 + e9324f5 commit 4375f15

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

libarchive/write.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def add_file_from_memory(
8484
8585
:param entry_path: where entry should be places in archive
8686
:type entry_path: str
87-
:param entry_size: entire size of entry
87+
:param entry_size: entire size of entry in bytes
8888
:type entry_size: int
8989
:param entry_data: content of entry
90-
:type entry_data: iterable
90+
:type entry_data: bytes or Iterable[bytes]
9191
:param filetype: which type of file: normal, symlink etc.
9292
should entry be created as
9393
:type filetype: octal number
@@ -96,6 +96,13 @@ def add_file_from_memory(
9696
"""
9797
archive_pointer = self._pointer
9898

99+
if isinstance(entry_data, bytes):
100+
entry_data = (entry_data,)
101+
elif isinstance(entry_data, str):
102+
raise TypeError(
103+
"entry_data: expected bytes, got %r" % type(entry_data)
104+
)
105+
99106
with new_archive_entry() as archive_entry_pointer:
100107
archive_entry = ArchiveEntry(None, archive_entry_pointer)
101108

tests/test_rwx.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"""Test reading, writing and extracting archives."""
22

33
from __future__ import division, print_function, unicode_literals
4+
45
import io
6+
import json
57

68
import libarchive
79
from libarchive.extract import EXTRACT_OWNER, EXTRACT_PERM, EXTRACT_TIME
810
from libarchive.write import memory_writer
911
from mock import patch
12+
import pytest
1013

1114
from . import check_archive, in_dir, treestat
1215

@@ -115,24 +118,30 @@ def test_write_not_fail(write_fail_mock):
115118
assert not write_fail_mock.called
116119

117120

118-
def test_adding_entry_from_memory():
119-
entry_path = 'this is path'
120-
entry_data = 'content'
121-
entry_size = len(entry_data)
121+
@pytest.mark.parametrize(
122+
'archfmt,data_bytes',
123+
[('zip', b'content'),
124+
('gnutar', b''),
125+
('pax', json.dumps({'a': 1, 'b': 2, 'c': 3}).encode()),
126+
('7zip', b'lorem\0ipsum')])
127+
def test_adding_entry_from_memory(archfmt, data_bytes):
128+
entry_path = 'testfile.data'
129+
entry_data = data_bytes
130+
entry_size = len(data_bytes)
122131

123132
blocks = []
124133

125134
def write_callback(data):
126135
blocks.append(data[:])
127136
return len(data)
128137

129-
with libarchive.custom_writer(write_callback, 'zip') as archive:
138+
with libarchive.custom_writer(write_callback, archfmt) as archive:
130139
archive.add_file_from_memory(entry_path, entry_size, entry_data)
131140

132141
buf = b''.join(blocks)
133142
with libarchive.memory_reader(buf) as memory_archive:
134143
for archive_entry in memory_archive:
135-
assert entry_data.encode() == b''.join(
136-
archive_entry.get_blocks()
137-
)
144+
expected = entry_data
145+
actual = b''.join(archive_entry.get_blocks())
146+
assert expected == actual
138147
assert archive_entry.path == entry_path

0 commit comments

Comments
 (0)