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

« back to all changes in this revision

Viewing changes to www/common/zip.py

  • Committer: drtomc
  • Date: 2008-02-03 22:43:33 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:392
Fix another glitch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
from common import studpath
30
30
 
31
 
def make_zip(basepath, paths, file, req):
 
31
def make_zip(basepath, paths, file):
32
32
    """Zips up a bunch of files on the student file space and writes it as
33
33
    a zip file.
34
34
 
84
84
            zip.write(localpath, path)
85
85
 
86
86
    zip.close()
 
87
 
 
88
def unzip(path, file):
 
89
    """Unzips a zip file (or file-like object) into a path.
 
90
    Note: All files go directly into the path. To avoid having a "zip bomb"
 
91
    situation, the zip file should have a single directory in it with all the
 
92
    files.
 
93
    """
 
94
    zip = zipfile.ZipFile(file, 'r')
 
95
    # First test the zip file
 
96
    if zip.testzip() is not None:
 
97
        raise OSError("ZIP: Bad zip file")
 
98
 
 
99
    for filename in zip.namelist():
 
100
        # Work out the name of this file on the local file system, and make
 
101
        # sure it is valid
 
102
        relpath = os.path.join(path, filename)
 
103
        _, localpath = studpath.url_to_local(relpath)
 
104
        if localpath is None:
 
105
            raise OSError("ZIP: Permission denied")
 
106
        # Create directory for filename
 
107
        (file_dir, _) = os.path.split(localpath)
 
108
        if not os.path.exists(file_dir):
 
109
            os.makedirs(file_dir)
 
110
 
 
111
        if filename.endswith(os.sep):
 
112
            # Is a directory make the directory
 
113
            if not os.path.exists(localpath):
 
114
                os.mkdir(localpath)
 
115
        else:
 
116
            filedata = zip.read(filename)
 
117
            f = open(localpath, 'w')
 
118
            f.write(filedata)
 
119
            f.close()