1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import os 7import zipfile 8 9 10def WriteToZipFile(zip_file, path, arc_path): 11 """Recursively write |path| to |zip_file| as |arc_path|. 12 13 zip_file: An open instance of zipfile.ZipFile. 14 path: An absolute path to the file or directory to be zipped. 15 arc_path: A relative path within the zip file to which the file or directory 16 located at |path| should be written. 17 """ 18 if os.path.isdir(path): 19 for dir_path, _, file_names in os.walk(path): 20 dir_arc_path = os.path.join(arc_path, os.path.relpath(dir_path, path)) 21 logging.debug('dir: %s -> %s', dir_path, dir_arc_path) 22 zip_file.write(dir_path, dir_arc_path, zipfile.ZIP_STORED) 23 for f in file_names: 24 file_path = os.path.join(dir_path, f) 25 file_arc_path = os.path.join(dir_arc_path, f) 26 logging.debug('file: %s -> %s', file_path, file_arc_path) 27 zip_file.write(file_path, file_arc_path, zipfile.ZIP_DEFLATED) 28 else: 29 logging.debug('file: %s -> %s', path, arc_path) 30 zip_file.write(path, arc_path, zipfile.ZIP_DEFLATED) 31 32