public static void extractGM(String pathToGMData) { String directoryPath = DirectorySettings.getCacheDirectory().getAbsolutePath(); try { File data = new File(pathToGMData); ZipFile zipData = new ZipFile(data.getAbsolutePath()); Enumeration entries = zipData.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { (new File(directoryPath + File.separator + entry.getName())).mkdirs(); continue; } IOUtils.copy( zipData.getInputStream(entry), new FileOutputStream(directoryPath + File.separator + entry.getName())); } zipData.close(); FileWriter fstream = new FileWriter( DirectorySettings.getGeneManiaDirectory() + File.separator + DirectorySettings.GENEMANIA_CHECK_FILE); BufferedWriter out = new BufferedWriter(fstream); out.write("This file indicates that the GeneMANIA data has finished downloading."); out.close(); if (!data.delete()) { LOG.error("Couldn't delete GeneMANIA .zip: " + data.getAbsolutePath()); } } catch (IOException ex) { java.util.logging.Logger.getLogger(GenemaniaInfoRetriever.class.getName()) .log(Level.SEVERE, null, ex); } }
@Override protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) { // Check we got a valid Multi-Part Form Upload FormData form = (FormData) req.parseContent(); if (form == null || !form.getIsMultiPart()) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Bad Upload"); } // Look for a file, we're quite flexible! File spreadsheet = null; for (FormData.FormField field : form.getFields()) { if (field.getIsFile()) { String ext = ".xls"; if (field.getFilename() != null && field.getFilename().endsWith(".xlsx")) { ext = ".xlsx"; } try { spreadsheet = TempFileProvider.createTempFile("spreadsheet", ext); FileOutputStream sout = new FileOutputStream(spreadsheet); IOUtils.copy(field.getInputStream(), sout); sout.close(); } catch (IOException e) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Upload Failed"); } break; } } // Process String[] sheetnames = null; if (spreadsheet != null) { try { sheetnames = excerpter.getSheetNames(spreadsheet); } catch (IOException e) { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Spreadsheet Corrupt", e); } } else { throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Spreadsheet Missing"); } // Report Map<String, Object> model = new HashMap<String, Object>(); model.put("file", spreadsheet.getName()); model.put("sheets", sheetnames); return model; }
// will throw IOException if not actually POIFS // can return null byte[] private byte[] handleEmbeddedPOIFS( InputStream is, Metadata metadata, AtomicInteger unknownFilenameCount) throws IOException { NPOIFSFileSystem fs = null; byte[] ret = null; try { fs = new NPOIFSFileSystem(is); DirectoryNode root = fs.getRoot(); if (root == null) { return ret; } if (root.hasEntry("Package")) { Entry ooxml = root.getEntry("Package"); TikaInputStream stream = TikaInputStream.get(new DocumentInputStream((DocumentEntry) ooxml)); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(stream, out); ret = out.toByteArray(); } else { // try poifs POIFSDocumentType type = POIFSDocumentType.detectType(root); if (type == POIFSDocumentType.OLE10_NATIVE) { try { // Try to un-wrap the OLE10Native record: Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(root); ret = ole.getDataBuffer(); } catch (Ole10NativeException ex) { // Not a valid OLE10Native record, skip it } } else if (type == POIFSDocumentType.COMP_OBJ) { DocumentEntry contentsEntry; try { contentsEntry = (DocumentEntry) root.getEntry("CONTENTS"); } catch (FileNotFoundException ioe) { contentsEntry = (DocumentEntry) root.getEntry("Contents"); } DocumentInputStream inp = null; try { inp = new DocumentInputStream(contentsEntry); ret = new byte[contentsEntry.getSize()]; inp.readFully(ret); } finally { if (inp != null) { inp.close(); } } } else { ByteArrayOutputStream out = new ByteArrayOutputStream(); is.reset(); IOUtils.copy(is, out); ret = out.toByteArray(); metadata.set( Metadata.RESOURCE_NAME_KEY, "file_" + unknownFilenameCount.getAndIncrement() + "." + type.getExtension()); metadata.set(Metadata.CONTENT_TYPE, type.getType().toString()); } } } finally { if (fs != null) { fs.close(); } } return ret; }