List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java
/** * Extra dir from the jar to destdir/*from www.j a va 2 s . c o m*/ * * @param jarpath * @param dir * @param destdir */ public static void extractDirFromJar(String jarpath, String dir, String destdir) { ZipFile zipFile = null; try { zipFile = new ZipFile(jarpath); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries != null && entries.hasMoreElements()) { ZipEntry ze = entries.nextElement(); if (!ze.isDirectory() && ze.getName().startsWith(dir)) { InputStream in = zipFile.getInputStream(ze); try { File file = new File(destdir, ze.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { if (in != null) in.close(); } } } } catch (Exception e) { LOG.warn("No " + dir + " from " + jarpath + "!\n" + e.getMessage()); } finally { if (zipFile != null) try { zipFile.close(); } catch (Exception e) { LOG.warn(e.getMessage()); } } }
From source file:com.dclab.preparation.ReadTest.java
public String handleZip(final ZipFile zf, final ZipEntry ze) throws IOException { if (ze.isDirectory()) { System.out.println(ze.getName() + " is folder "); } else if (ze.getName().endsWith(".xml")) { Logger.getAnonymousLogger().info("process file " + ze.getName()); String s = ze.toString(); // ByteBuffer bb = ByteBuffer.allocate(MAX_CAPACITY); InputStream is = zf.getInputStream(ze); byte[] bytes = IOUtils.toByteArray(is); return extract(new String(bytes)); //scan( sr ); }//from w ww . ja va2 s . c o m return null; }
From source file:dk.deck.resolver.util.ZipUtils.java
private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); return;/*from w ww . j a v a 2 s . c om*/ } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } // log.debug("Extracting: " + entry); BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { outputStream.close(); inputStream.close(); } }
From source file:JarResource.java
private List<String> load(InputStream is) throws IOException { List<String> jarContents = new ArrayList<String>(); try {/*ww w . jav a2 s. c o m*/ ZipInputStream zis = new ZipInputStream(is); ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.isDirectory()) { continue; } jarContents.add(ze.getName()); ze = zis.getNextEntry(); } } catch (NullPointerException e) { System.out.println("done."); } return jarContents; }
From source file:com.android.tradefed.util.FileUtil.java
/** * Utility method to extract entire contents of zip file into given * directory//from ww w . j av a 2s. c o m * * @param zipFile * the {@link ZipFile} to extract * @param destDir * the local dir to extract file to * @throws IOException * if failed to extract file */ public static void extractZip(ZipFile zipFile, File destDir) throws IOException { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File childFile = new File(destDir, entry.getName()); childFile.getParentFile().mkdirs(); if (entry.isDirectory()) { continue; } else { FileUtil.writeToFile(zipFile.getInputStream(entry), childFile); } } }
From source file:eionet.webq.converter.MultipartFileToUserFileConverter.java
/** * Extract files from zip archive, not recursive. * * @param uploadedFile uploaded zip file * @return collection of zip files./* www . jav a 2 s. c o m*/ */ private Collection<UserFile> extractFromZip(UploadedFile uploadedFile) { LOGGER.info("Start extraction from zip file with name=" + uploadedFile.getName()); final List<UserFile> userFiles = new ArrayList<UserFile>(); ZipUtil.iterate(new ByteArrayInputStream(uploadedFile.getContent().getFileContent()), new ZipEntryCallback() { @Override public void process(InputStream inputStream, ZipEntry zipEntry) throws IOException { if (!zipEntry.isDirectory()) { byte[] content = IOUtils.toByteArray(inputStream); String xmlSchema = defaultString(xmlSchemaExtractor.extractXmlSchema(content), DUMMY_XML_SCHEMA); userFiles.add(new UserFile(new UploadedFile(zipEntry.getName(), content), xmlSchema)); } } }); LOGGER.info("Extracted " + userFiles.size() + " from zip archive."); return userFiles; }
From source file:it.tidalwave.northernwind.importer.infoglue.ExportConverter.java
protected void addLibraries(final @Nonnull String zippedLibraryPath, final @Nonnull ZonedDateTime dateTime) throws IOException { final @Cleanup ZipFile zipFile = new ZipFile(zippedLibraryPath); final Enumeration enumeration = zipFile.entries(); while (enumeration.hasMoreElements()) { final ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); if (!zipEntry.isDirectory()) { // System.out.println("Unzipping: " + zipEntry.getName()); final @Cleanup InputStream is = new BufferedInputStream(zipFile.getInputStream(zipEntry)); ResourceManager.addCommand(new AddResourceCommand(dateTime, zipEntry.getName(), IOUtils.toByteArray(is), "Extracted from library")); }//from ww w .j ava2s . c om } }
From source file:asciidoc.maven.plugin.tools.ZipHelper.java
public void unzipEntry(ZipFile zipfile, ZipEntry zipEntry, File outputDir) throws IOException { if (zipEntry.isDirectory()) { createDir(new File(outputDir, zipEntry.getName())); return;// w ww . j av a 2 s .c om } File outputFile = new File(outputDir, zipEntry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } if (this.log.isDebugEnabled()) this.log.debug("Extracting " + zipEntry); BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(zipEntry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { outputStream.close(); inputStream.close(); } if ((outputFile != null) && (!outputFile.isDirectory() && FileHelper.getFileExtension(outputFile).equalsIgnoreCase("py"))) { outputFile.setExecutable(true); } }
From source file:asciidoc.maven.plugin.AbstractAsciiDocMojo.java
private void unzipEntry(ZipFile zipfile, ZipEntry zipEntry, File outputDir) throws IOException { if (zipEntry.isDirectory()) { createDir(new File(outputDir, zipEntry.getName())); return;//from w ww .ja v a 2s . com } File outputFile = new File(outputDir, zipEntry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } if (getLog().isDebugEnabled()) getLog().debug("Extracting " + zipEntry); BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(zipEntry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { outputStream.close(); inputStream.close(); } }
From source file:br.com.ingenieux.mojo.jbake.SeedMojo.java
private void unpackZip(File tmpZipFile) throws IOException { ZipInputStream zis = new ZipInputStream(new FileInputStream(tmpZipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.isDirectory()) { ze = zis.getNextEntry();/*www.j a v a 2 s. c o m*/ continue; } String fileName = stripLeadingPath(ze.getName()); File newFile = new File(outputDirectory + File.separator + fileName); new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); IOUtils.copy(zis, fos); fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }