def test_large(self): test_fs = open_fs('mem://') test_fs.setbytes('test.bin', b'a'*50000) write_zip(test_fs, self._temp_path) self.fs = self.load_archive() with self.fs.openbin('test.bin') as f: self.assertEqual(f.read(), b'a'*50000) with self.fs.openbin('test.bin') as f: self.assertEqual(f.read(50000), b'a'*50000) with self.fs.openbin('test.bin') as f: self.assertEqual(f.read1(), b'a'*50000) with self.fs.openbin('test.bin') as f: self.assertEqual(f.read1(50000), b'a'*50000)
def test_large(self): test_fs = open_fs("mem://") test_fs.writebytes("test.bin", b"a" * 50000) write_zip(test_fs, self._temp_path) self.fs = self.load_archive() with self.fs.openbin("test.bin") as f: self.assertEqual(f.read(), b"a" * 50000) with self.fs.openbin("test.bin") as f: self.assertEqual(f.read(50000), b"a" * 50000) with self.fs.openbin("test.bin") as f: self.assertEqual(f.read1(), b"a" * 50000) with self.fs.openbin("test.bin") as f: self.assertEqual(f.read1(50000), b"a" * 50000)
def compress(self, fs): fh, self._temp_path = tempfile.mkstemp() os.close(fh) write_zip(fs, self._temp_path)
def merge_zip_files_func(): from more_itertools import all_equal from fs import open_fs from fs.compress import write_zip from fs.copy import copy_fs_if_newer from filetype import guess def ask_for_dst_path(): return tui.prompt_input( '? Input the path of the ZIP file to merge into: ') def is_zip_file(path: str): mime = guess(path).mime if mime == 'application/zip': return True else: print('! Not a ZIP file: {path}') return False args = rtd.args dry_run = args.dry_run auto_yes = args.yes src_l = args.src or mylib.ex.ostk.clipboard.list_path() src_l = [s for s in src_l if is_zip_file(s)] if len(src_l) < 2: print(f'! at least 2 zip files') return print('# Merge all below ZIP files:') print('\n'.join(src_l)) dbx_l = [mylib.easy.split_path_dir_base_ext(p) for p in src_l] if all_equal([d for d, b, x in dbx_l]): common_dir = dbx_l[0][0] else: common_dir = '' if all_equal([x for d, b, x in dbx_l]): common_ext = dbx_l[0][-1] else: common_ext = '' if common_dir and common_ext: common_base = os.path.commonprefix([b for d, b, x in dbx_l]).strip() if common_base: tmp_dst = mylib.easy.join_path_dir_base_ext( common_dir, common_base, common_ext) if auto_yes or tui.prompt_confirm( f'? Merge into ZIP file "{tmp_dst}"', default=True): dst = tmp_dst else: dst = ask_for_dst_path() else: dst = ask_for_dst_path() elif common_dir: if auto_yes or tui.prompt_confirm( f'? Put merged ZIP file into this dir "{common_dir}"', default=True): filename = tui.prompt_input( f'? Input the basename of the ZIP file to merge into: ') dst = fstk.make_path(common_dir, filename) else: dst = ask_for_dst_path() else: dst = ask_for_dst_path() if dry_run: print(f'@ Merge into ZIP file "{dst}"') return print(f'* Merge into ZIP file "{dst}"') with open_fs('mem://tmp') as tmp: for s in src_l: with open_fs(f'zip://{s}') as z: copy_fs_if_newer( z, tmp ) # todo: seem check time of ZIP-FS but not files inside write_zip(tmp, dst) for s in src_l: if s == dst: continue send2trash(s) print(f'# Trash <- {s}')