List of usage examples for java.util.zip ZipFile getInputStream
public InputStream getInputStream(ZipEntry entry) throws IOException
From source file:com.izforge.izpack.compiler.CompilerConfig.java
private IXMLElement readRefPackData(String refFileName, boolean isselfcontained) throws CompilerException { File refXMLFile = new File(refFileName); if (!refXMLFile.isAbsolute()) { refXMLFile = new File(compilerData.getBasedir(), refFileName); }//from www .ja v a 2s .com if (!refXMLFile.canRead()) { throw new CompilerException("Invalid file: " + refXMLFile); } InputStream specin; if (isselfcontained) { if (!refXMLFile.getAbsolutePath().endsWith(".zip")) { throw new CompilerException( "Invalid file: " + refXMLFile + ". Selfcontained files can only be of type zip."); } ZipFile zip; try { zip = new ZipFile(refXMLFile, ZipFile.OPEN_READ); ZipEntry specentry = zip.getEntry("META-INF/izpack.xml"); specin = zip.getInputStream(specentry); } catch (IOException e) { throw new CompilerException("Error reading META-INF/izpack.xml in " + refXMLFile); } } else { try { specin = new FileInputStream(refXMLFile.getAbsolutePath()); } catch (FileNotFoundException e) { throw new CompilerException("FileNotFoundException exception while reading refXMLFile"); } } IXMLParser refXMLParser = new XMLParser(); // We get it IXMLElement refXMLData = refXMLParser.parse(specin, refXMLFile.getAbsolutePath()); // Now checked the loaded XML file for basic syntax // We check it if (!"installation".equalsIgnoreCase(refXMLData.getName())) { assertionHelper.parseError(refXMLData, "this is not an IzPack XML installation file"); } if (!CompilerData.VERSION.equalsIgnoreCase(xmlCompilerHelper.requireAttribute(refXMLData, "version"))) { assertionHelper.parseError(refXMLData, "the file version is different from the compiler version"); } // Read the properties and perform replacement on the rest of the tree substituteProperties(refXMLData); // call addResources to add the referenced XML resources to this installation addResources(refXMLData); try { specin.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return refXMLData; }
From source file:com.pari.pcb.zip.ZIPProcessor.java
private File copyEntry(ZipEntry entry, ZipFile inZip, String hostName, String cmd) throws IOException { InputStream inStream = inZip.getInputStream(entry); return copyEntry(inStream, hostName, cmd); }
From source file:com.flexive.core.storage.GenericDivisionImporter.java
/** * Import flat storages to the hierarchical storage * * @param con an open and valid connection to store imported data * @param zip zip file containing the data * @throws Exception on errors//from w ww . j a va 2 s . c o m */ protected void importFlatStoragesHierarchical(Connection con, ZipFile zip) throws Exception { //mapping: storage->level->columnname->assignment id final Map<String, Map<Integer, Map<String, Long>>> flatAssignmentMapping = new HashMap<String, Map<Integer, Map<String, Long>>>( 5); //mapping: assignment id->position index final Map<Long, Integer> assignmentPositions = new HashMap<Long, Integer>(100); //mapping: flatstorage->column sizes [string,bigint,double,select,text] final Map<String, Integer[]> flatstoragesColumns = new HashMap<String, Integer[]>(5); ZipEntry zeMeta = getZipEntry(zip, FILE_FLATSTORAGE_META); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(zip.getInputStream(zeMeta)); XPath xPath = XPathFactory.newInstance().newXPath(); //calculate column sizes NodeList nodes = (NodeList) xPath.evaluate("/flatstorageMeta/storageMeta", document, XPathConstants.NODESET); Node currNode; for (int i = 0; i < nodes.getLength(); i++) { currNode = nodes.item(i); int cbigInt = Integer.parseInt(currNode.getAttributes().getNamedItem("bigInt").getNodeValue()); int cdouble = Integer.parseInt(currNode.getAttributes().getNamedItem("double").getNodeValue()); int cselect = Integer.parseInt(currNode.getAttributes().getNamedItem("select").getNodeValue()); int cstring = Integer.parseInt(currNode.getAttributes().getNamedItem("string").getNodeValue()); int ctext = Integer.parseInt(currNode.getAttributes().getNamedItem("text").getNodeValue()); String tableName = null; if (currNode.hasChildNodes()) { for (int j = 0; j < currNode.getChildNodes().getLength(); j++) if (currNode.getChildNodes().item(j).getNodeName().equals("name")) { tableName = currNode.getChildNodes().item(j).getTextContent(); } } if (tableName != null) { flatstoragesColumns.put(tableName, new Integer[] { cstring, cbigInt, cdouble, cselect, ctext }); } } //parse mappings nodes = (NodeList) xPath.evaluate("/flatstorageMeta/mapping", document, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { currNode = nodes.item(i); long assignment = Long.valueOf(currNode.getAttributes().getNamedItem("assid").getNodeValue()); int level = Integer.valueOf(currNode.getAttributes().getNamedItem("lvl").getNodeValue()); String storage = null; String columnname = null; final NodeList childNodes = currNode.getChildNodes(); for (int c = 0; c < childNodes.getLength(); c++) { Node child = childNodes.item(c); if ("tblname".equals(child.getNodeName())) storage = child.getTextContent(); else if ("colname".equals(child.getNodeName())) columnname = child.getTextContent(); } if (storage == null || columnname == null) throw new Exception("Invalid flatstorage export: could not read storage or column name!"); if (!flatAssignmentMapping.containsKey(storage)) flatAssignmentMapping.put(storage, new HashMap<Integer, Map<String, Long>>(20)); Map<Integer, Map<String, Long>> levelMap = flatAssignmentMapping.get(storage); if (!levelMap.containsKey(level)) levelMap.put(level, new HashMap<String, Long>(30)); Map<String, Long> columnMap = levelMap.get(level); if (!columnMap.containsKey(columnname)) columnMap.put(columnname, assignment); //calculate position assignmentPositions.put(assignment, getAssignmentPosition(flatstoragesColumns.get(storage), columnname)); } if (flatAssignmentMapping.size() == 0) { LOG.warn("No flatstorage assignments found to process!"); return; } ZipEntry zeData = getZipEntry(zip, FILE_DATA_FLAT); final String xpathStorage = "flatstorages/storage"; final String xpathData = "flatstorages/storage/data"; final PreparedStatement psGetAssInfo = con.prepareStatement( "SELECT DISTINCT a.APROPERTY,a.XALIAS,p.DATATYPE FROM " + DatabaseConst.TBL_STRUCT_ASSIGNMENTS + " a, " + DatabaseConst.TBL_STRUCT_PROPERTIES + " p WHERE a.ID=? AND p.ID=a.APROPERTY"); final Map<Long, Object[]> assignmentPropAlias = new HashMap<Long, Object[]>(assignmentPositions.size()); final String insert1 = "INSERT INTO " + DatabaseConst.TBL_CONTENT_DATA + //1 2 3 4 5 6 =1 =1 =1 =1 7 8 9 "(ID,VER,POS,LANG,TPROP,ASSIGN,XDEPTH,XMULT,XINDEX,PARENTXMULT,ISMAX_VER,ISLIVE_VER,ISMLDEF,"; final String insert2 = "(?,?,?,?,1,?,?,1,1,1,?,?,?,"; final PreparedStatement psString = con .prepareStatement(insert1 + "FTEXT1024,UFTEXT1024,FSELECT,FINT)VALUES" + insert2 + "?,?,0,?)"); final PreparedStatement psText = con .prepareStatement(insert1 + "FCLOB,UFCLOB,FSELECT,FINT)VALUES" + insert2 + "?,?,0,?)"); final PreparedStatement psDouble = con .prepareStatement(insert1 + "FDOUBLE,FSELECT,FINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psNumber = con .prepareStatement(insert1 + "FINT,FSELECT,FBIGINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psLargeNumber = con .prepareStatement(insert1 + "FBIGINT,FSELECT,FINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psFloat = con .prepareStatement(insert1 + "FFLOAT,FSELECT,FINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psBoolean = con .prepareStatement(insert1 + "FBOOL,FSELECT,FINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psReference = con .prepareStatement(insert1 + "FREF,FSELECT,FINT)VALUES" + insert2 + "?,0,?)"); final PreparedStatement psSelectOne = con .prepareStatement(insert1 + "FSELECT,FINT)VALUES" + insert2 + "?,?)"); try { final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final DefaultHandler handler = new DefaultHandler() { private String currentElement = null; private String currentStorage = null; private Map<String, String> data = new HashMap<String, String>(10); private StringBuilder sbData = new StringBuilder(10000); boolean inTag = false; boolean inElement = false; List<String> path = new ArrayList<String>(10); StringBuilder currPath = new StringBuilder(100); int insertCount = 0; /** * {@inheritDoc} */ @Override public void startDocument() throws SAXException { inTag = false; inElement = false; path.clear(); currPath.setLength(0); sbData.setLength(0); data.clear(); currentElement = null; currentStorage = null; insertCount = 0; } /** * {@inheritDoc} */ @Override public void endDocument() throws SAXException { LOG.info("Imported [" + insertCount + "] flatstorage entries into the hierarchical storage"); } /** * {@inheritDoc} */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { pushPath(qName, attributes); if (currPath.toString().equals(xpathData)) { inTag = true; data.clear(); for (int i = 0; i < attributes.getLength(); i++) { String name = attributes.getLocalName(i); if (StringUtils.isEmpty(name)) name = attributes.getQName(i); data.put(name, attributes.getValue(i)); } } else if (currPath.toString().equals(xpathStorage)) { currentStorage = attributes.getValue("name"); LOG.info("Processing storage: " + currentStorage); } else { currentElement = qName; } inElement = true; sbData.setLength(0); } /** * Push a path element from the stack * * @param qName element name to push * @param att attributes */ @SuppressWarnings({ "UnusedDeclaration" }) private void pushPath(String qName, Attributes att) { path.add(qName); buildPath(); } /** * Pop the top path element from the stack */ private void popPath() { path.remove(path.size() - 1); buildPath(); } /** * Rebuild the current path */ private synchronized void buildPath() { currPath.setLength(0); for (String s : path) currPath.append(s).append('/'); if (currPath.length() > 1) currPath.delete(currPath.length() - 1, currPath.length()); // System.out.println("currPath: " + currPath); } /** * {@inheritDoc} */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (currPath.toString().equals(xpathData)) { // LOG.info("Insert [" + xpathData + "]: [" + data + "]"); inTag = false; processData(); /*try { if (insertMode) { if (executeInsertPhase) { processColumnSet(insertColumns, psInsert); counter += psInsert.executeUpdate(); } } else { if (executeUpdatePhase) { if (processColumnSet(updateSetColumns, psUpdate)) { processColumnSet(updateClauseColumns, psUpdate); counter += psUpdate.executeUpdate(); } } } } catch (SQLException e) { throw new SAXException(e); } catch (ParseException e) { throw new SAXException(e); }*/ } else { if (inTag) { data.put(currentElement, sbData.toString()); } currentElement = null; } popPath(); inElement = false; sbData.setLength(0); } void processData() { // System.out.println("processing " + currentStorage + " -> " + data); final String[] cols = { "string", "bigint", "double", "select", "text" }; for (String column : data.keySet()) { if (column.endsWith("_mld")) continue; for (String check : cols) { if (column.startsWith(check)) { if ("select".equals(check) && "0".equals(data.get(column))) continue; //dont insert 0-referencing selects try { insertData(column); } catch (SQLException e) { //noinspection ThrowableInstanceNeverThrown throw new FxDbException(e, "ex.db.sqlError", e.getMessage()) .asRuntimeException(); } } } } } private void insertData(String column) throws SQLException { final int level = Integer.parseInt(data.get("lvl")); long assignment = flatAssignmentMapping.get(currentStorage).get(level) .get(column.toUpperCase()); int pos = FxArrayUtils.getIntElementAt(data.get("positions"), ',', assignmentPositions.get(assignment)); String _valueData = data.get("valuedata"); Integer valueData = _valueData == null ? null : FxArrayUtils.getHexIntElementAt(data.get("valuedata"), ',', assignmentPositions.get(assignment)); Object[] propXP = getPropertyXPathDataType(assignment); long prop = (Long) propXP[0]; String xpath = (String) propXP[1]; FxDataType dataType; try { dataType = FxDataType.getById((Long) propXP[2]); } catch (FxNotFoundException e) { throw e.asRuntimeException(); } long id = Long.parseLong(data.get("id")); int ver = Integer.parseInt(data.get("ver")); long lang = Integer.parseInt(data.get("lang")); boolean isMaxVer = "1".equals(data.get("ismax_ver")); boolean isLiveVer = "1".equals(data.get("islive_ver")); boolean mlDef = "1".equals(data.get(column + "_mld")); PreparedStatement ps; int vdPos; switch (dataType) { case String1024: ps = psString; ps.setString(10, data.get(column)); ps.setString(11, data.get(column).toUpperCase()); vdPos = 12; break; case Text: case HTML: ps = psText; ps.setString(10, data.get(column)); ps.setString(11, data.get(column).toUpperCase()); vdPos = 12; break; case Number: ps = psNumber; ps.setLong(10, Long.valueOf(data.get(column))); vdPos = 11; break; case LargeNumber: ps = psLargeNumber; ps.setLong(10, Long.valueOf(data.get(column))); vdPos = 11; break; case Reference: ps = psReference; ps.setLong(10, Long.valueOf(data.get(column))); vdPos = 11; break; case Float: ps = psFloat; ps.setFloat(10, Float.valueOf(data.get(column))); vdPos = 11; break; case Double: ps = psDouble; ps.setDouble(10, Double.valueOf(data.get(column))); vdPos = 11; break; case Boolean: ps = psBoolean; ps.setBoolean(10, "1".equals(data.get(column))); vdPos = 11; break; case SelectOne: ps = psSelectOne; ps.setLong(10, Long.valueOf(data.get(column))); vdPos = 11; break; default: //noinspection ThrowableInstanceNeverThrown throw new FxInvalidParameterException("assignment", "ex.structure.flatstorage.datatype.unsupported", dataType.name()) .asRuntimeException(); } ps.setLong(1, id); ps.setInt(2, ver); ps.setInt(3, pos); ps.setLong(4, lang); ps.setLong(5, prop); ps.setLong(6, assignment); ps.setBoolean(7, isMaxVer); ps.setBoolean(8, isLiveVer); ps.setBoolean(9, mlDef); if (valueData == null) ps.setNull(vdPos, java.sql.Types.NUMERIC); else ps.setInt(vdPos, valueData); ps.executeUpdate(); insertCount++; } /** * Get property id, xpath and data type for an assignment * * @param assignment assignment id * @return Object[] {propertyId, xpath, datatype} */ private Object[] getPropertyXPathDataType(long assignment) { if (assignmentPropAlias.get(assignment) != null) return assignmentPropAlias.get(assignment); try { psGetAssInfo.setLong(1, assignment); ResultSet rs = psGetAssInfo.executeQuery(); if (rs != null && rs.next()) { Object[] data = new Object[] { rs.getLong(1), rs.getString(2), rs.getLong(3) }; assignmentPropAlias.put(assignment, data); return data; } } catch (SQLException e) { throw new IllegalArgumentException( "Could not load data for assignment " + assignment + ": " + e.getMessage()); } throw new IllegalArgumentException("Could not load data for assignment " + assignment + "!"); } /** * {@inheritDoc} */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (inElement) sbData.append(ch, start, length); } }; parser.parse(zip.getInputStream(zeData), handler); } finally { Database.closeObjects(GenericDivisionImporter.class, psGetAssInfo, psString, psBoolean, psDouble, psFloat, psLargeNumber, psNumber, psReference, psSelectOne, psText); } }
From source file:fr.certu.chouette.gui.command.Command.java
/** * import command : ( -fileFormat utilis si l'extension du fichier n'est pas reprsentative du format) * -c import -o line -format XXX -inputFile YYYY [-fileFormat TTT] -importId ZZZ ... * @param manager/*from w w w.ja v a 2s . c o m*/ * @param parameters * @return */ private int executeImport(INeptuneManager<NeptuneIdentifiedObject> manager, Map<String, List<String>> parameters) { parameters.put("reportforsave", Arrays.asList(new String[] { "true" })); // parameters.put("validate",Arrays.asList(new String[]{"true"})); // force validation if possible GuiReport saveReport = new GuiReport("SAVE", Report.STATE.OK); Report importReport = null; List<Report> reports = new ArrayList<Report>(); // check if import exists and accept unzip before call String format = getSimpleString(parameters, "format"); String inputFile = getSimpleString(parameters, "inputfile"); // String fileFormat = getSimpleString(parameters,"fileformat",""); Long importId = Long.valueOf(getSimpleString(parameters, "importid")); if (!importDao.exists(importId)) { // error import not found logger.error("import not found " + importId); return 1; } GuiImport guiImport = importDao.get(importId); logger.info("Export data for export id " + importId); logger.info(" type : " + guiImport.getType()); logger.info(" options : " + guiImport.getOptions()); Referential referential = referentialDao.get(guiImport.getReferentialId()); logger.info("Referential " + guiImport.getReferentialId()); logger.info(" name : " + referential.getName()); logger.info(" slug : " + referential.getSlug()); logger.info(" projection type : " + referential.getProjectionType()); String projectionType = null; if (referential.getProjectionType() != null && !referential.getProjectionType().isEmpty()) { logger.info(" projection type for export: " + referential.getProjectionType()); projectionType = referential.getProjectionType(); parameters.put("srid", Arrays.asList(new String[] { projectionType })); } // set projection for import (inactive if not set) geographicService.switchProjection(projectionType); int beanCount = 0; boolean zipped = (inputFile.toLowerCase().endsWith(".zip")); try { List<FormatDescription> formats = manager.getImportFormats(null); FormatDescription description = null; for (FormatDescription formatDescription : formats) { if (formatDescription.getName().equalsIgnoreCase(format)) { description = formatDescription; break; } } if (description == null) { throw new IllegalArgumentException("format " + format + " unavailable"); } List<String> suffixes = new ArrayList<String>(); for (ParameterDescription desc : description.getParameterDescriptions()) { if (desc.getName().equalsIgnoreCase("inputfile")) { suffixes = desc.getAllowedExtensions(); break; } } List<ParameterValue> values = populateParameters(description, parameters, "inputfile", "fileformat"); if (zipped && description.isUnzipAllowed()) { SimpleParameterValue inputFileParam = new SimpleParameterValue("inputFile"); values.add(inputFileParam); // unzip files , import and save contents ZipFile zip = null; File temp = null; File tempRep = new File(FileUtils.getTempDirectory(), "massImport" + importId); if (!tempRep.exists()) tempRep.mkdirs(); try { zip = new ZipFile(inputFile); for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { File dir = new File(tempRep, entry.getName()); dir.mkdirs(); continue; } if (!FilenameUtils.isExtension(entry.getName().toLowerCase(), suffixes)) { logger.error("entry " + entry.getName() + " ignored, unknown extension"); continue; } InputStream stream = null; try { stream = zip.getInputStream(entry); } catch (IOException e) { logger.error("entry " + entry.getName() + " cannot read"); continue; } byte[] bytes = new byte[4096]; int len = stream.read(bytes); temp = new File(tempRep, entry.getName()); FileOutputStream fos = new FileOutputStream(temp); while (len > 0) { fos.write(bytes, 0, len); len = stream.read(bytes); } fos.close(); // import if (verbose) System.out.println("import file " + entry.getName()); logger.info("import file " + entry.getName()); inputFileParam.setFilepathValue(temp.getAbsolutePath()); ReportHolder holder = new ReportHolder(); List<NeptuneIdentifiedObject> beans = manager.doImport(null, format, values, holder); if (holder.getReport() != null) { if (importReport == null) { importReport = holder.getReport(); reports.add(importReport); } else { importReport.addAll(holder.getReport().getItems()); } } // save if (beans != null && !beans.isEmpty()) { for (NeptuneIdentifiedObject bean : beans) { if (verbose) { System.out.println("save " + bean.getName() + " (" + bean.getObjectId() + ")"); } logger.info("save " + bean.getName() + " (" + bean.getObjectId() + ")"); // check all stopareas if (bean instanceof Line) { Line line = (Line) bean; checkProjection(line); } } try { manager.saveAll(null, beans, true, true); for (NeptuneIdentifiedObject bean : beans) { GuiReportItem item = new GuiReportItem("SAVE_OK", Report.STATE.OK, bean.getName()); importReport.addItem(item); beanCount++; } } catch (Exception e) { logger.error("fail to save data :" + e.getMessage(), e); for (NeptuneIdentifiedObject bean : beans) { GuiReportItem item = new GuiReportItem("SAVE_ERROR", Report.STATE.ERROR, bean.getName(), filter_chars(e.getMessage())); importReport.addItem(item); } } } temp.delete(); } try { zip.close(); } catch (IOException e) { logger.info("cannot close zip file"); } } catch (IOException e) { //reports.add(saveReport); System.out.println("import failed " + e.getMessage()); logger.error("import failed " + e.getMessage(), e); saveImportReports(importId, format, reports); return 1; } finally { try { FileUtils.deleteDirectory(tempRep); } catch (IOException e) { logger.warn("temporary directory " + tempRep.getAbsolutePath() + " could not be deleted"); } } } else { SimpleParameterValue inputFileParam = new SimpleParameterValue("inputFile"); inputFileParam.setFilepathValue(inputFile); values.add(inputFileParam); // if (!fileFormat.isEmpty()) // { // SimpleParameterValue fileFormatParam = new SimpleParameterValue("fileFormat"); // fileFormatParam.setStringValue(fileFormat); // values.add(fileFormatParam); // } // surround with try catch ReportHolder holder = new ReportHolder(); List<NeptuneIdentifiedObject> beans = manager.doImport(null, format, values, holder); if (holder.getReport() != null) { importReport = holder.getReport(); reports.add(holder.getReport()); } logger.info("imported Lines " + beans.size()); for (NeptuneIdentifiedObject bean : beans) { if (bean instanceof Line) { Line line = (Line) bean; checkProjection(line); } List<NeptuneIdentifiedObject> oneBean = new ArrayList<NeptuneIdentifiedObject>(); oneBean.add(bean); try { logger.info("save Line " + bean.getName()); manager.saveAll(null, oneBean, true, true); GuiReportItem item = new GuiReportItem("SAVE_OK", Report.STATE.OK, bean.getName()); saveReport.addItem(item); beanCount++; } catch (Exception e) { logger.error("save failed " + e.getMessage(), e); GuiReportItem item = new GuiReportItem("SAVE_ERROR", Report.STATE.ERROR, bean.getName(), e.getMessage()); saveReport.addItem(item); } } } } catch (Exception e) { // fill report with error if (saveReport.getItems() != null && !saveReport.getItems().isEmpty()) reports.add(saveReport); String msg = e.getMessage(); if (msg == null) msg = e.getClass().getName(); System.out.println("import failed " + msg); logger.error("import failed " + msg, e); GuiReport errorReport = new GuiReport("IMPORT_ERROR", Report.STATE.ERROR); GuiReportItem item = new GuiReportItem("EXCEPTION", Report.STATE.ERROR, msg); errorReport.addItem(item); reports.add(errorReport); saveImportReports(importId, format, reports); return 1; } if (saveReport.getItems() != null && !saveReport.getItems().isEmpty()) reports.add(saveReport); saveImportReports(importId, format, reports); return (beanCount == 0 ? 1 : 0); }
From source file:com.app.server.WarDeployer.java
/** * This method deploys the war in exploded form and configures it. * /*from www .j av a 2 s.co m*/ * @param file * @param customClassLoader * @param warDirectoryPath */ public void extractWar(File file, ClassLoader classLoader) { StringBuffer classPath = new StringBuffer(); int numBytes; WebClassLoader customClassLoader = null; CopyOnWriteArrayList<URL> jarUrls = new CopyOnWriteArrayList<URL>(); try { ConcurrentHashMap jspMap = new ConcurrentHashMap(); ZipFile zip = new ZipFile(file); ZipEntry ze = null; // String fileName=file.getName(); String directoryName = file.getName(); directoryName = directoryName.substring(0, directoryName.indexOf('.')); try { /*ClassLoader classLoader = Thread.currentThread() .getContextClassLoader();*/ // log.info("file://"+warDirectoryPath+"/WEB-INF/classes/"); jarUrls.add(new URL("file:" + scanDirectory + "/" + directoryName + "/WEB-INF/classes/")); // new WebServer().addURL(new // URL("file://"+warDirectoryPath+"/"),customClassLoader); } catch (Exception e) { log.error("syntax of the URL is incorrect", e); //e1.printStackTrace(); } String fileDirectory; Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ze = entries.nextElement(); // //log.info("Unzipping " + ze.getName()); String filePath = scanDirectory + "/" + directoryName + "/" + ze.getName(); if (!ze.isDirectory()) { fileDirectory = filePath.substring(0, filePath.lastIndexOf('/')); } else { fileDirectory = filePath; } // //log.info(fileDirectory); createDirectory(fileDirectory); if (!ze.isDirectory()) { FileOutputStream fout = new FileOutputStream(filePath); byte[] inputbyt = new byte[8192]; InputStream istream = zip.getInputStream(ze); while ((numBytes = istream.read(inputbyt, 0, inputbyt.length)) >= 0) { fout.write(inputbyt, 0, numBytes); } fout.close(); istream.close(); if (ze.getName().endsWith(".jsp")) { jspMap.put(ze.getName(), filePath); } else if (ze.getName().endsWith(".jar")) { jarUrls.add(new URL("file:///" + scanDirectory + "/" + directoryName + "/" + ze.getName())); classPath.append(filePath); classPath.append(";"); } } } zip.close(); Set jsps = jspMap.keySet(); Iterator jspIterator = jsps.iterator(); classPath.append(scanDirectory + "/" + directoryName + "/WEB-INF/classes;"); jarUrls.add(new URL("file:" + scanDirectory + "/" + directoryName + "/WEB-INF/classes/;")); ArrayList<String> jspFiles = new ArrayList(); // log.info(classPath.toString()); if (jspIterator.hasNext()) { jarUrls.add(new URL("file:" + scanDirectory + "/temp/" + directoryName + "/")); customClassLoader = new WebClassLoader(jarUrls.toArray(new URL[jarUrls.size()]), classLoader); while (jspIterator.hasNext()) { String filepackageInternal = (String) jspIterator.next(); String filepackageInternalTmp = filepackageInternal; if (filepackageInternal.lastIndexOf('/') == -1) { filepackageInternal = ""; } else { filepackageInternal = filepackageInternal.substring(0, filepackageInternal.lastIndexOf('/')) .replace("/", "."); filepackageInternal = "." + filepackageInternal; } createDirectory(scanDirectory + "/temp/" + directoryName); File jspFile = new File((String) jspMap.get(filepackageInternalTmp)); String fName = jspFile.getName(); String fileNameWithoutExtension = fName.substring(0, fName.lastIndexOf(".jsp")) + "_jsp"; // String fileCreated=new JspCompiler().compileJsp((String) // jspMap.get(filepackageInternalTmp), // scanDirectory+"/temp/"+fileName, // "com.app.server"+filepackageInternal,classPath.toString()); synchronized (customClassLoader) { String fileNameInWar = filepackageInternalTmp; jspFiles.add(fileNameInWar.replace("/", "\\")); if (fileNameInWar.contains("/") || fileNameInWar.contains("\\")) { customClassLoader.addURL("/" + fileNameInWar.replace("\\", "/"), "com.app.server" + filepackageInternal.replace("WEB-INF", "WEB_002dINF") + "." + fileNameWithoutExtension); } else { customClassLoader.addURL("/" + fileNameInWar, "com.app.server" + filepackageInternal.replace("WEB-INF", "WEB_002dINF") + "." + fileNameWithoutExtension); } } } } else { customClassLoader = new WebClassLoader(jarUrls.toArray(new URL[jarUrls.size()]), classLoader); } String filePath = file.getAbsolutePath(); // log.info("filePath"+filePath); filePath = filePath.substring(0, filePath.toLowerCase().lastIndexOf(".war")); urlClassLoaderMap.put(filePath.replace("\\", "/"), customClassLoader); if (jspFiles.size() > 0) { ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); //Thread.currentThread().setContextClassLoader(customClassLoader); String classPathBefore = ""; try { JspCompiler jspc = new JspCompiler(); jspc.setUriroot(scanDirectory + "/" + directoryName); jspc.setAddWebXmlMappings(false); jspc.setListErrors(false); jspc.setCompile(true); jspc.setOutputDir(scanDirectory + "/temp/" + directoryName + "/"); jspc.setPackage("com.app.server"); StringBuffer buffer = new StringBuffer(); for (String jspFile : jspFiles) { buffer.append(","); buffer.append(jspFile); } String jsp = buffer.toString(); jsp = jsp.substring(1, jsp.length()); //log.info(jsp); classPathBefore = System.getProperty("java.class.path"); System.setProperty("java.class.path", System.getProperty("java.class.path") + ";" + classPath.toString().replace("/", "\\")); //jspc.setClassPath(System.getProperty("java.class.path")+";"+classPath.toString().replace("/", "\\")); jspc.setJspFiles(jsp); jspc.initCL(customClassLoader); jspc.execute(); //jspc.closeClassLoader(); //jspc.closeClassLoader(); } catch (Throwable je) { log.error("Error in compiling the jsp page", je); //je.printStackTrace(); } finally { System.setProperty("java.class.path", classPathBefore); //Thread.currentThread().setContextClassLoader(oldCL); } //Thread.currentThread().setContextClassLoader(customClassLoader); } try { File execxml = new File(scanDirectory + "/" + directoryName + "/WEB-INF/" + "executorservices.xml"); if (execxml.exists()) { new ExecutorServicesConstruct().getExecutorServices(serverdigester, executorServiceMap, execxml, customClassLoader); } } catch (Exception e) { log.error("error in getting executor services ", e); // e.printStackTrace(); } try { File messagingxml = new File( scanDirectory + "/" + directoryName + "/WEB-INF/" + "messagingclass.xml"); if (messagingxml.exists()) { new MessagingClassConstruct().getMessagingClass(messagedigester, messagingxml, customClassLoader, messagingClassMap); } } catch (Exception e) { log.error("Error in getting the messaging classes ", e); // e.printStackTrace(); } webxmldigester.setNamespaceAware(true); webxmldigester.setValidating(true); // digester.setRules(null); FileInputStream webxml = new FileInputStream( scanDirectory + "/" + directoryName + "/WEB-INF/" + "web.xml"); InputSource is = new InputSource(webxml); try { //log.info("SCHEMA"); synchronized (webxmldigester) { // webxmldigester.set("config/web-app_2_4.xsd"); WebAppConfig webappConfig = (WebAppConfig) webxmldigester.parse(is); servletMapping.put(scanDirectory + "/" + directoryName.replace("\\", "/"), webappConfig); } webxml.close(); } catch (Exception e) { log.error("Error in pasrsing the web.xml", e); //e.printStackTrace(); } //customClassLoader.close(); // ClassLoaderUtil.closeClassLoader(customClassLoader); } catch (Exception ex) { log.error("Error in Deploying war " + file.getAbsolutePath(), ex); } }
From source file:org.codehaus.mojo.webstart.JnlpMojo.java
protected void removeSignatures(File currentJar, String shortName) throws IOException { // We should remove any previous signature information. This // can screw up the packing code, and if it is signed with 2 signatures // it can not be verified. ZipFile currentJarZipFile = new ZipFile(currentJar); Enumeration entries = currentJarZipFile.entries(); // There might be more valid extensions but this is what we've seen so far // the ?i makes it case insensitive Pattern signatureChecker = Pattern.compile("(?i)^meta-inf/.*((\\.sf)|(\\.rsa))$"); getLog().debug("checking for old signature : " + shortName); Vector signatureFiles = new Vector(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); Matcher matcher = signatureChecker.matcher(entry.getName()); if (matcher.find()) { // we found a old signature in the file signatureFiles.add(entry.getName()); getLog().warn("found signature: " + entry.getName()); }/*from w w w.j a v a 2 s . c o m*/ } if (signatureFiles.size() == 0) { // no old files were found currentJarZipFile.close(); return; } // We found some old signature files, write out the file again without // them File outFile = new File(currentJar.getParent(), currentJar.getName() + ".unsigned"); ZipOutputStream zipOutStream = new ZipOutputStream(new FileOutputStream(outFile)); entries = currentJarZipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (signatureFiles.contains(entry.getName())) { continue; } InputStream entryInStream = currentJarZipFile.getInputStream(entry); ZipEntry newEntry = new ZipEntry(entry); zipOutStream.putNextEntry(newEntry); IOUtil.copy(entryInStream, zipOutStream); entryInStream.close(); } zipOutStream.close(); currentJarZipFile.close(); FileUtils.rename(outFile, currentJar); getLog().info("removed signature"); }
From source file:edu.umd.cs.marmoset.utilities.ZipExtractor.java
/** * Extract the zip file.// w w w . j a va2s. com * @throws IOException * @throws BuilderException */ public void extract(File directory) throws IOException, ZipExtractorException { ZipFile z = new ZipFile(zipFile); Pattern badName = Pattern.compile("[\\p{Cntrl}<>]"); try { Enumeration<? extends ZipEntry> entries = z.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryName = entry.getName(); if (!shouldExtract(entryName)) continue; if (badName.matcher(entryName).find()) { if (entry.getSize() > 0) getLog().debug("Skipped entry of length " + entry.getSize() + " with bad file name " + java.net.URLEncoder.encode(entryName, "UTF-8")); continue; } try { // Get the filename to extract the entry into. // Subclasses may define this to be something other // than the entry name. String entryFileName = transformFileName(entryName); if (!entryFileName.equals(entryName)) { getLog().debug("Transformed zip entry name: " + entryName + " ==> " + entryFileName); } entriesExtractedFromZipArchive.add(entryFileName); File entryOutputFile = new File(directory, entryFileName).getAbsoluteFile(); File parentDir = entryOutputFile.getParentFile(); if (!parentDir.exists()) { if (!parentDir.mkdirs()) { throw new ZipExtractorException( "Couldn't make directory for entry output file " + entryOutputFile.getPath()); } } if (!parentDir.isDirectory()) { throw new ZipExtractorException( "Parent directory for entry " + entryOutputFile.getPath() + " is not a directory"); } // Make sure the entry output file lies within the build directory. // A malicious zip file might have ".." components in it. getLog().trace("entryOutputFile path: " + entryOutputFile.getCanonicalPath()); if (!entryOutputFile.getCanonicalPath().startsWith(directory.getCanonicalPath() + "/")) { if (!entry.isDirectory()) getLog().warn("Zip entry " + entryName + " accesses a path " + entryOutputFile.getPath() + "outside the build directory " + directory.getPath()); continue; } if (entry.isDirectory()) { entryOutputFile.mkdir(); continue; } // Extract the entry InputStream entryInputStream = null; OutputStream entryOutputStream = null; try { entryInputStream = z.getInputStream(entry); entryOutputStream = new BufferedOutputStream(new FileOutputStream(entryOutputFile)); CopyUtils.copy(entryInputStream, entryOutputStream); } finally { IOUtils.closeQuietly(entryInputStream); IOUtils.closeQuietly(entryOutputStream); } // Hook for subclasses, to specify when entries are // successfully extracted. successfulFileExtraction(entryName, entryFileName); ++numFilesExtacted; } catch (RuntimeException e) { getLog().error("Error extracting " + entryName, e); throw e; } } } finally { z.close(); } }
From source file:it.readbeyond.minstrel.unzipper.Unzipper.java
private String unzip(String inputZip, String destinationDirectory, String mode, JSONObject parameters) throws IOException, JSONException { // store the zip entries to decompress List<String> list = new ArrayList<String>(); // store the zip entries actually decompressed List<String> decompressed = new ArrayList<String>(); // open input zip file File sourceZipFile = new File(inputZip); ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); // open destination directory, creating it if needed File unzipDestinationDirectory = new File(destinationDirectory); unzipDestinationDirectory.mkdirs();/*from www .j av a2 s . c om*/ // extract all files if (mode.equals(ARGUMENT_MODE_ALL)) { Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { list.add(zipFileEntries.nextElement().getName()); } } // extract all files except audio and video // (determined by file extension) if (mode.equals(ARGUMENT_MODE_ALL_NON_MEDIA)) { String[] excludeExtensions = JSONArrayToStringArray( parameters.optJSONArray(ARGUMENT_ARGS_EXCLUDE_EXTENSIONS)); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { String name = zipFileEntries.nextElement().getName(); String lower = name.toLowerCase(); if (!isFile(lower, excludeExtensions)) { list.add(name); } } } // extract all small files // maximum size is passed in args parameter // or, if not passed, defaults to const DEFAULT_MAXIMUM_SIZE_FILE if (mode.equals(ARGUMENT_MODE_ALL_SMALL)) { long maximum_size = parameters.optLong(ARGUMENT_ARGS_MAXIMUM_FILE_SIZE, DEFAULT_MAXIMUM_SIZE_FILE); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { ZipEntry ze = zipFileEntries.nextElement(); if (ze.getSize() <= maximum_size) { list.add(ze.getName()); } } } // extract only the requested files if (mode.equals(ARGUMENT_MODE_SELECTED)) { String[] entries = JSONArrayToStringArray(parameters.optJSONArray(ARGUMENT_ARGS_ENTRIES)); for (String entry : entries) { ZipEntry ze = zipFile.getEntry(entry); if (ze != null) { list.add(entry); } } } // extract all "structural" files if (mode.equals(ARGUMENT_MODE_ALL_STRUCTURE)) { String[] extensions = JSONArrayToStringArray(parameters.optJSONArray(ARGUMENT_ARGS_EXTENSIONS)); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { String name = zipFileEntries.nextElement().getName(); String lower = name.toLowerCase(); boolean extract = isFile(lower, extensions); if (extract) { list.add(name); } } } // NOTE list contains only valid zip entries // perform unzip for (String currentEntry : list) { ZipEntry entry = zipFile.getEntry(currentEntry); File destFile = new File(unzipDestinationDirectory, currentEntry); File destinationParent = destFile.getParentFile(); destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry)); int numberOfBytesRead; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((numberOfBytesRead = is.read(data, 0, BUFFER_SIZE)) > -1) { dest.write(data, 0, numberOfBytesRead); } dest.flush(); dest.close(); is.close(); fos.close(); decompressed.add(currentEntry); } } zipFile.close(); return stringify(decompressed); }
From source file:com.flexive.core.storage.GenericDivisionImporter.java
/** * Import database and filesystem binaries * * @param con an open and valid connection to store imported data * @param zip zip file containing the data * @throws Exception on errors/* w w w .j a v a 2 s . com*/ */ public void importBinaries(Connection con, ZipFile zip) throws Exception { ZipEntry ze = getZipEntry(zip, FILE_BINARIES); Statement stmt = con.createStatement(); try { importTable(stmt, zip, ze, "binaries/binary", DatabaseConst.TBL_CONTENT_BINARY); ZipEntry curr; final String nodeBinDir = FxBinaryUtils.getBinaryDirectory(); File binDir = new File( nodeBinDir + File.separatorChar + String.valueOf(FxContext.get().getDivisionId())); if (!binDir.exists()) //noinspection ResultOfMethodCallIgnored binDir.mkdirs(); if (!binDir.exists() && binDir.isDirectory()) { LOG.error("Failed to create binary directory [" + binDir.getAbsolutePath() + "]!"); return; } int count = 0; for (Enumeration e = zip.entries(); e.hasMoreElements();) { curr = (ZipEntry) e.nextElement(); if (curr.getName().startsWith(FOLDER_FS_BINARY) && !curr.isDirectory()) { File out = new File(binDir.getAbsolutePath() + File.separatorChar + curr.getName().substring(FOLDER_FS_BINARY.length() + 1)); String path = out.getAbsolutePath(); path = path.replace('\\', '/'); //normalize separator chars path = path.replace('/', File.separatorChar); path = path.substring(0, out.getAbsolutePath().lastIndexOf(File.separatorChar)); File fPath = new File(path); if (!fPath.exists()) { if (!fPath.mkdirs()) { LOG.error("Failed to create path [" + path + "!]"); continue; } } if (!out.createNewFile()) { LOG.error("Failed to create file [" + out.getAbsolutePath() + "]!"); continue; } if (FxFileUtils.copyStream2File(curr.getSize(), zip.getInputStream(curr), out)) count++; else LOG.error("Failed to write zip stream to file [" + out.getAbsolutePath() + "]!"); } } FxContext.get().runAsSystem(); try { EJBLookup.getNodeConfigurationEngine().put(SystemParameters.NODE_BINARY_PATH, nodeBinDir); } finally { FxContext.get().stopRunAsSystem(); } LOG.info("Imported [" + count + "] files to filesystem binary storage located at [" + binDir.getAbsolutePath() + "]"); } finally { Database.closeObjects(GenericDivisionImporter.class, stmt); } }
From source file:com.pari.pcb.zip.ZIPProcessor.java
public void process(boolean blocking) { tmpRoot = new File("TEMP" + File.separator + "ZIP_TMP_" + System.nanoTime()); tmpRoot.mkdirs();/*from w w w .j a v a2 s . c o m*/ long start = System.currentTimeMillis(); if (!zipFile.exists()) { errMsg = "File " + fileName + " does not exist"; setCompleted(); return; } if (!zipFile.canRead()) { errMsg = "Unable to read the contents of the file " + fileName; setCompleted(); return; } try { ZipFile inZip = new ZipFile(zipFile); numEntries = inZip.size(); Enumeration<? extends ZipEntry> entryEnumeration = inZip.entries(); if (numEntries == 0) { errMsg = "Empty ZIP file."; setCompleted(); return; } while (entryEnumeration.hasMoreElements()) { ZipEntry entry = entryEnumeration.nextElement(); currentEntryName = entry.getName(); if (entry.isDirectory()) { if (numEntries > 1) { numEntries--; } continue; } currentEntryIdx++; if (currentEntryIdx % 100 == 0) { System.gc(); NCCMObserver.getInstance().logMemoryUsage( "After handling " + currentEntryIdx + " files in Zip Processor, Memory Usage"); } InputStream ipStream = inZip.getInputStream(entry); String fileName = entry.getName(); System.err.println("Processing file: " + fileName); String line = null; try { BufferedReader br = new BufferedReader(new InputStreamReader(ipStream)); line = br.readLine(); CharsetDetector detector = new CharsetDetector(); if (entry.getSize() == 0) { logger.error("No Contents found in the file"); continue; } else { detector.setText(line.getBytes()); charset = Charset.forName(detector.detect().getName()); } } catch (Exception ex) { logger.error("Error while getting the charset encoding of the file"); } finally { try { ipStream.close(); } catch (Exception e) { logger.error("Exception while closing the stream"); } } ipStream = inZip.getInputStream(entry); processZipEntry(ipStream, fileName, entry, inZip); if (progressIf != null) { progressIf.updateProgress(currentEntryName, currentEntryIdx, numEntries); } if (fileResultMap.get(fileName) == null) { ZIPFileResult fr = new ZIPFileResult(fileName); fr.fileType = ZIPFileType.UNKNOWN; fr.msg = "Unable to process the file."; fileResultMap.put(fileName, fr); } try { ipStream.close(); } catch (Exception ex) { logger.warn("Unable to close the inputstream for entry " + entry.getName(), ex); ex.printStackTrace(); } if (isCancelled) { break; } if (!blocking) { Thread.sleep(100 /* msec */); } if (isCancelled) { break; } } if (!isCancelled) { processCatOsVersionFiles(inZip); processFileResult(); } inZip.close(); } catch (ZipException e) { errMsg = "File is not a valid Zip file"; logger.error("Exception while processing ZipFile: " + fileName, e); } catch (IOException e) { errMsg = "Unable to parse Zip file "; logger.error("IOException while processing ZipFile: " + fileName, e); } catch (InterruptedException e) { errMsg = "Zip processing Interrupted internally."; logger.error("Interrupted while processing ZipFile: " + fileName, e); } catch (Exception e) { errMsg = "Exception while processing zip file."; logger.error("Exception while processing ZipFile: " + fileName, e); } logger.debug("Done with : " + numEntries + " in " + (System.currentTimeMillis() - start) + " msecs"); String[] devNames = getDeviceNames(); if (devNames == null || devNames.length == 0) { addWarning("No valid devices found in the specified Zip file."); } setCompleted(); return; }