def _test_tar(self, filename, arcfile, opener): mem = fs.open_fs('mem://') # Check we can write with fs.archive.open_archive(mem, filename) as archive: self.assertIsInstance(archive, fs.archive.tarfs.TarFS) archive.settext('abc.txt', 'abc') archive.makedir('dir') # Check the dumped archive is a gzipped file with mem.openbin(filename) as mytar: arc = arcfile(mytar) arc.read(1) # Check we can open it normally with mem.openbin(filename) as mytar: tar = getattr(TarFile, opener)(filename, fileobj=mytar) with mem.openbin(filename) as mytar: tar = TarFile.open(filename, fileobj=mytar) # Check we can read it with the TarFS with fs.archive.open_archive(mem, filename) as archive: self.assertEqual(sorted(archive.listdir('/')), ['abc.txt', 'dir']) self.assertEqual(archive.gettext('abc.txt'), 'abc')
def test_open_read_only(self): mem = fs.open_fs('mem://') with fs.archive.open_archive(mem, 'myzip.zip') as archive: self.assertIsInstance(archive, fs.archive.zipfs.ZipFS) archive.settext('abc.txt', 'abc') archive.makedir('dir') mem = WrapReadOnly(mem) with fs.archive.open_archive(mem, 'myzip.zip') as archive: self.assertIsInstance(archive, fs.archive.zipfs.ZipReadFS)
def test_zip(self): """Check ``*.zip`` files are opened in `ZipFS` filesystems. """ mem = fs.open_fs('mem://') with fs.archive.open_archive(mem, 'myzip.zip') as archive: self.assertIsInstance(archive, ZipFS) archive.settext('abc.txt', 'abc') archive.makedir('dir') with fs.archive.open_archive(mem, 'myzip.zip') as archive: self.assertEqual(sorted(archive.listdir('/')), ['abc.txt', 'dir']) self.assertEqual(archive.gettext('abc.txt'), 'abc') with mem.openbin('myzip.zip') as myzip: self.assertTrue(zipfile.is_zipfile(myzip))