class SevenZipArchive(BaseArchive): def __init__(self, file): self._archive = SevenZipFile(file, mode='r') def list(self, *args, **kwargs): self._archive.printdir(*args, **kwargs) def filenames(self): return self._archive.getnames() def namelist(self): return self._archive.getnames() def close(self): return self._archive.close() def extract(self, file): return self._archive.extract(file) def is_encrypted(self): return self._archive.password_protected def extractall(self, file): return self._archive.extractall(file)
def test_register_archive_format(tmp_path): tmp_path.joinpath('src').mkdir() tmp_path.joinpath('tgt').mkdir() # Prepare test data py7zr.unpack_7zarchive(os.path.join(testdata_path, 'test_1.7z'), path=tmp_path.joinpath('src')) # shutil.register_archive_format('7zip', pack_7zarchive, description='7zip archive') shutil.make_archive(str(tmp_path.joinpath('target')), '7zip', str(tmp_path.joinpath('src'))) # check result archive = SevenZipFile(tmp_path.joinpath('target.7z'), 'r') archive.extractall(path=tmp_path.joinpath('tgt')) archive.close() m = hashlib.sha256() m.update((tmp_path / 'tgt' / 'setup.py').open('rb').read()) assert m.digest() == binascii.unhexlify('b916eed2a4ee4e48c51a2b51d07d450de0be4dbb83d20e67f6fd166ff7921e49') m = hashlib.sha256() m.update((tmp_path / 'tgt' / 'scripts' / 'py7zr').open('rb').read()) assert m.digest() == binascii.unhexlify('b0385e71d6a07eb692f5fb9798e9d33aaf87be7dfff936fd2473eab2a593d4fd')
def test_register_archive_format(tmp_path): tmp_path.joinpath("src").mkdir() tmp_path.joinpath("tgt").mkdir() # Prepare test data py7zr.unpack_7zarchive(os.path.join(testdata_path, "test_1.7z"), path=tmp_path.joinpath("src")) # shutil.register_archive_format("7zip", pack_7zarchive, description="7zip archive") shutil.make_archive(str(tmp_path.joinpath("target")), "7zip", str(tmp_path.joinpath("src"))) # check result archive = SevenZipFile(tmp_path.joinpath("target.7z"), "r") archive.extractall(path=tmp_path.joinpath("tgt")) archive.close() m = hashlib.sha256() m.update((tmp_path / "tgt" / "setup.py").open("rb").read()) assert m.digest() == binascii.unhexlify("b916eed2a4ee4e48c51a2b51d07d450de0be4dbb83d20e67f6fd166ff7921e49") m = hashlib.sha256() m.update((tmp_path / "tgt" / "scripts" / "py7zr").open("rb").read()) assert m.digest() == binascii.unhexlify("b0385e71d6a07eb692f5fb9798e9d33aaf87be7dfff936fd2473eab2a593d4fd")
def _download_dataset(self): if not osp.isdir(self._dataset_root): os.mkdir(self._dataset_root) if self._verbose >= 1: print('Download files for the dataset...') infos = FILES_INFOS[self._subset] # Download archives files for name, info in infos.items(): filename, url, hash_ = info['filename'], info['url'], info['hash'] filepath = osp.join(self._dataset_root, filename) if not osp.isfile(filepath): if self._verbose >= 1: print(f'Download file "{filename}" from url "{url}"...') if osp.exists(filepath): raise RuntimeError( f'Object "{filepath}" already exists but it\'s not a file.' ) download_url(url, self._dataset_root, filename, hash_value=hash_, hash_type='md5') # Extract audio files from archives for name, info in infos.items(): filename = info['filename'] filepath = osp.join(self._dataset_root, filename) extension = filename.split('.')[-1] if extension == '7z': extracted_path = osp.join(self._dataset_root, self._subset) if not osp.isdir(extracted_path): if self._verbose >= 1: print(f'Extract archive file "{filename}"...') archive_file = SevenZipFile(filepath) archive_file.extractall(self._dataset_root) archive_file.close()