~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/zip.py

  • Committer: William Grant
  • Date: 2009-04-28 04:50:39 UTC
  • Revision ID: grantw@unimelb.edu.au-20090428045039-ibb7gwtjrhe9osq3
Populate req.config in a cleaner manner.

Show diffs side-by-side

added added

removed removed

Lines of Context:
86
86
 
87
87
    zip.close()
88
88
 
89
 
def unzip(path, file):
 
89
def unzip(path, file, overwrite=False):
90
90
    """Unzips a zip file (or file-like object) into a path.
91
91
    Note: All files go directly into the path. To avoid having a "zip bomb"
92
92
    situation, the zip file should have a single directory in it with all the
93
93
    files.
94
94
    The path is an absolute path in the current filesystem
95
 
    (if this code is executed inside the jail, then it's inside the jail,
96
 
    if it's not then it's not).
 
95
    (if this code is executed inside the jail, then it's inside the jail).
97
96
    """
 
97
    # XXX: Really bad, but circular imports need avoiding.
 
98
    from ivle.fileservice_lib.exceptions import WillNotOverwrite
 
99
 
98
100
    zip = zipfile.ZipFile(file, 'r')
99
101
    # First test the zip file
100
102
    if zip.testzip() is not None:
101
103
        raise OSError("ZIP: Bad zip file")
102
104
 
 
105
    if (not overwrite):
 
106
        for filename in zip.namelist():
 
107
            localpath = os.path.join(path, filename)
 
108
            if os.path.exists(localpath):
 
109
                raise WillNotOverwrite(filename)
 
110
 
103
111
    for filename in zip.namelist():
104
112
        localpath = os.path.join(path, filename)
105
113
        # Create directory for filename
106
114
        (file_dir, _) = os.path.split(localpath)
107
115
        if not os.path.exists(file_dir):
108
116
            os.makedirs(file_dir)
109
 
 
 
117
        
110
118
        if filename.endswith(os.sep):
111
119
            # Is a directory make the directory
112
120
            if not os.path.exists(localpath):