List of usage examples for java.util.zip ZipFile getInputStream
public InputStream getInputStream(ZipEntry entry) throws IOException
From source file:fll.web.admin.UploadSubjectiveData.java
/** * Save the data stored in file to the database and update the subjective * score totals.//from w w w .j ava 2 s . c o m * * @param file the file to read the data from * @param connection the database connection to write to * @throws SAXException if there is an error parsing the document */ public static void saveSubjectiveData(final File file, final int currentTournament, final ChallengeDescription challengeDescription, final Connection connection, final ServletContext application) throws SQLException, IOException, ParseException, SAXException { if (LOGGER.isDebugEnabled()) { try { LOGGER.debug("Saving uploaded file to "); final String baseFilename = "subjective-upload_" + DATE_TIME_FORMAT.get().format(new Date()); final String filename = application.getRealPath("/WEB-INF/" + baseFilename); final File copy = new File(filename); FileOutputStream output = null; FileInputStream input = null; try { input = new FileInputStream(file); output = new FileOutputStream(copy); IOUtils.copy(input, output); } finally { IOUtils.closeQuietly(input); IOUtils.closeQuietly(output); } } catch (final IOException e) { LOGGER.debug("Error creating copy of subjective datafile", e); } } ZipFile zipfile = null; Document scoreDocument = null; try { try { zipfile = new ZipFile(file); // read in score data final ZipEntry scoreZipEntry = zipfile.getEntry("score.xml"); if (null == scoreZipEntry) { throw new RuntimeException("Zipfile does not contain score.xml as expected"); } final InputStream scoreStream = zipfile.getInputStream(scoreZipEntry); scoreDocument = XMLUtils.parseXMLDocument(scoreStream); scoreStream.close(); zipfile.close(); } catch (final ZipException ze) { LOGGER.info("Subjective upload is not a zip file, trying as an XML file"); // not a zip file, parse as just the XML file FileInputStream fis = null; try { fis = new FileInputStream(file); scoreDocument = XMLUtils.parseXMLDocument(fis); } finally { IOUtils.closeQuietly(fis); } } if (null == scoreDocument) { throw new FLLRuntimeException( "Cannot parse input as a compressed subjective data file or an uncompressed XML file"); } saveSubjectiveData(scoreDocument, currentTournament, challengeDescription, connection); } finally { if (null != zipfile) { zipfile.close(); } } }
From source file:org.jaggeryjs.jaggery.core.manager.JaggeryDeployerManager.java
private static JSONObject readJaggeryConfig(Context context, Path appBase) { String content = null;/*from w w w . ja v a 2 s . c om*/ String path = null; if (context.getDocBase().contains(WAR_EXTENSION)) { try { if (!appBase.endsWith("/")) { path = appBase + File.separator + context.getDocBase(); } else { path = appBase + context.getDocBase(); } ZipFile zip = new ZipFile(path); for (Enumeration e = zip.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); if (entry.getName().toLowerCase().contains(JAGGERY_CONF)) { InputStream inputStream = zip.getInputStream(entry); content = IOUtils.toString(inputStream); } } } catch (IOException e) { log.error("Error occuered when the accessing the jaggery.conf file of " + context.getPath().substring(1), e); } } else { File file = new File(appBase + context.getPath() + File.separator + JAGGERY_CONF); try { content = FileUtils.readFileToString(file); } catch (IOException e) { log.error("IOException is thrown when accessing the jaggery.conf file of " + context.getPath().substring(1), e); } } JSONObject jaggeryConfig = null; try { JSONParser jp = new JSONParser(); jaggeryConfig = (JSONObject) jp.parse(content); } catch (ParseException e) { log.error("Error in parsing the jaggery.conf file", e); } return jaggeryConfig; }
From source file:com.cenrise.test.azkaban.Utils.java
public static void unzip(final ZipFile source, final File dest) throws IOException { final Enumeration<?> entries = source.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = (ZipEntry) entries.nextElement(); final File newFile = new File(dest, entry.getName()); if (entry.isDirectory()) { newFile.mkdirs();/*from w w w.j a v a2s. co m*/ } else { newFile.getParentFile().mkdirs(); final InputStream src = source.getInputStream(entry); try { final OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile)); try { IOUtils.copy(src, output); } finally { output.close(); } } finally { src.close(); } } } }
From source file:azkaban.common.utils.Utils.java
public static void unzip(ZipFile source, File dest) throws IOException { Enumeration<?> entries = source.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); File newFile = new File(dest, entry.getName()); if (entry.isDirectory()) { newFile.mkdirs();//ww w .j a va2s.c o m } else { newFile.getParentFile().mkdirs(); InputStream src = source.getInputStream(entry); OutputStream output = new BufferedOutputStream(new FileOutputStream(newFile)); IOUtils.copy(src, output); src.close(); output.close(); } } }
From source file:it.geosolutions.tools.compress.file.Extractor.java
/** * Inflate the provided {@link ZipFile} in the provided output directory. * //from w w w . ja v a2 s . c o m * @param archive * the {@link ZipFile} to inflate. * @param outputDirectory * the directory where to inflate the archive. * @throws IOException * in case something bad happens. * @throws FileNotFoundException * in case something bad happens. */ public static void inflate(ZipFile archive, File outputDirectory, String fileName) throws IOException, FileNotFoundException { final Enumeration<? extends ZipEntry> entries = archive.entries(); try { while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (!entry.isDirectory()) { final String name = entry.getName(); final String ext = FilenameUtils.getExtension(name); final InputStream in = new BufferedInputStream(archive.getInputStream(entry)); final File outFile = new File(outputDirectory, fileName != null ? new StringBuilder(fileName).append(".").append(ext).toString() : name); final OutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); IOUtils.copyStream(in, out, true, true); } } } finally { try { archive.close(); } catch (Throwable e) { if (LOGGER.isTraceEnabled()) LOGGER.error("unable to close archive.\nMessage is: " + e.getMessage(), e); } } }
From source file:org.apache.geode.management.internal.configuration.utils.ZipUtils.java
public static void unzip(String zipFilePath, String outputDirectoryPath) throws IOException { ZipFile zipFile = new ZipFile(zipFilePath); @SuppressWarnings("unchecked") Enumeration<ZipEntry> zipEntries = (Enumeration<ZipEntry>) zipFile.entries(); try {/*from w ww.j a v a 2 s . c o m*/ while (zipEntries.hasMoreElements()) { ZipEntry zipEntry = zipEntries.nextElement(); String fileName = outputDirectoryPath + File.separator + zipEntry.getName(); if (zipEntry.isDirectory()) { FileUtils.forceMkdir(new File(fileName)); continue; } File entryDestination = new File(fileName); File parent = entryDestination.getParentFile(); if (parent != null) { FileUtils.forceMkdir(parent); } if (entryDestination.createNewFile()) { InputStream in = zipFile.getInputStream(zipEntry); OutputStream out = new FileOutputStream(entryDestination); try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } else { throw new IOException("Cannot create file :" + entryDestination.getCanonicalPath()); } } } finally { zipFile.close(); } }
From source file:com.headswilllol.basiclauncher.Launcher.java
public static void unzip(ZipFile zip, ZipEntry entry, File dest) { // convenience method for unzipping from archive if (dest.exists()) dest.delete();/*w ww . j a va 2s .c om*/ dest.getParentFile().mkdirs(); try { BufferedInputStream bIs = new BufferedInputStream(zip.getInputStream(entry)); int b; byte buffer[] = new byte[1024]; FileOutputStream fOs = new FileOutputStream(dest); BufferedOutputStream bOs = new BufferedOutputStream(fOs, 1024); while ((b = bIs.read(buffer, 0, 1024)) != -1) bOs.write(buffer, 0, b); bOs.flush(); bOs.close(); bIs.close(); } catch (Exception ex) { ex.printStackTrace(); createExceptionLog(ex); progress = "Failed to unzip " + entry.getName(); fail = "Errors occurred; see log file for details"; launcher.paintImmediately(0, 0, width, height); } }
From source file:org.eclipse.thym.core.internal.util.FileUtils.java
/** * Copies the contents of a source file to the destination file. * It replaces the value pairs passed on the templatesValues while * copying. /*from www . j a v a 2 s. co m*/ * * @param source file on the file system or jar file * @param destination file on the file system * @param templateValues value pairs to be replaced * @throws IOException * @throws IllegalArgumentException * <ul> * <li>source or destination is null or not a file</li> * <li>destination is not a file URL</li> * </ul> */ public static void templatedFileCopy(URL source, URL destination, Map<String, String> templateValues) throws IOException { checkCanCopy(source, destination); if (templateValues == null) throw new IllegalArgumentException("Template values can not be null"); source = getFileURL(source); destination = getFileURL(destination); File dstFile = new File(destination.getFile()); BufferedReader in = null; BufferedWriter out = null; try { out = new BufferedWriter(new FileWriter(dstFile)); if ("file".equals(source.getProtocol())) { File srcFile = new File(source.getFile()); in = new BufferedReader(new FileReader(srcFile)); } else if ("jar".equals(source.getProtocol())) { ZipFile zipFile = getZipFile(source); String file = source.getFile(); int exclamation = file.indexOf('!'); String jarLocation = file.substring(exclamation + 2); // remove jar separator !/ ZipEntry zipEntry = zipFile.getEntry(jarLocation); if (zipEntry == null) { throw new IllegalArgumentException(source + " can not be found on the zip file"); } InputStream zipStream = zipFile.getInputStream(zipEntry); in = new BufferedReader(new InputStreamReader(zipStream)); } String line; while ((line = in.readLine()) != null) { for (Map.Entry<String, String> entry : templateValues.entrySet()) { line = line.replace(entry.getKey(), entry.getValue()); } out.write(line); out.newLine(); } } finally { if (out != null) out.close(); if (in != null) in.close(); } }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * <p>Loads a properties file from a zip file and updates 2 properties in that properties file.</p> * <p>The properties file in the zip is not written back to the zip file with the updates.</p> * @param inZip The zip file to load the properties file from. * @param cadmiumPropertiesEntry The entry of a properties file in the zip to load. * @param repoUri The value to set the "com.meltmedia.cadmium.git.uri" property with. * @param branch The value to set the "com.meltmedia.cadmium.branch" property with. * @param configRepoUri The value to set the "com.meltmedia.cadmium.config.git.uri" property with. * @param configBranch The value to set the "com.meltmedia.cadmium.config.branch" property with. * @return The updated properties object that was loaded from the zip file. * @throws IOException/* ww w .j a v a 2s .c om*/ */ public static Properties updateProperties(ZipFile inZip, ZipEntry cadmiumPropertiesEntry, String repoUri, String branch, String configRepoUri, String configBranch) throws IOException { Properties cadmiumProps = new Properties(); cadmiumProps.load(inZip.getInputStream(cadmiumPropertiesEntry)); if (org.apache.commons.lang3.StringUtils.isNotBlank(repoUri)) { cadmiumProps.setProperty("com.meltmedia.cadmium.git.uri", repoUri); } if (branch != null) { cadmiumProps.setProperty("com.meltmedia.cadmium.branch", branch); } if (org.apache.commons.lang3.StringUtils.isNotBlank(configRepoUri) && !org.apache.commons.lang3.StringUtils .equals(configRepoUri, cadmiumProps.getProperty("com.meltmedia.cadmium.git.uri"))) { cadmiumProps.setProperty("com.meltmedia.cadmium.config.git.uri", configRepoUri); } else if (org.apache.commons.lang3.StringUtils.equals(configRepoUri, cadmiumProps.getProperty("com.meltmedia.cadmium.git.uri"))) { cadmiumProps.remove("com.meltmedia.cadmium.config.git.uri"); } if (configBranch != null) { cadmiumProps.setProperty("com.meltmedia.cadmium.config.branch", configBranch); } return cadmiumProps; }
From source file:de.shadowhunt.subversion.internal.AbstractPrepare.java
private static boolean extractArchive(final File zip, final File prefix) throws Exception { final ZipFile zipFile = new ZipFile(zip); final Enumeration<? extends ZipEntry> enu = zipFile.entries(); while (enu.hasMoreElements()) { final ZipEntry zipEntry = enu.nextElement(); final String name = zipEntry.getName(); final File file = new File(prefix, name); if (name.charAt(name.length() - 1) == Resource.SEPARATOR_CHAR) { if (!file.isDirectory() && !file.mkdirs()) { throw new IOException("can not create directory structure: " + file); }//from www.j a v a2 s .c om continue; } final File parent = file.getParentFile(); if (parent != null) { if (!parent.isDirectory() && !parent.mkdirs()) { throw new IOException("can not create directory structure: " + parent); } } final InputStream is = zipFile.getInputStream(zipEntry); final FileOutputStream fos = new FileOutputStream(file); final byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) >= 0) { fos.write(bytes, 0, length); } is.close(); fos.close(); } zipFile.close(); return true; }