2626# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
29- import os , unittest , tempfile , random , string , subprocess , sys
29+ import os , unittest , tempfile , random , string , sys
30+ import zipfile
31+ import io
3032
31- from libarchive import is_archive_name , is_archive
33+ from libarchive import Archive , is_archive_name , is_archive
3234from libarchive .zip import is_zipfile , ZipFile , ZipEntry
3335
3436PY3 = sys .version_info [0 ] == 3
3537
36- TMPDIR = tempfile .mkdtemp ()
37- ZIPCMD = '/usr/bin/zip'
38+ TMPDIR = tempfile .mkdtemp (suffix = '.python-libarchive' )
3839ZIPFILE = 'test.zip'
3940ZIPPATH = os .path .join (TMPDIR , ZIPFILE )
4041
@@ -54,13 +55,10 @@ def make_temp_files():
5455
5556
5657def make_temp_archive ():
57- if not os .access (ZIPCMD , os .X_OK ):
58- raise AssertionError ('Cannot execute %s.' % ZIPCMD )
59- cmd = [ZIPCMD , ZIPFILE ]
6058 make_temp_files ()
61- cmd . extend ( FILENAMES )
62- os . chdir ( TMPDIR )
63- subprocess . call ( cmd , stdout = subprocess . PIPE )
59+ with zipfile . ZipFile ( ZIPPATH , mode = "w" ) as z :
60+ for name in FILENAMES :
61+ z . write ( os . path . join ( TMPDIR , name ), arcname = name )
6462
6563
6664class TestIsArchiveName (unittest .TestCase ):
@@ -152,10 +150,7 @@ def test_filenames(self):
152150 z = ZipFile (self .f , 'r' )
153151 names = []
154152 for e in z :
155- if PY3 :
156- names .append (e .filename )
157- else :
158- names .append (e .filename [0 ])
153+ names .append (e .filename )
159154 self .assertEqual (names , FILENAMES , 'File names differ in archive.' )
160155
161156 #~ def test_non_ascii(self):
@@ -250,5 +245,35 @@ def test_deferred_close_by_archive(self):
250245 self .assertIsNone (z ._stream )
251246 z .close ()
252247
248+
249+ class TestHighLevelAPI (unittest .TestCase ):
250+ def setUp (self ):
251+ make_temp_archive ()
252+
253+ def _test_listing_content (self , f ):
254+ """ Test helper capturing file paths while iterating the archive. """
255+ found = []
256+ with Archive (f ) as a :
257+ for entry in a :
258+ found .append (entry .pathname )
259+
260+ self .assertEqual (set (found ), set (FILENAMES ))
261+
262+ def test_open_by_name (self ):
263+ """ Test an archive opened directly by name. """
264+ self ._test_listing_content (ZIPPATH )
265+
266+ def test_open_by_named_fobj (self ):
267+ """ Test an archive using a file-like object opened by name. """
268+ with open (ZIPPATH , 'rb' ) as f :
269+ self ._test_listing_content (f )
270+
271+ def test_open_by_unnamed_fobj (self ):
272+ """ Test an archive using file-like object opened by fileno(). """
273+ with open (ZIPPATH , 'rb' ) as zf :
274+ with io .FileIO (zf .fileno (), mode = 'r' , closefd = False ) as f :
275+ self ._test_listing_content (f )
276+
277+
253278if __name__ == '__main__' :
254279 unittest .main ()
0 commit comments