From 6840e7e094f968ab6ca0bef558121372f0b617f1 Mon Sep 17 00:00:00 2001 From: Hiroshi Miura Date: Thu, 9 Jul 2020 13:19:30 +0900 Subject: [PATCH] CLI: catch exception during decompression Signed-off-by: Hiroshi Miura --- py7zr/cli.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/py7zr/cli.py b/py7zr/cli.py index 96718387..a76f31ab 100644 --- a/py7zr/cli.py +++ b/py7zr/cli.py @@ -260,7 +260,7 @@ def run_extract(self, args: argparse.Namespace) -> int: verbose = args.verbose if not py7zr.is_7zfile(target): print('not a 7z file') - return(1) + return 1 if not args.password: password = None # type: Optional[str] else: @@ -269,16 +269,29 @@ def run_extract(self, args: argparse.Namespace) -> int: except getpass.GetPassWarning: sys.stderr.write('Warning: your password may be shown.\n') return(1) - a = py7zr.SevenZipFile(target, 'r', password=password) + try: + a = py7zr.SevenZipFile(target, 'r', password=password) + except py7zr.exceptions.Bad7zFile: + print('Header is corrupted. Cannot read as 7z file.') + return 1 + cb = None # Optional[ExtractCallback] if verbose: archive_info = a.archiveinfo() cb = CliExtractCallback(total_bytes=archive_info.uncompressed, ofd=sys.stderr) - if args.odir: - a.extractall(path=args.odir, callback=cb) + 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 else: - a.extractall(callback=cb) - return(0) + return 0 def _check_volumesize_valid(self, size: str) -> bool: if self.unit_pattern.match(size):