29
29
from ivle import studpath
30
from ivle.fileservice_lib.exceptions import WillNotOverwrite
32
31
def make_zip(basepath, paths, file):
33
32
"""Zips up a bunch of files on the student file space and writes it as
90
def unzip(path, file, overwrite=False):
89
def unzip(path, file):
91
90
"""Unzips a zip file (or file-like object) into a path.
92
91
Note: All files go directly into the path. To avoid having a "zip bomb"
93
92
situation, the zip file should have a single directory in it with all the
95
94
The path is an absolute path in the current filesystem
96
(if this code is executed inside the jail, then it's inside the jail).
95
(if this code is executed inside the jail, then it's inside the jail,
96
if it's not then it's not).
98
98
zip = zipfile.ZipFile(file, 'r')
99
99
# First test the zip file
100
100
if zip.testzip() is not None:
101
101
raise OSError("ZIP: Bad zip file")
104
for filename in zip.namelist():
105
localpath = os.path.join(path, filename)
106
if os.path.exists(localpath):
107
raise WillNotOverwrite(filename)
109
103
for filename in zip.namelist():
110
104
localpath = os.path.join(path, filename)
111
105
# Create directory for filename
112
106
(file_dir, _) = os.path.split(localpath)
113
107
if not os.path.exists(file_dir):
114
108
os.makedirs(file_dir)
116
110
if filename.endswith(os.sep):
117
111
# Is a directory make the directory
118
112
if not os.path.exists(localpath):