42
42
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT | DIR_PERMISSIONS
45
def zip_exporter_generator(tree, dest, root, subdir=None,
46
force_mtime=None, fileobj=None):
45
def zip_archive_generator(tree, dest, root, subdir=None,
47
47
""" Export this tree to a new zip file.
49
49
`dest` will be created holding the contents of this tree; if it
50
50
already exists, it will be overwritten".
53
52
compression = zipfile.ZIP_DEFLATED
54
if fileobj is not None:
58
zipf = zipfile.ZipFile(dest, "w", compression)
60
for dp, tp, ie in _export_iter_entries(tree, subdir):
62
mutter(" export {%s} kind %s to %s", file_id, ie.kind, dest)
64
# zipfile.ZipFile switches all paths to forward
65
# slashes anyway, so just stick with that.
66
if force_mtime is not None:
69
mtime = tree.get_file_mtime(tp, ie.file_id)
70
date_time = time.localtime(mtime)[:6]
71
filename = osutils.pathjoin(root, dp).encode('utf8')
73
zinfo = zipfile.ZipInfo(
76
zinfo.compress_type = compression
77
zinfo.external_attr = _FILE_ATTR
78
content = tree.get_file_text(tp, file_id)
79
zipf.writestr(zinfo, content)
80
elif ie.kind == "directory":
81
# Directories must contain a trailing slash, to indicate
82
# to the zip routine that they are really directories and
83
# not just empty files.
84
zinfo = zipfile.ZipInfo(
85
filename=filename + '/',
87
zinfo.compress_type = compression
88
zinfo.external_attr = _DIR_ATTR
89
zipf.writestr(zinfo, '')
90
elif ie.kind == "symlink":
91
zinfo = zipfile.ZipInfo(
92
filename=(filename + '.lnk'),
94
zinfo.compress_type = compression
95
zinfo.external_attr = _FILE_ATTR
96
zipf.writestr(zinfo, tree.get_symlink_target(tp, file_id))
101
except UnicodeEncodeError:
104
from breezy.errors import BzrError
105
raise BzrError("Can't export non-ascii filenames to zip")
53
with tempfile.SpooledTemporaryFile() as buf:
54
with closing(zipfile.ZipFile(buf, "w", compression)) as zipf, \
56
for dp, tp, ie in _export_iter_entries(tree, subdir):
57
mutter(" export {%s} kind %s to %s", tp, ie.kind, dest)
59
# zipfile.ZipFile switches all paths to forward
60
# slashes anyway, so just stick with that.
61
if force_mtime is not None:
64
mtime = tree.get_file_mtime(tp)
65
date_time = time.localtime(mtime)[:6]
66
filename = osutils.pathjoin(root, dp)
68
zinfo = zipfile.ZipInfo(
71
zinfo.compress_type = compression
72
zinfo.external_attr = _FILE_ATTR
73
content = tree.get_file_text(tp)
74
zipf.writestr(zinfo, content)
75
elif ie.kind in ("directory", "tree-reference"):
76
# Directories must contain a trailing slash, to indicate
77
# to the zip routine that they are really directories and
78
# not just empty files.
79
zinfo = zipfile.ZipInfo(
80
filename=filename + '/',
82
zinfo.compress_type = compression
83
zinfo.external_attr = _DIR_ATTR
84
zipf.writestr(zinfo, '')
85
elif ie.kind == "symlink":
86
zinfo = zipfile.ZipInfo(
87
filename=(filename + '.lnk'),
89
zinfo.compress_type = compression
90
zinfo.external_attr = _FILE_ATTR
91
zipf.writestr(zinfo, tree.get_symlink_target(tp))
92
# Urgh, headers are written last since they include e.g. file size.
93
# So we have to buffer it all :(
95
for chunk in osutils.file_iterator(buf):