def retrieve_archive(self, package, path=None, command=None): archive = package.archive url = package.url self.logger.info("-Downloading {}...".format(url)) try: r = requests.get(url, allow_redirects=False, stream=True) if r.status_code == 302: newurl = altlink(r.url) # newurl = r.headers['Location'] self.logger.info('Redirected to new URL: {}'.format(newurl)) r = requests.get(newurl, stream=True) except requests.exceptions.ConnectionError as e: self.logger.warning("Caught download error: %s" % e.args) return False else: with open(archive, 'wb') as fd: for chunk in r.iter_content(chunk_size=8196): fd.write(chunk) self.logger.info("-Extracting {}...".format(archive)) if sys.version_info > (3, 5): if not py7zr.is_7zfile(archive): raise BadPackageFile if command is None: py7zr.SevenZipFile(archive).extractall(path=path) else: if path is not None: run([command, 'x', '-aoa', '-bd', '-y', '-o{}'.format(path), archive]) else: run([command, 'x', '-aoa', '-bd', '-y', archive]) os.unlink(archive) return True
def test_py7zr_is_not_7zfile(): tmpdir = tempfile.mkdtemp() target = os.path.join(tmpdir, 'test_not.7z') with open(target, 'wb') as f: f.write(b'12345dahodjg98adfjfak;') assert not py7zr.is_7zfile(target) shutil.rmtree(tmpdir)
def run_test(self, args): target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return 1 with open(target, 'rb') as f: try: a = py7zr.SevenZipFile(f) file = sys.stdout file.write("Testing archive: {}\n".format(a.filename)) self.print_archiveinfo(archive=a, file=file) file.write('\n') if a.testzip() is None: file.write('Everything is Ok\n') return 0 else: file.write('Bad 7zip file\n') return 1 except py7zr.exceptions.Bad7zFile: print('Header is corrupted. Cannot read as 7z file.') return 1 except py7zr.exceptions.PasswordRequired: print( 'The archive is encrypted but password is not given. FAILED.' ) return 1
def download_archive_and_add_subtitle_files(self, link, language, video, fps, num_cds): logger.info('Downloading subtitle %r', link) cache_key = sha1(link.encode("utf-8")).digest() request = region.get(cache_key) if request is NO_VALUE: request = self.session.get( link, headers={'Referer': 'https://subsunacs.net/search.php'}) request.raise_for_status() region.set(cache_key, request) else: logger.info('Cache file: %s', codecs.encode(cache_key, 'hex_codec').decode('utf-8')) try: archive_stream = io.BytesIO(request.content) if is_rarfile(archive_stream): return self.process_archive_subtitle_files( RarFile(archive_stream), language, video, link, fps, num_cds) elif is_zipfile(archive_stream): return self.process_archive_subtitle_files( ZipFile(archive_stream), language, video, link, fps, num_cds) elif archive_stream.seek(0) == 0 and is_7zfile(archive_stream): return self.process_archive_subtitle_files( SevenZipFile(archive_stream), language, video, link, fps, num_cds) except: pass logger.error('Ignore unsupported archive %r', request.headers) region.delete(cache_key) return []
def run_extract(self, args: argparse.Namespace) -> int: target = args.arcfile verbose = args.verbose if not py7zr.is_7zfile(target): print('not a 7z file') return 1 if not args.password: password = None # type: Optional[str] else: try: password = getpass.getpass() except getpass.GetPassWarning: sys.stderr.write('Warning: your password may be shown.\n') return 1 try: a = py7zr.SevenZipFile(target, 'r', password=password) except py7zr.exceptions.Bad7zFile: print('Header is corrupted. Cannot read as 7z file.') return 1 except py7zr.exceptions.PasswordRequired: print( 'The archive is encrypted, but password is not given. ABORT.') return 1 except lzma.LZMAError or _lzma.LZMAError: if password is None: print('The archive is corrupted. ABORT.') else: print('The archive is corrupted, or password is wrong. ABORT.') return 1 cb = None # Optional[ExtractCallback] if verbose: archive_info = a.archiveinfo() cb = CliExtractCallback(total_bytes=archive_info.uncompressed, ofd=sys.stderr) try: if args.odir: a.extractall(path=args.odir, callback=cb) else: a.extractall(callback=cb) except py7zr.exceptions.UnsupportedCompressionMethodError: print("Unsupported compression method is used in archive. ABORT.") return 1 except py7zr.exceptions.DecompressionError: print("Error has been occurred during decompression. ABORT.") return 1 except py7zr.exceptions.PasswordRequired: print( 'The archive is encrypted, but password is not given. ABORT.') return 1 except lzma.LZMAError or _lzma.LZMAError: if password is None: print('The archive is corrupted. ABORT.') else: print('The archive is corrupted, or password is wrong. ABORT.') return 1 else: return 0
def un_7z(folderPath, filePath, _7z_password): try: if py7zr.is_7zfile(filePath): with py7zr.SevenZipFile(filePath, password=_7z_password, mode='r') as sevenZ_f: sevenZ_f.extractall(folderPath) return 0 except: return 1
def run_list(self, args): target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return (1) with open(target, 'rb') as f: a = py7zr.SevenZipFile(f) a.list() return (0)
def run_extract(self, args: argparse.Namespace) -> int: target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return (1) a = py7zr.SevenZipFile(target, 'r') if args.odir: a.extractall(path=args.odir) else: a.extractall() return (0)
def run_test(self, args): target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return (1) with open(target, 'rb') as f: a = py7zr.SevenZipFile(f) res = a.test() if res: return (0) else: return (1)
def un_py7zr(filename, extract_dir): list = [] try: is7z = py7zr.is_7zfile(filename) if is7z: ret = py7zr.unpack_7zarchive(filename, extract_dir) arc = py7zr.SevenZipFile(filename) list = arc.getnames() # print(list) else: print('un_py7zr: unknow file type') except Exception as e: print(e) return list
def install(self): downloadUrl: str = None for tag in get(self.app.repoApiUrl).json(): if tag['name'] == self.app.version: for asset in tag['assets']: if '.exe' in asset['name']: downloadUrl = asset['browser_download_url'] break break if not downloadUrl: wx.GenericMessageDialog( parent=self.app.root, message="Can't find the version specified", caption='Error', style=None, ).ShowModal() wx.CallAfter(self.app.root.Destroy) return request = get(downloadUrl, stream=True) # download BEE # working variables zipdata = io.BytesIO() dl = 0 total_length = int(request.headers.get('content-length')) total_length_mb: int(total_length / 1024 / 1024) wx.CallAfter(self.app.root.progBar.SetRange, total_length) # download! for data in request.iter_content(chunk_size=1024): dl += len(data) zipdata.write(data) done = int(100 * dl / total_length) print(f'total: {total_length}, dl: {dl}, done: {done}') wx.CallAfter(self.app.root.progBar.SetValue, done) wx.CallAfter(self.app.root.megaText.SetLabel, f'Done: {done / 1024 / 1024 }/{total_length_mb}MB') wx.CallAfter(self.app.root.speedText.SetLabel, f'Speed: {len(data)}mbs') wx.CallAfter(self.app.root.progBar.Pulse) # read the data as bytes and then create the zipfile object from it if py7zr.is_7zfile(zipdata): wx.GenericMessageDialog( parent=self.app.root, message="The downloaded file wasn't a 7z file.", caption='Error', style=None, ).ShowModal() wx.CallAfter(self.app.root.Destroy) return tempdir = tempfile.mkdtemp(prefix='bridge-inst') py7zr.unpack_7zarchive(zipdata, tempdir) shutil.move(tempdir, self.app.installPath)
def un_py7zr(filename): extract_dir = os.getcwd() + "\\firmware" if os.path.isdir(extract_dir): pass else: os.mkdir(extract_dir) is7z = py7zr.is_7zfile(filename) py7zr.SevenZipFile if is7z: ret = py7zr.unpack_7zarchive(filename, extract_dir) print(ret) else: print('unknow file type') return extract_dir
def run_test(self, args): target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return (1) with open(target, 'rb') as f: a = py7zr.SevenZipFile(f) file = sys.stdout file.write("Testing archive: {}\n".format(a.filename)) self.print_archiveinfo(archive=a, file=file) file.write('\n') if a.test(): file.write('Everything is Ok\n') return (0) else: file.write('Bad 7zip file\n') return (1)
def async_unzip(file_path, zip_out=TEMP_DOWNLOAD_DIRECTORY): if not exists(zip_out): makedirs(zip_out) output_path = join(zip_out, basename(splitext(file_path)[0])) if is_zipfile(file_path): zip_type = ZipFile elif is_rarfile(file_path): zip_type = RarFile elif is_tarfile(file_path): zip_type = TarFile elif is_7zfile(file_path): zip_type = SevenZipFile else: raise TypeError("Unsupported archive.") with zip_type(file_path, "r") as zip_obj: zip_obj.extractall(output_path) return output_path
def extract_file(file_path, directory) -> bool: """ Extract the contents of a file to a directory. Uses 7za or libarchive depending on what's available WARNING: This can and will silently overwrite files in the target directory. """ requirements = requirement_status() if requirements["py7zr"]: import py7zr if py7zr.is_7zfile(file_path): return extract_via_py7zr(file_path, directory) if requirements["libarchive"]: return extract_via_libarchive(file_path, directory) if requirements["7-Zip"]: return extract_via_7za(file_path, directory) update_logger.warning( "No usable file extractors found. Try installing 7-Zip.") return False
def run_extract(self, args: argparse.Namespace) -> int: target = args.arcfile if not py7zr.is_7zfile(target): print('not a 7z file') return (1) if not args.password: password = None # type: Optional[str] else: try: password = getpass.getpass() except getpass.GetPassWarning: sys.stderr.write('Warning: your password may be shown.\n') return (1) a = py7zr.SevenZipFile(target, 'r', password=password) if args.odir: a.extractall(path=args.odir) else: a.extractall() return (0)
def unzip_a_file(filename: str, extract_dir=None, file_password=None): """Extract files in a .7z or .zip archive""" import zipfile import py7zr import os if extract_dir is None: extract_dir = os.getcwd() # when file is .zip file if zipfile.is_zipfile(filename): with open(filename, mode='rb') as f: zipfile.ZipFile(f).extractall(path=extract_dir) # when file is .7z file elif py7zr.is_7zfile(filename): py7zr.SevenZipFile( filename, password=file_password).extractall(path=extract_dir) print('Operation Complete.')
async def unzip_file(event): if event.fwd_from: return if not exists(TEMP_DOWNLOAD_DIRECTORY): os.makedirs(TEMP_DOWNLOAD_DIRECTORY) input_str = event.pattern_match.group(1) output_path = TEMP_DOWNLOAD_DIRECTORY + basename(splitext(input_str)[0]) if exists(input_str): start_time = datetime.now() await event.edit("`Unzipping...`") if is_zipfile(input_str): zip_type = ZipFile elif is_rarfile(input_str): zip_type = RarFile elif is_tarfile(input_str): zip_type = TarFile elif is_7zfile(input_str): zip_type = SevenZipFile else: return await event.edit( "`Unsupported file types!`\n`ZIP, TAR, 7z, and RAR only`" ) try: with zip_type(input_str, "r") as zip_obj: zip_obj.extractall(output_path) except BadRarFile: return await event.edit("**Error:** `Corrupted RAR File`") except BadZipFile: return await event.edit("**Error:** `Corrupted ZIP File`") except Bad7zFile: return await event.edit("**Error:** `Corrupted 7z File`") except BaseException as err: return await event.edit(f"**Error:** `{err}`") end_time = (datetime.now() - start_time).seconds await event.edit( f"Unzipped `{input_str}` into `{output_path}` in `{end_time}` seconds." ) else: await event.edit("`404: Not Found`")
def retrieve_archive(package, path=None, command=None): archive = package.archive url = package.url print("-Downloading {}...".format(url)) try: r = requests.get(url, stream=True, allow_redirects=False) if r.status_code == 302: # tsinghua.edu.cn is problematic and it prohibit service to specific geo location. # we will use another redirected location for that. newurl = r.headers['Location'] mml = Metalink(url) newurl = mml.altlink(blacklist=blacklist) print('Redirected to new URL: {}'.format(newurl)) r = requests.get(newurl, stream=True, allow_redirects=True) except requests.exceptions.ConnectionError as e: print("Caught download error: %s" % e.args) return False else: with open(archive, 'wb') as fd: for chunk in r.iter_content(chunk_size=8196): fd.write(chunk) print("-Extracting {}...".format(archive)) if sys.version_info > (3, 5): if not py7zr.is_7zfile(archive): raise BadPackageFile if command is None: py7zr.SevenZipFile(archive).extractall(path=path) else: if path is not None: run([ command, 'x', '-aoa', '-bd', '-y', '-o{}'.format(path), archive ]) else: run([command, 'x', '-aoa', '-bd', '-y', archive]) os.unlink(archive) return True
def checkIsProtocolFile(protocol, path): """ 验证压缩包文件 Verify compressed package file :param protocol: 压缩协议 | compress protocol :param path: 文件路径 | File Path :return: """ if os.path.exists(path): if protocol == tarfile and not tarfile.is_tarfile(path): raise TypeError(f"{path} " + ('Not recognized by tar protocol' if user_lang != 'zh' else '无法被tar协议识别')) elif protocol == zipfile and not zipfile.is_zipfile(path): raise TypeError(f"{path} " + ('Not recognized by zip protocol' if user_lang != 'zh' else '无法被zip协议识别')) elif protocol == rarfile and not rarfile.is_rarfile(path): raise TypeError(f"{path} " + ('Not recognized by rar protocol' if user_lang != 'zh' else '无法被rar协议识别')) elif protocol == py7zr and not py7zr.is_7zfile(path): raise TypeError(f"{path} " + ('Not recognized by 7z protocol' if user_lang != 'zh' else '无法被7z协议识别')) else: raise FileNotFoundError
def test_py7zr_is_7zfile(): assert py7zr.is_7zfile(os.path.join(testdata_path, "test_1.7z"))
def run_list(self, args): """Print a table of contents to file. """ target = args.arcfile verbose = args.verbose if not py7zr.is_7zfile(target): print('not a 7z file') return 1 with open(target, 'rb') as f: a = py7zr.SevenZipFile(f) file = sys.stdout archive_info = a.archiveinfo() archive_list = a.list() if verbose: file.write("Listing archive: {}\n".format(target)) file.write("--\n") file.write("Path = {}\n".format(archive_info.filename)) file.write("Type = 7z\n") fstat = os.stat(archive_info.filename) file.write("Phisical Size = {}\n".format(fstat.st_size)) file.write("Headers Size = {}\n".format( archive_info.header_size)) file.write("Method = {}\n".format(archive_info.method_names)) if archive_info.solid: file.write("Solid = {}\n".format('+')) else: file.write("Solid = {}\n".format('-')) file.write("Blocks = {}\n".format(archive_info.blocks)) file.write('\n') file.write('total %d files and directories in %sarchive\n' % (len(archive_list), (archive_info.solid and 'solid ') or '')) file.write( ' Date Time Attr Size Compressed Name\n') file.write( '------------------- ----- ------------ ------------ ------------------------\n' ) for f in archive_list: if f.creationtime is not None: lastwritedate = f.creationtime.astimezone(Local).strftime( "%Y-%m-%d") lastwritetime = f.creationtime.astimezone(Local).strftime( "%H:%M:%S") else: lastwritedate = ' ' lastwritetime = ' ' if f.is_directory: attrib = 'D...' else: attrib = '....' if f.archivable: attrib += 'A' else: attrib += '.' if f.is_directory: extra = ' 0 ' elif f.compressed is None: extra = ' ' else: extra = '%12d ' % (f.compressed) file.write('%s %s %s %12d %s %s\n' % (lastwritedate, lastwritetime, attrib, f.uncompressed, extra, f.filename)) file.write( '------------------- ----- ------------ ------------ ------------------------\n' ) return 0
def _run_list(self, target, verbose): if not py7zr.is_7zfile(target): print("not a 7z file") return 1 with py7zr.SevenZipFile(target, "r") as a: file = sys.stdout archive_info = a.archiveinfo() archive_list = a.list() if verbose: if isinstance(target, io.IOBase): file.write("Listing archive: {}\n".format(target.name)) else: file.write("Listing archive: {}\n".format(str(target))) file.write("--\n") file.write("Path = {}\n".format(archive_info.filename)) file.write("Type = 7z\n") fstat = archive_info.stat file.write("Phisical Size = {}\n".format(fstat.st_size)) file.write("Headers Size = {}\n".format( archive_info.header_size)) file.write("Method = {}\n".format(", ".join( archive_info.method_names))) if archive_info.solid: file.write("Solid = {}\n".format("+")) else: file.write("Solid = {}\n".format("-")) file.write("Blocks = {}\n".format(archive_info.blocks)) file.write("\n") file.write("total %d files and directories in %sarchive\n" % (len(archive_list), (archive_info.solid and "solid ") or "")) file.write( " Date Time Attr Size Compressed Name\n") file.write( "------------------- ----- ------------ ------------ ------------------------\n" ) for f in archive_list: if f.creationtime is not None: lastwritedate = f.creationtime.astimezone(Local).strftime( "%Y-%m-%d") lastwritetime = f.creationtime.astimezone(Local).strftime( "%H:%M:%S") else: lastwritedate = " " lastwritetime = " " if f.is_directory: attrib = "D..." else: attrib = "...." if f.archivable: attrib += "A" else: attrib += "." if f.is_directory: extra = " 0 " elif f.compressed is None: extra = " " else: extra = "%12d " % (f.compressed) file.write("%s %s %s %12d %s %s\n" % ( lastwritedate, lastwritetime, attrib, f.uncompressed, extra, f.filename, )) file.write( "------------------- ----- ------------ ------------ ------------------------\n" ) return 0
def test_py7zr_is_not_7zfile(tmp_path): target = tmp_path.joinpath("test_not.7z") with target.open("wb") as f: f.write(b"12345dahodjg98adfjfak;") with target.open("rb") as f: assert not py7zr.is_7zfile(f)
def test_py7zr_is_7zfile_path(): assert py7zr.is_7zfile(pathlib.Path(testdata_path).joinpath("test_1.7z"))
def test_py7zr_is_7zfile_fileish(): assert py7zr.is_7zfile(open(os.path.join(testdata_path, "test_1.7z"), "rb"))
def test_py7zr_is_7zfile_fileish(): assert py7zr.is_7zfile(open(os.path.join(testdata_path, 'test_1.7z'), 'rb'))
def checkFileCorrupted(self,path7Z): return is_7zfile(path7Z)
def test_py7zr_is_not_7zfile(tmp_path): target = tmp_path.joinpath('test_not.7z') with target.open('wb') as f: f.write(b'12345dahodjg98adfjfak;') with target.open('rb') as f: assert not py7zr.is_7zfile(f)