From 975a0a5f49e1c8aa3af4487e498bdc0f1c6d9760 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 19 Dec 2014 14:19:26 +0100 Subject: [PATCH] Don't compress common filetypes which are already compressed --- bam/blend/blendfile_pack.py | 20 +++++++++++--------- bam/utils/system.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/bam/blend/blendfile_pack.py b/bam/blend/blendfile_pack.py index f5116ce..b611637 100755 --- a/bam/blend/blendfile_pack.py +++ b/bam/blend/blendfile_pack.py @@ -350,13 +350,18 @@ def pack( _compress_level_orig = zlib.Z_DEFAULT_COMPRESSION zlib.Z_DEFAULT_COMPRESSION = compress_level _compress_mode = zipfile.ZIP_DEFLATED (compress_level == 0) if zipfile.ZIP_STORED else zipfile.ZIP_DEFLATED + if _compress_mode == zipfile.ZIP_STORED: + is_compressed_filetype = lambda fn: False + else: + from bam.utils.system import is_compressed_filetype with zipfile.ZipFile(blendfile_dst.decode('utf-8'), 'w', _compress_mode) as zip_handle: for fn in path_temp_files: yield report(" %s: %r -> \n" % (colorize("copying", color='blue'), fn)) zip_handle.write( fn.decode('utf-8'), - arcname=os.path.relpath(fn[:-1], base_dir_dst_temp).decode('utf-8')) + arcname=os.path.relpath(fn[:-1], base_dir_dst_temp).decode('utf-8'), + ) os.remove(fn) shutil.rmtree(base_dir_dst_temp) @@ -368,14 +373,11 @@ def pack( yield report(" %s: %r\n" % (colorize("source missing", color='red'), src)) else: yield report(" %s: %r -> \n" % (colorize("copying", color='blue'), src)) - zip_handle.write(src.decode('utf-8'), - arcname=os.path.relpath(dst, base_dir_dst).decode('utf-8')) - - """ - _dbg(b"") - _dbg(b"REAL_FILE: " + dst) - _dbg(b"RELATIVE_FILE: " + os.path.relpath(dst, base_dir_dst)) - """ + zip_handle.write( + src.decode('utf-8'), + arcname=os.path.relpath(dst, base_dir_dst).decode('utf-8'), + compress_type=zipfile.ZIP_STORED if is_compressed_filetype(dst) else _compress_mode, + ) zlib.Z_DEFAULT_COMPRESSION = _compress_level_orig del _compress_level_orig, _compress_mode diff --git a/bam/utils/system.py b/bam/utils/system.py index a0fdfde..4bfc26a 100644 --- a/bam/utils/system.py +++ b/bam/utils/system.py @@ -91,3 +91,36 @@ def write_json_to_file(path, data): # optional (pretty) sort_keys=True, indent=4, separators=(',', ': '), ) + + +def is_compressed_filetype(filepath): + """ + Use to check if we should compress files in a zip. + """ + # for now, only include files which Blender is likely to reference + import os + assert(isinstance(filepath, bytes)) + return os.path.splitext(filepath)[1].lower() in { + # images + b'.exr', + b'.jpg', b'.jpeg', + b'.png', + + # audio + b'.aif', b'.aiff', + b'.mp3', + b'.ogg', b'.ogv', + b'.wav', + + # video + b'.avi', + b'.mkv', + b'.mov', + b'.mpg', b'.mpeg', + + # archives + # '.bz2', '.tbz', + # '.gz', '.tgz', + # '.zip', + } +