def test_fuzzer_once(obj, volume, basepath): target = numbered_tempdir(basepath, "test_fuzzer", "once").joinpath("target.bin") with MV.open(target, mode="wb", volume=volume) as f: f.write(obj) with MV.open(target, mode="rb") as f: result = f.read() assert result == obj
def test_fuzzer_block(obj, volume, wblock, rblock, basepath): target = numbered_tempdir(basepath, "test_fuzzer", "block").joinpath("target.bin") src = io.BytesIO(obj) result = b"" with MV.open(target, mode="wb", volume=volume) as f: data = src.read(wblock) while len(data) > 0: f.write(data) data = src.read(wblock) with MV.open(target, mode="rb") as f: data = f.read(rblock) while len(data) > 0: result += data data = f.read(rblock) assert result == obj
def test_read_double_close(): target = os.path.join(testdata_path, "archive.7z") mv = MV.open(target, mode="rb") assert not mv.closed mv.close() assert mv.closed mv.close()
def test_readinto(): target = os.path.join(testdata_path, "archive.7z") b = bytearray(200) with MV.open(target, mode="rb") as mv: mv.seek(24900) size = mv.readinto(b) assert size == 100
def test_write_append1(tmp_path): target_volume = tmp_path.joinpath("target.7z.0001") with target_volume.open(mode="wb") as target: with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as src: target.write(src.read(1000)) # target = tmp_path.joinpath("target.7z") with MV.open(target, mode="ab", volume=10240) as volume: assert volume.seekable() is False with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as src: src.seek(1000) volume.write(src.read(24000)) with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) volume.flush() # existed = tmp_path.joinpath("target.7z.0001") assert existed.exists() assert existed.stat().st_size == 10240 created2 = tmp_path.joinpath("target.7z.0002") assert created2.exists() assert created2.stat().st_size == 10240 created6 = tmp_path.joinpath("target.7z.0006") assert created6.exists() assert created6.stat().st_size == 1137
def test_readall(): target = os.path.join(testdata_path, "archive.7z") with MV.open(target, mode="rb") as mv: b = mv.readall() sha = hashlib.sha256(b) assert sha.digest() == binascii.unhexlify( "e0cfeb8010d105d0e2d56d2f94d2b786ead352cef5bca579cb7689d08b3614d1" )
def test_read_context(): target = os.path.join(testdata_path, "archive.7z") with MV.open(target, mode="rb") as mv: assert mv.readable() mv.seek(24900) data = mv.read(200) assert len(data) == 100 data = mv.read(200) assert len(data) == 200
def test_extract_multi_volume(tmp_path): with testdata_path.joinpath('lzma2bcj.7z').open('rb') as src: with tmp_path.joinpath('lzma2bcj.7z.001').open('wb') as tgt: tgt.write(src.read(25000)) with tmp_path.joinpath('lzma2bcj.7z.002').open('wb') as tgt: tgt.write(src.read(27337)) with MVF.open(tmp_path.joinpath('lzma2bcj.7z'), mode='rb') as tgt: with py7zr.SevenZipFile(tgt) as arc: arc.extractall(tmp_path.joinpath('tgt'))
def test_stat(): target = os.path.join(testdata_path, "archive.7z") first_target = os.path.join(testdata_path, "archive.7z.001") with MV.open(target, mode="rb") as mv: st = mv.stat() assert st.st_size == 52337 assert st.st_mtime == os.stat(first_target).st_mtime assert st.st_mtime_ns == os.stat(first_target).st_mtime_ns assert st.st_dev == os.stat(first_target).st_dev assert st.st_ino == 0
def test_extract_multi_volume(tmp_path): with testdata_path.joinpath("lzma2bcj.7z").open("rb") as src: with tmp_path.joinpath("lzma2bcj.7z.001").open("wb") as tgt: tgt.write(src.read(25000)) with tmp_path.joinpath("lzma2bcj.7z.002").open("wb") as tgt: tgt.write(src.read(27337)) with multivolumefile.open(tmp_path.joinpath("lzma2bcj.7z"), mode="rb") as tgt: with py7zr.SevenZipFile(tgt) as arc: arc.extractall(tmp_path.joinpath("tgt"))
def test_compress_to_multi_volume(tmp_path): tmp_path.joinpath('src').mkdir() py7zr.unpack_7zarchive(os.path.join(testdata_path, 'lzma2bcj.7z'), path=tmp_path.joinpath('src')) with MVF.open(tmp_path.joinpath('target.7z'), mode='wb', volume=10240) as tgt: with py7zr.SevenZipFile(tgt, 'w') as arc: arc.writeall(tmp_path.joinpath('src'), 'src') target = tmp_path.joinpath('target.7z.0001') assert target.exists() assert target.stat().st_size == 10240
def test_read_check_sha1(): target = os.path.join(testdata_path, "archive.7z") mv = MV.open(target, mode="rb") sha = hashlib.sha256() data = mv.read(BLOCKSIZE) while len(data) > 0: sha.update(data) data = mv.read(BLOCKSIZE) mv.close() assert sha.digest() == binascii.unhexlify( "e0cfeb8010d105d0e2d56d2f94d2b786ead352cef5bca579cb7689d08b3614d1" )
def test_readinto_boundary(): target = os.path.join(testdata_path, "archive.7z") b = bytearray(200) with MV.open(target, mode="rb") as mv: mv.seek(25000) size = mv.readinto(b) assert size == 200 sha = hashlib.sha256(b) assert ( sha.digest() == b"6\xbc\x92\n\xa53tY\x97\xaa`!\xf3\xa7\xaa\xc3\\V\xac\xc1p\x97\xb0\xf5M\xc0)\x12\x0eD$\x9a" )
def test_write_exist(tmp_path): target = tmp_path.joinpath("target.7z") target_volume = tmp_path.joinpath("target.7z.0001") shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume) # with MV.open(target, mode="wb", volume=10240) as volume: assert volume.writable() with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r: data = r.read(10000) volume.write(data) volume.flush() created = tmp_path.joinpath("target.7z.0001") assert created.stat().st_size == 10000
def test_write_boundary(tmp_path): target = tmp_path.joinpath("target.7z") with MV.open(target, mode="wb", volume=10240) as volume: assert volume.writable() with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r: data = r.read(10000) volume.write(data) data = r.read(250) volume.write(data) volume.flush() created = tmp_path.joinpath("target.7z.0002") assert created.exists() assert created.stat().st_size == 10
def test_compress_to_multi_volume(tmp_path): tmp_path.joinpath("src").mkdir() py7zr.unpack_7zarchive(os.path.join(testdata_path, "lzma2bcj.7z"), path=tmp_path.joinpath("src")) with multivolumefile.open(tmp_path.joinpath("target.7z"), mode="wb", volume=10240) as tgt: with py7zr.SevenZipFile(tgt, "w") as arc: arc.writeall(tmp_path.joinpath("src"), "src") assert tmp_path.joinpath("target.7z.0001").stat().st_size == 10240 assert tmp_path.joinpath("target.7z.0002").stat().st_size == 10240 assert tmp_path.joinpath("target.7z.0003").stat().st_size == 10240 assert 6000 < tmp_path.joinpath("target.7z.0004").stat().st_size < 6100 # p7zip_test(tmp_path.joinpath("target.7z.0001"))
def test_read_seek(): target = os.path.join(testdata_path, "archive.7z") mv = MV.open(target, mode="rb") assert mv.isatty() is False assert mv.seekable() mv.seek(40000) pos = mv.tell() assert pos == 40000 mv.seek(-1000, os.SEEK_CUR) pos = mv.tell() assert pos == 39000 mv.seek(-2337, os.SEEK_END) pos = mv.tell() assert pos == 50000 mv.close() assert mv.readable() is False mv.flush()
def split_directory(directory: PathLike, volume_size: int) -> None: """ Split a file into multivolume zip files. Args: directory: PathLike = Directory of the file volume_size: int = Size of each file """ if Path(f"{getenv('ZIP_SAVE') + Path(directory).stem}/").is_dir(): with multivolumefile.open( # noqa: SIM117 f"{getenv('ZIP_SAVE') + Path(directory).stem}/{Path(directory).stem}", mode="wb", volume=volume_size) as target_archive: with open(f"{Path(directory)}", "rb") as f: for entry in read_in_chunks(10000000, f): target_archive.write(entry) else: Path(f"{getenv('ZIP_SAVE') + Path(directory).stem}/").mkdir() split_directory(directory, volume_size)
def test_write_default_volume(tmp_path): target = tmp_path.joinpath("target.7z").as_posix() with MV.open(target, mode="wb") as volume: with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) assert volume.seekable() volume.seek(0) volume.seek(51000) volume.flush() created = tmp_path.joinpath("target.7z.0001") assert created.exists() assert created.stat().st_size == 52337
def extract(): # extract tar files for (dir_path1, dir_names, files1) in os.walk(tar_path): file_list = set([]) target_path1 = dir_path1.replace('tar', 'raw') try: os.mkdir(target_path1) except Exception as e1: print(e1) # get list of file for file_name1 in files1: print(file_name1) file_list.add(file_name1[:-4]) print(file_list) # extraction for file in file_list: print(dir_path1 + '/' + file) with multivolumefile.open(dir_path1 + '/' + file, mode='rb') as target_archive: with SevenZipFile(target_archive, 'r') as archive: archive.extractall(target_path1)
def test_exclusive_write(tmp_path): target = tmp_path.joinpath("target.7z") with MV.open(target, mode="xb", volume=10240) as volume: assert volume.writable() with open(os.path.join(testdata_path, "archive.7z.001"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) assert volume.seekable() volume.seek(0) volume.seek(51000) volume.flush() created = tmp_path.joinpath("target.7z.0001") assert created.exists() assert created.stat().st_size == 10240
def test_write_append2(tmp_path): target = tmp_path.joinpath("target.7z") target_volume = tmp_path.joinpath("target.7z.0001") shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume) # with MV.open(target, mode="ab", volume=10240) as volume: with open(os.path.join(testdata_path, "archive.7z.002"), "rb") as r: data = r.read(BLOCKSIZE) while len(data) > 0: volume.write(data) data = r.read(BLOCKSIZE) volume.flush() # existed = tmp_path.joinpath("target.7z.0001") assert existed.exists() assert existed.stat().st_size == 25000 created2 = tmp_path.joinpath("target.7z.0002") assert created2.exists() assert created2.stat().st_size == 10240 created4 = tmp_path.joinpath("target.7z.0004") assert created4.exists() assert created4.stat().st_size == 6857 # 52337 - 25000 - 10240 * 2
def test_exclusive_write_exist(tmp_path): target = tmp_path.joinpath("target.7z") target_volume = tmp_path.joinpath("target.7z.0001") shutil.copyfile(os.path.join(testdata_path, "archive.7z.001"), target_volume) with pytest.raises(FileExistsError): MV.open(target, "x")
def test_read_fileno(): target = os.path.join(testdata_path, "archive.7z") mv = MV.open(target, mode="rb") with pytest.raises(RuntimeError): mv.fileno() mv.close()
def test_read_boundary(): target = os.path.join(testdata_path, "archive.7z") with MV.open(target, mode="rb") as mv: mv.seek(24999) b = mv.read(200) assert len(b) == 1
def test_readlines(): target = os.path.join(testdata_path, "archive.7z") with pytest.raises(NotImplementedError): with MV.open(target, mode="rb") as mv: mv.readlines()