54
54
compression = zipfile.ZIP_DEFLATED
55
55
with tempfile.SpooledTemporaryFile() as buf:
56
56
with closing(zipfile.ZipFile(buf, "w", compression)) as zipf, \
58
58
for dp, tp, ie in _export_iter_entries(tree, subdir):
59
file_id = getattr(ie, 'file_id', None)
60
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
59
mutter(" export {%s} kind %s to %s", tp, ie.kind, dest)
62
61
# zipfile.ZipFile switches all paths to forward
63
62
# slashes anyway, so just stick with that.
64
63
if force_mtime is not None:
65
64
mtime = force_mtime
67
mtime = tree.get_file_mtime(tp, file_id)
66
mtime = tree.get_file_mtime(tp)
68
67
date_time = time.localtime(mtime)[:6]
69
68
filename = osutils.pathjoin(root, dp)
70
69
if ie.kind == "file":
71
70
zinfo = zipfile.ZipInfo(
74
73
zinfo.compress_type = compression
75
74
zinfo.external_attr = _FILE_ATTR
76
content = tree.get_file_text(tp, file_id)
75
content = tree.get_file_text(tp)
77
76
zipf.writestr(zinfo, content)
78
77
elif ie.kind in ("directory", "tree-reference"):
79
78
# Directories must contain a trailing slash, to indicate
80
79
# to the zip routine that they are really directories and
81
80
# not just empty files.
82
81
zinfo = zipfile.ZipInfo(
83
filename=filename + '/',
82
filename=filename + '/',
85
84
zinfo.compress_type = compression
86
85
zinfo.external_attr = _DIR_ATTR
87
86
zipf.writestr(zinfo, '')
88
87
elif ie.kind == "symlink":
89
88
zinfo = zipfile.ZipInfo(
90
filename=(filename + '.lnk'),
89
filename=(filename + '.lnk'),
92
91
zinfo.compress_type = compression
93
92
zinfo.external_attr = _FILE_ATTR
94
zipf.writestr(zinfo, tree.get_symlink_target(tp, file_id))
93
zipf.writestr(zinfo, tree.get_symlink_target(tp))
95
94
# Urgh, headers are written last since they include e.g. file size.
96
95
# So we have to buffer it all :(