def setUp(self): self.temp_filename = "".join( random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(6)) + ".zip" self.temp_filename = os.path.join(tempfile.gettempdir(), self.temp_filename) zip_fs = zipfs.ZipFS(self.temp_filename, 'w') def makefile(filename, contents): if dirname(filename): zip_fs.makedir(dirname(filename), recursive=True, allow_recreate=True) f = zip_fs.open(filename, 'w') f.write(contents) f.close() makefile("a.txt", "Hello, World!") makefile("b.txt", "b") zip_fs.close() zip_fs = zipfs.ZipFS(self.temp_filename, 'a') makefile("foo/bar/baz.txt", "baz") makefile( u"\N{GREEK SMALL LETTER ALPHA}/\N{GREEK CAPITAL LETTER OMEGA}.txt", "this is the alpha and the omega") makefile("foo/second.txt", "hai") zip_fs.close()
def test_unicode_paths(self): # https://github.com/PyFilesystem/pyfilesystem2/issues/135 with zipfs.ZipFS(self._temp_path, write=True) as zip_fs: zip_fs.settext("Файл", "some content") with zipfs.ZipFS(self._temp_path) as zip_fs: paths = list(zip_fs.walk.files()) for path in paths: with zip_fs.openbin(path) as f: f.read()
def test_read_non_existent_file(self): fs = zipfs.ZipFS(open(self._temp_path, "rb")) # it has been very difficult to catch exception in __del__() del fs._zip try: fs.close() except AttributeError: self.fail("Could not close tar fs properly") except Exception: self.fail("Strange exception in closing fs")
def _package(self, outfile): os.makedirs(os.path.dirname(outfile), exist_ok=True) if self.config['Store As']['Type'].lower() == 'tar': savefs = tarfs.TarFS( outfile, write=True, compression=self.config['Store As']['Compression'] ) elif self.config['Store As']['Type'].lower() == 'zip': savefs = zipfs.ZipFS(outfile) else: _config_error("Store AS: Type is invalid") file_io.join_files(self.tmp, savefs)
def setUp(self): self.temp_filename = "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(6))+".zip" self.temp_filename = os.path.join(tempfile.gettempdir(), self.temp_filename) self.zf = zipfile.ZipFile(self.temp_filename, "w") zf = self.zf zf.writestr("a.txt", b("Hello, World!")) zf.writestr("b.txt", b("b")) zf.writestr("1.txt", b("1")) zf.writestr("foo/bar/baz.txt", b("baz")) zf.writestr("foo/second.txt", b("hai")) zf.close() self.fs = zipfs.ZipFS(self.temp_filename, "r")
def test_implied(self): """Test zipfs creates intermediate directories.""" fh, path = tempfile.mkstemp('testzip.zip') try: os.close(fh) _zip_file = zipfile.ZipFile(path, mode='w') _zip_file.writestr('foo/bar/baz/egg', b'hello') _zip_file.close() zip_fs = zipfs.ZipFS(path) zip_fs.getinfo('foo') zip_fs.getinfo('foo/bar') zip_fs.getinfo('foo/bar/baz') self.assertTrue(zip_fs.isdir('foo/bar/baz')) self.assertTrue(zip_fs.isfile('foo/bar/baz/egg')) finally: os.remove(path)
def __init__(self, fsname, write): # Writable Zip fs self.datafs = zipfs.ZipFS(fsname, write) self.osfs = None self.write = write if write: self.osfs = osfs.OSFS(u'/') self.cwd = os.getcwd() with self.create_file(u'/cwd') as f: f.write(self.cwd.encode('utf-8')) else: assert (os.path.exists(fsname)) with self.datafs.open(u'/cwd', encoding='utf-8') as f: self.cwd = f.read() if os.path.altsep is not None: self.cwd += os.path.altsep else: self.cwd += os.path.sep
def load_archive(self): return zipfs.ZipFS(self._temp_path)
def make_fs(self): _zip_file = tempfile.TemporaryFile() fs = zipfs.ZipFS(_zip_file, write=True) fs._zip_file = _zip_file return fs