ArrayList<String> fileNames = new ArrayList<String>();
try {
// Parse the request
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
IVResource userDirectory = user.getResource(path);
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField()) {
String fileName = item.getName();
InputStream is = new BufferedInputStream(item.getInputStream());;
OutputStream os = null;
byte[] buffer = new byte[8192];
try {
if(explodeZip && ("application/zip".equals(item.getContentType()) || fileName.endsWith(".zip"))){
ZipInputStream zis = new ZipInputStream(is);
ZipEntry entry;
int bytesIn;
is = zis;
while ((entry = zis.getNextEntry()) != null) {
String zipFileName = entry.getName();
// theLogger.finest("Uploading zip: " + fileName + " entry: " + zipFileName);
if(!zipFileName.endsWith("/")) {
// Odd that create dir seems to do a recursive create, but file does not
userDirectory.create(new Path(zipFileName).removeLastSegments(1).toString()+"/");
IVResource uploaded = userDirectory.create(zipFileName);
os = uploaded.getOutputStreem();
while ((bytesIn = zis.read(buffer)) != -1) {
os.write(buffer, 0, bytesIn);
}
os.flush();
os.close();
uploaded.flushWorkingCopy();
}
fileNames.add(zipFileName);
}
} else {
//FIXME: common with above code
IVResource uploaded = userDirectory.create(fileName);
os = uploaded.getOutputStreem();
while (true) {
int bytesRead = -1;
bytesRead = is.read(buffer);
if (bytesRead == -1) {
break;
}
os.write(buffer, 0, bytesRead);
}
fileNames.add(fileName);
uploaded.flushWorkingCopy();
}
} finally {
is.close();
if (os != null) {
os.flush();