def archive(cls, path):
"""Creates a zip file containing the given directory.
:param path: Path to the archived directory.
:return:
"""
import zipfile
dir_name = os.path.basename(path)
zip_path = os.path.join(tempfile.gettempdir(), '%s.zip' % dir_name)
parent_path = os.path.dirname(path) + '/'
z = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED,
allowZip64=True)
try:
for current_dir_path, dir_names, file_names in os.walk(path):
for dir_name in dir_names:
dir_path = os.path.join(current_dir_path, dir_name)
arch_path = dir_path[len(parent_path):]
z.write(dir_path, arch_path)
for file_name in file_names:
file_path = os.path.join(current_dir_path, file_name)
arch_path = file_path[len(parent_path):]
z.write(file_path, arch_path)
finally:
z.close()
return zip_path
评论列表
文章目录