List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:io.sledge.core.impl.installer.SledgePackageConfigurer.java
private void createNewZipfileWithReplacedPlaceholders(InputStream packageStream, Path configurationPackagePath, Properties envProps) throws IOException { // For reading the original configuration package ZipInputStream configPackageZipStream = new ZipInputStream(new BufferedInputStream(packageStream), Charset.forName("UTF-8")); // For outputting the configured (placeholders replaced) version of the package as zip file File outZipFile = configurationPackagePath.toFile(); ZipOutputStream outConfiguredZipStream = new ZipOutputStream(new FileOutputStream(outZipFile)); ZipEntry zipEntry; while ((zipEntry = configPackageZipStream.getNextEntry()) != null) { if (zipEntry.isDirectory()) { configPackageZipStream.closeEntry(); continue; } else {//w ww. ja va2 s .c o m ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; byte[] buffer = new byte[2048]; while ((length = configPackageZipStream.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, length); } InputStream zipEntryInputStream = new BufferedInputStream( new ByteArrayInputStream(output.toByteArray())); if (zipEntry.getName().endsWith("instance.properties")) { ByteArrayOutputStream envPropsOut = new ByteArrayOutputStream(); envProps.store(envPropsOut, "Environment configurations"); zipEntryInputStream = new BufferedInputStream( new ByteArrayInputStream(envPropsOut.toByteArray())); } else if (isTextFile(zipEntry.getName(), zipEntryInputStream)) { String configuredContent = StrSubstitutor.replace(output, envProps); zipEntryInputStream = new BufferedInputStream( new ByteArrayInputStream(configuredContent.getBytes())); } // Add to output zip file addToZipFile(zipEntry.getName(), zipEntryInputStream, outConfiguredZipStream); zipEntryInputStream.close(); configPackageZipStream.closeEntry(); } } outConfiguredZipStream.close(); configPackageZipStream.close(); }
From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java
protected void processZipFileItem(FileItemFactory factory, FileItem zip, List<FileItem> fileItems) throws IOException { ZipInputStream ziS = new ZipInputStream(zip.getInputStream()); ZipEntry zE; try {//from www .ja v a 2s . c om while (null != (zE = ziS.getNextEntry())) { if (zE.isDirectory() || !uploadFileNameFilter.accept(null, zE.getName())) continue; FileItem item = factory.createItem(null, null, false, zE.getName()); OutputStream oS = item.getOutputStream(); IOUtils.copyLarge(ziS, oS); oS.close(); fileItems.add(item); } } finally { ziS.close(); } }
From source file:com.aurel.track.lucene.util.openOffice.OOIndexer.java
private List unzip(String zip, String destination) { List destLs = new ArrayList(); Enumeration entries;//from w w w. j ava2s . c o m ZipFile zipFile; File dest = new File(destination); dest.mkdir(); if (dest.isDirectory()) { try { zipFile = new ZipFile(zip); entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { (new File(dest.getAbsolutePath() + File.separator + entry.getName())).mkdirs(); continue; } if (entry.getName().lastIndexOf("/") > 0) { File f = new File(dest.getAbsolutePath() + File.separator + entry.getName().substring(0, entry.getName().lastIndexOf("/"))); f.mkdirs(); } copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(dest.getAbsolutePath() + File.separator + entry.getName()))); destLs.add(dest.getAbsolutePath() + File.separator + TMP_UNZIP_DIR + File.separator + entry.getName()); } zipFile.close(); } catch (IOException e) { deleteDir(new File(destination)); LOGGER.error(e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } } else { LOGGER.info("There is already a file by that name"); } return destLs; }
From source file:com.flexive.tests.disttools.DistPackageTest.java
/** * Extracts the flexive-dist.zip package and returns the (temporary) directory handle. All extracted * files will be registered for deletion on the JVM exit. * * @return the (temporary) directory handle. * @throws IOException if the package could not be extracted */// w w w.j a v a 2s.c o m private File extractDistPackage() throws IOException { final ZipFile file = getDistFile(); final File tempDir = createTempDir(); try { final Enumeration<? extends ZipEntry> entries = file.entries(); while (entries.hasMoreElements()) { final ZipEntry zipEntry = entries.nextElement(); final String path = getTempPath(tempDir, zipEntry); if (zipEntry.isDirectory()) { final File dir = new File(path); dir.mkdirs(); dir.deleteOnExit(); } else { final InputStream in = file.getInputStream(zipEntry); final OutputStream out = new BufferedOutputStream(new FileOutputStream(path)); // copy streams final byte[] buffer = new byte[32768]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.close(); new File(path).deleteOnExit(); } } } finally { file.close(); } return new File(tempDir + File.separator + "flexive-dist"); }
From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.AddDataFileController.java
/** * Processing of the valid form/*from w w w .j a v a 2 s .c o m*/ */ @Override protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException bindException) throws Exception { log.debug("Processing form data."); AddDataFileCommand addDataCommand = (AddDataFileCommand) command; MultipartHttpServletRequest mpRequest = (MultipartHttpServletRequest) request; // the map containing file names mapped to files Map m = mpRequest.getFileMap(); Set set = m.keySet(); for (Object key : set) { MultipartFile file = (MultipartFile) m.get(key); if (file == null) { log.error("No file was uploaded!"); } else { log.debug("Creating measuration with ID " + addDataCommand.getMeasurationId()); Experiment experiment = new Experiment(); experiment.setExperimentId(addDataCommand.getMeasurationId()); if (file.getOriginalFilename().endsWith(".zip")) { ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(file.getBytes())); ZipEntry en = zis.getNextEntry(); while (en != null) { if (en.isDirectory()) { en = zis.getNextEntry(); continue; } DataFile data = new DataFile(); data.setExperiment(experiment); String name[] = en.getName().split("/"); data.setFilename(name[name.length - 1]); data.setDescription(addDataCommand.getDescription()); data.setFileContent(Hibernate.createBlob(zis)); String[] partOfName = en.getName().split("[.]"); data.setMimetype(partOfName[partOfName.length - 1]); dataFileDao.create(data); en = zis.getNextEntry(); } } else { log.debug("Creating new Data object."); DataFile data = new DataFile(); data.setExperiment(experiment); log.debug("Original name of uploaded file: " + file.getOriginalFilename()); String filename = file.getOriginalFilename().replace(" ", "_"); data.setFilename(filename); log.debug("MIME type of the uploaded file: " + file.getContentType()); if (file.getContentType().length() > MAX_MIMETYPE_LENGTH) { int index = filename.lastIndexOf("."); data.setMimetype(filename.substring(index)); } else { data.setMimetype(file.getContentType()); } log.debug("Parsing the sapmling rate."); data.setDescription(addDataCommand.getDescription()); log.debug("Setting the binary data to object."); data.setFileContent(Hibernate.createBlob(file.getBytes())); dataFileDao.create(data); log.debug("Data stored into database."); } } } log.debug("Returning MAV"); ModelAndView mav = new ModelAndView( "redirect:/experiments/detail.html?experimentId=" + addDataCommand.getMeasurationId()); return mav; }
From source file:com.servoy.extension.install.CopyZipEntryImporter.java
public void handleFile() { if (expFile != null && expFile.exists() && expFile.isFile() && expFile.canRead() && expFile.getName().endsWith(FileBasedExtensionProvider.EXTENSION_PACKAGE_FILE_EXTENSION) && installDir != null && installDir.exists() && installDir.isDirectory() && installDir.canWrite()) { ZipFile zipFile = null;//w w w . j a v a 2s .co m try { zipFile = new ZipFile(expFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.isDirectory()) { String fileName = entry.getName().replace('\\', '/'); fileName = fileName.replace(WEBTEMPLATES_SOURCE_FOLDER, WEBTEMPLATES_DESTINATION_FOLDER); File outputFile = new File(installDir, fileName); handleZipEntry(outputFile, zipFile, entry); } } } catch (IOException ex) { String tmp = "Exception while handling entries of expFile: " + expFile; //$NON-NLS-1$ messages.addError(tmp); Debug.error(tmp, ex); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { // ignore } } enforceBackUpFolderLimit(); } handleExpFile(); } else { // shouldn't happen String tmp = "Invalid install/uninstall file/destination: " + expFile + ", " + installDir; //$NON-NLS-1$//$NON-NLS-2$ messages.addError(tmp); Debug.error(tmp); } }
From source file:edu.sabanciuniv.sentilab.sare.controllers.base.documentStore.NonDerivedStoreFactory.java
protected NonDerivedStoreFactory<T> addZipPacket(T store, InputStream input) throws IOException { Validate.notNull(store, CannedMessages.NULL_ARGUMENT, "store"); Validate.notNull(input, CannedMessages.NULL_ARGUMENT, "input"); ZipInputStream zipStream = new ZipInputStream(input); ZipEntry zipEntry; while ((zipEntry = zipStream.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { // we create a byte stream so that the input stream is not closed by the underlying methods. this.createSpecific(store, new ByteArrayInputStream(IOUtils.toByteArray(zipStream)), FilenameUtils.getExtension(zipEntry.getName())); }/* www . j av a2s . c o m*/ } return this; }
From source file:org.apache.jackrabbit.j2ee.BackwardsCompatibilityIT.java
private void unpack(File archive, File dir) throws IOException { ZipFile zip = new ZipFile(archive); try {/*from w w w .j av a 2 s. c o m*/ Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().startsWith("META-INF")) { } else if (entry.isDirectory()) { new File(dir, entry.getName()).mkdirs(); } else { File file = new File(dir, entry.getName()); file.getParentFile().mkdirs(); InputStream input = zip.getInputStream(entry); try { OutputStream output = new FileOutputStream(file); try { IOUtils.copy(input, output); } finally { output.close(); } } finally { input.close(); } } } } finally { zip.close(); } }
From source file:com.izforge.izpack.compiler.helper.CompilerHelper.java
/** * Returns a list which contains the pathes of all files which are included in the given url. * This method expects as the url param a jar. * * @param url url of the jar file//from w ww.j a va 2 s .co m * @return full qualified paths of the contained files * @throws IOException if the jar cannot be read */ public List<String> getContainedFilePaths(URL url) throws IOException { JarInputStream jis = new JarInputStream(url.openStream()); ZipEntry zentry; ArrayList<String> fullNames = new ArrayList<String>(); while ((zentry = jis.getNextEntry()) != null) { String name = zentry.getName(); // Add only files, no directory entries. if (!zentry.isDirectory()) { fullNames.add(name); } } jis.close(); return (fullNames); }
From source file:com.gatf.executor.report.ReportHandler.java
/** * @param zipFile/* w w w . j av a2s . co m*/ * @param directoryToExtractTo Provides file unzip functionality */ public static void unzipZipFile(InputStream zipFile, String directoryToExtractTo) { ZipInputStream in = new ZipInputStream(zipFile); try { File directory = new File(directoryToExtractTo); if (!directory.exists()) { directory.mkdirs(); logger.info("Creating directory for Extraction..."); } ZipEntry entry = in.getNextEntry(); while (entry != null) { try { File file = new File(directory, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[2048]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); } in.closeEntry(); entry = in.getNextEntry(); } catch (Exception e) { logger.severe(ExceptionUtils.getStackTrace(e)); } } } catch (IOException ioe) { logger.severe(ExceptionUtils.getStackTrace(ioe)); return; } }