~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/export/zip_exporter.py

  • Committer: Aaron Bentley
  • Date: 2006-11-17 04:06:03 UTC
  • mfrom: (2139 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2162.
  • Revision ID: aaron.bentley@utoronto.ca-20061117040603-pgebxndswvwk26tt
Merge from 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
 
            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":
53
70
                zinfo = zipfile.ZipInfo(
54
 
                            filename=str(os.path.join(root, dp)),
 
71
                            filename=filename,
55
72
                            date_time=now)
56
73
                zinfo.compress_type = compression
 
74
                zinfo.external_attr = _FILE_ATTR
57
75
                zipf.writestr(zinfo, tree.get_file_text(file_id))
58
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.
59
80
                zinfo = zipfile.ZipInfo(
60
 
                            filename=str(os.path.join(root, dp)+os.sep),
 
81
                            filename=filename + '/',
61
82
                            date_time=now)
62
83
                zinfo.compress_type = compression
 
84
                zinfo.external_attr = _DIR_ATTR
63
85
                zipf.writestr(zinfo,'')
64
86
            elif ie.kind == "symlink":
65
87
                zinfo = zipfile.ZipInfo(
66
 
                            filename=str(os.path.join(root, dp+".lnk")),
 
88
                            filename=(filename + '.lnk'),
67
89
                            date_time=now)
68
90
                zinfo.compress_type = compression
 
91
                zinfo.external_attr = _FILE_ATTR
69
92
                zipf.writestr(zinfo, ie.symlink_target)
70
93
 
71
94
        zipf.close()