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