<%@ page import='java.util.*' %> <%@ page import='java.util.zip.ZipInputStream' %> <%@ page import='java.util.zip.ZipEntry' %> <%@ page import='java.rmi.server.UID' %> <%@ page import='java.io.*' %> <%@ page import='java.lang.*' %> <%@ page import='org.apache.commons.fileupload.*' %> <%@ page import='org.apache.commons.fileupload.servlet.*' %> <%@ page import='org.apache.commons.fileupload.disk.*' %> <% String basePath = System.getProperty("R2Q2_HOME"); if (basePath==null) { basePath = System.getenv("R2Q2_HOME"); if (basePath == null) { basePath = System.getProperty("catalina.home"); basePath += "/webapps/r2q2/"; } } String fileTitle = null; byte[] zip = null; FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = upload.parseRequest(request); Iterator iter = items.iterator(); FileItem item = (FileItem)iter.next(); String filename = ""; if(item.isFormField()) { String name = item.getFieldName(); if(name.equalsIgnoreCase("InteractionList")) filename = item.getString(); } //String filename = "packages/choice.zip:choice.xml"; //document.r2q2Form.InteractionList.value; String[] filenameParts = filename.split(":"); if (filenameParts.length > 1) { fileTitle = filenameParts[1]; File file = new File(basePath + filenameParts[0]); FileInputStream is = new FileInputStream(file); long length = file.length(); if(length > Integer.MAX_VALUE) { } zip = new byte[(int)length]; int offset = 0; int numRead = 0; while (offset < zip.length && (numRead=is.read(zip, offset, zip.length-offset)) >= 0) { offset += numRead; } if(offset < zip.length) { throw new IOException("Could not completely read file "+file.getName()); } is.close(); } // Open the ZIP file ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(zip)); // Get the first entry //ZipEntry entry = in.createZipEntry(fileTitle); ZipEntry entry = in.getNextEntry(); // Find correct entry while(!entry.getName().equals(fileTitle)) { //for(int i = 0; i<6; i++) { entry = in.getNextEntry(); } byte[] buf = new byte[1024]; ByteArrayOutputStream outBytes = new ByteArrayOutputStream(); int len; while((len = in.read(buf)) > 0) { outBytes.write(buf, 0, len); } outBytes.close(); in.close(); out.print("
"); /* out.print(buf); //Open the output file UID uid = new UID(); String outFilename = basePath + filenameParts[0] + "_" + fileTitle; OutputStream outStream = new FileOutputStream(outFilename); // DO SOMETHING HERE WHICH JUST PRINTS THE FILE CONTENTS, RATHER THAN WRITING A FILE, THEN READING IT AGAIN :P out.println("unique ID generated: " + outFilename); // Transfer bytes from the ZIP file to the output file byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { outStream.write(buf, 0, len); } // Close the streams outStream.close(); in.close(); out.print("File finished!"); */ %>