From: Oleg Broytman Date: Sun, 5 Jun 2016 13:44:14 +0000 (+0300) Subject: unzip.py: optimization X-Git-Url: https://git.phdru.name/?a=commitdiff_plain;h=05cfb307e205fea010f32eda0c3944ffc40b6775;p=dotfiles.git unzip.py: optimization Use shutil.copyfileobj() to copy big files by small chunks. --- diff --git a/bin/unzip.py b/bin/unzip.py index 6eb4832..a5d3cf3 100755 --- a/bin/unzip.py +++ b/bin/unzip.py @@ -6,6 +6,7 @@ import sys, os, time from getopt import getopt, GetoptError +from shutil import copyfileobj from zipfile import ZipFile from m_lib.defenc import default_encoding @@ -13,11 +14,11 @@ def usage(): sys.exit('Usage: %s file.zip' % sys.argv[0]) try: - options, arguments = getopt(sys.argv[1:], '') + options, arguments = getopt(sys.argv[1:], '') except GetoptError: usage() -if len(arguments) <> 1: +if len(arguments) != 1: usage() zf = ZipFile(arguments[0], 'r') @@ -40,9 +41,11 @@ for zinfo in zf.infolist(): os.makedirs(tgtdir) if not tgt.endswith('/'): + infile = zf.open(zinfo.filename) fp = open(tgt, 'wb') - fp.write(zf.read(zinfo.filename)) + copyfileobj(infile, fp) fp.close() + infile.close() dt = time.mktime(zinfo.date_time + (0, 0, -1)) os.utime(tgt, (dt, dt)) zf.close()