From c8ff84b21979002a0397bc7955dd7d4183f47576 Mon Sep 17 00:00:00 2001 From: Kelvin Zhang Date: Wed, 15 Feb 2023 16:52:46 -0800 Subject: [PATCH] Use zip2zip to copy zipfiles ZipDelete() works by copying every non-deleted entry to a new zipfile. Current implementation uses python's zipfile module to perform entry copying, which is inefficient, as every entry must be decompressed and then re-compressed. Instead, use zip2zip which avoid re-compression. Improvement: deleting META/dynamic_partitions_info.txt from raven-target_files-9465001.zip improved from 500+ seconds to 13 seconds. Change-Id: I0548255bc29380303314763f6d81e74bf3dbb76e --- tools/releasetools/common.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py index 9919029854..2ae39649b2 100644 --- a/tools/releasetools/common.py +++ b/tools/releasetools/common.py @@ -302,6 +302,8 @@ def RunAndCheckOutput(args, verbose=None, **kwargs): Raises: ExternalError: On non-zero exit from the command. """ + if verbose is None: + verbose = OPTIONS.verbose proc = Run(args, verbose=verbose, **kwargs) output, _ = proc.communicate() if output is None: @@ -2893,13 +2895,12 @@ def ZipDelete(zip_filename, entries, force=False): fd, new_zipfile = tempfile.mkstemp(dir=os.path.dirname(zip_filename)) os.close(fd) + cmd = ["zip2zip", "-i", zip_filename, "-o", new_zipfile] + for entry in entries: + cmd.append("-x") + cmd.append(entry) + RunAndCheckOutput(cmd) - with zipfile.ZipFile(new_zipfile, 'w') as zout: - for item in zin.infolist(): - if item.filename in entries: - continue - buffer = zin.read(item.filename) - zout.writestr(item, buffer) os.replace(new_zipfile, zip_filename)