~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""
19
19
 
20
20
import os
 
21
import stat
 
22
import zipfile
 
23
 
 
24
from bzrlib import (
 
25
    osutils,
 
26
    )
21
27
from bzrlib.trace import mutter
22
 
import zipfile
 
28
 
 
29
 
 
30
# Windows expects this bit to be set in the 'external_attr' section
 
31
# Or it won't consider the entry a directory
 
32
ZIP_DIRECTORY_BIT = (1 << 4)
 
33
 
 
34
_FILE_ATTR = stat.S_IFREG
 
35
_DIR_ATTR = stat.S_IFDIR | ZIP_DIRECTORY_BIT
 
36
 
23
37
 
24
38
def zip_exporter(tree, dest, root):
25
39
    """ Export this tree to a new zip file.
49
63
            file_id = ie.file_id
50
64
            mutter("  export {%s} kind %s to %s", file_id, ie.kind, dest)
51
65
 
52
 
            filename = os.path.join(root, dp).encode('utf8')
53
 
            if ie.kind == "file": 
 
66
            # zipfile.ZipFile switches all paths to forward
 
67
            # slashes anyway, so just stick with that.
 
68
            filename = osutils.pathjoin(root, dp).encode('utf8')
 
69
            if ie.kind == "file":
54
70
                zinfo = zipfile.ZipInfo(
55
71
                            filename=filename,
56
72
                            date_time=now)
57
73
                zinfo.compress_type = compression
 
74
                zinfo.external_attr = _FILE_ATTR
58
75
                zipf.writestr(zinfo, tree.get_file_text(file_id))
59
76
            elif ie.kind == "directory":
 
77
                # Directories must contain a trailing slash, to indicate
 
78
                # to the zip routine that they are really directories and
 
79
                # not just empty files.
60
80
                zinfo = zipfile.ZipInfo(
61
 
                            filename=filename,
 
81
                            filename=filename + '/',
62
82
                            date_time=now)
63
83
                zinfo.compress_type = compression
 
84
                zinfo.external_attr = _DIR_ATTR
64
85
                zipf.writestr(zinfo,'')
65
86
            elif ie.kind == "symlink":
66
87
                zinfo = zipfile.ZipInfo(
67
88
                            filename=(filename + '.lnk'),
68
89
                            date_time=now)
69
90
                zinfo.compress_type = compression
 
91
                zinfo.external_attr = _FILE_ATTR
70
92
                zipf.writestr(zinfo, ie.symlink_target)
71
93
 
72
94
        zipf.close()