def _ArchiveOutputDir(self):
"""Archive all files in the output dir, and return as compressed bytes."""
with io.BytesIO() as archive:
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as contents:
num_files = 0
for absdir, _, files in os.walk(self._output_dir):
reldir = os.path.relpath(absdir, self._output_dir)
for filename in files:
src_path = os.path.join(absdir, filename)
# We use normpath to turn './file.txt' into just 'file.txt'.
dst_path = os.path.normpath(os.path.join(reldir, filename))
contents.write(src_path, dst_path)
num_files += 1
if num_files:
logging.info('%d files in the output dir were archived.', num_files)
else:
logging.warning('No files in the output dir. Archive is empty.')
return archive.getvalue()
评论列表
文章目录