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