List of usage examples for java.util.zip ZipInputStream ZipInputStream
public ZipInputStream(InputStream in)
From source file:co.cask.hydrator.action.ftp.FTPCopyAction.java
private void copyZip(FTPClient ftp, String source, FileSystem fs, Path destination) throws IOException { InputStream is = ftp.retrieveFileStream(source); try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) { ZipEntry entry;// w w w .ja va2 s .c o m while ((entry = zis.getNextEntry()) != null) { LOG.debug("Extracting {}", entry); Path destinationPath = fs.makeQualified(new Path(destination, entry.getName())); try (OutputStream os = fs.create(destinationPath)) { LOG.debug("Downloading {} to {}", entry.getName(), destinationPath.toString()); ByteStreams.copy(zis, os); } } } }
From source file:com.github.nethad.clustermeister.provisioning.local.JPPFLocalNode.java
private void unzipNode(InputStream fileToUnzip, File targetDir) { ZipInputStream zipFile;/* w w w. j a v a 2 s .c o m*/ try { zipFile = new ZipInputStream(fileToUnzip); ZipEntry entry; while ((entry = zipFile.getNextEntry()) != null) { // ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { // Assume directories are stored parents first then children. System.err.println("Extracting directory: " + entry.getName()); // This is not robust, just for demonstration purposes. (new File(targetDir, entry.getName())).mkdir(); continue; } System.err.println("Extracting file: " + entry.getName()); File targetFile = new File(targetDir, entry.getName()); copyInputStream_notClosing(zipFile, new BufferedOutputStream(new FileOutputStream(targetFile))); // zipFile.closeEntry(); } zipFile.close(); } catch (IOException ioe) { logger.warn("Unhandled exception.", ioe); } }
From source file:com.facebook.buck.zip.ZipStepTest.java
@Test public void zipWithEmptyDir() throws IOException { Path parent = tmp.newFolder("zipstep"); Path out = parent.resolve("output.zip"); tmp.newFolder("zipdir"); tmp.newFolder("zipdir/foo/"); tmp.newFolder("zipdir/bar/"); ZipStep step = new ZipStep(filesystem, Paths.get("zipstep/output.zip"), ImmutableSet.of(), true, ZipCompressionLevel.DEFAULT_COMPRESSION_LEVEL, Paths.get("zipdir")); assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode()); try (Zip zip = new Zip(out, false)) { assertEquals(ImmutableSet.of("", "foo/", "bar/"), zip.getDirNames()); }/*from w w w . j av a 2s .c o m*/ // Directories should be stored, not deflated as this sometimes causes issues // (e.g. installing an .ipa over the air in iOS 9.1) try (ZipInputStream is = new ZipInputStream(new FileInputStream(out.toFile()))) { for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) { assertEquals(entry.getName(), ZipEntry.STORED, entry.getMethod()); } } }
From source file:com.izforge.izpack.compiler.packager.impl.Packager.java
/** * Copy included jars to primary jar./*from w w w . j av a 2 s . com*/ */ protected void writeIncludedJars() throws IOException { sendMsg("Merging " + includedJarURLs.size() + " jars into installer"); for (Object[] includedJarURL : includedJarURLs) { InputStream is = ((URL) includedJarURL[0]).openStream(); ZipInputStream inJarStream = new ZipInputStream(is); IoHelper.copyZip(inJarStream, primaryJarStream, (List<String>) includedJarURL[1], alreadyWrittenFiles); } }
From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileSelector.java
protected File unzip3(File zf) throws FileNotFoundException { //File f = null; String rename = zf.getAbsolutePath().replaceFirst("\\.zip", identifier + ".xml");//.replaceFirst("\\.gz", ".xml"); File f = new File(rename); try {/*from www. ja v a 2 s . c om*/ FileInputStream fis = new FileInputStream(zf); ZipInputStream zin = new ZipInputStream(fis); ZipEntry ze; final byte[] content = new byte[BUFFER]; while ((ze = zin.getNextEntry()) != null) { f = new File(getCalTempPath() + File.separator + ze.getName()); FileOutputStream fos = new FileOutputStream(f); BufferedOutputStream bos = new BufferedOutputStream(fos, content.length); int n = 0; while (-1 != (n = zin.read(content))) { bos.write(content, 0, n); } bos.flush(); bos.close(); } fis.close(); zin.close(); } catch (IOException ioe) { jlog.error("Error processing Zip " + zf + " Excluding! :: " + ioe); return null; } //try again... what could go wrong /* if (checkMinFileSize(f) && retry_counter<MAX_UNGZIP_RETRIES){ retry_counter++; f.delete(); f = unzip2(zf); } */ return f; }
From source file:com.coinblesk.client.backup.BackupRestoreDialogFragment.java
/** * Extracts all files from a zip to disk. * @param zip data (zip format)/*from w w w . j ava 2 s.c o m*/ * @throws IOException */ private void extractZip(byte[] zip) throws IOException { ZipInputStream zis = null; try { ByteArrayInputStream bais = new ByteArrayInputStream(zip); zis = new ZipInputStream(new BufferedInputStream(bais)); ZipEntry ze = null; // extract all entries Log.d(TAG, "Start extracting backup."); int i = 0; while ((ze = zis.getNextEntry()) != null) { if (!shouldIgnoreZipEntry(ze)) { extractFromZipAndSaveToFile(zis, ze); ++i; } else { Log.d(TAG, "Ignore entry: " + ze.getName()); } } Log.i(TAG, "Extracted " + i + " elements from backup."); } finally { if (zis != null) { try { zis.close(); } catch (IOException e) { Log.w(TAG, "Could not close zip input stream.", e); } } } }
From source file:com.googlecode.osde.internal.profiling.Profile.java
/** * Unzips a zip content into a physical folder. * * @return The newly-created folder with the unzipped content. *//*from www . j a va 2 s . com*/ private static File unzip(InputStream resource, File output) throws IOException { // Allocate a 16K buffer to read the XPI file faster. ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(resource, 16 * 1024)); ZipEntry entry = zipStream.getNextEntry(); while (entry != null) { final File target = new File(output, entry.getName()); if (entry.isDirectory()) { forceMkdir(target); } else { unzipFile(target, zipStream); } entry = zipStream.getNextEntry(); } return output; }
From source file:gov.nih.nci.caarray.web.action.ArrayDesignActionTest.java
License:asdf
@Test public void testDownload() throws Exception { assertEquals("list", this.arrayDesignAction.download()); final CaArrayFile rawFile = this.fasStub.add(AffymetrixArrayDesignFiles.TEST3_CDF); final ArrayDesign arrayDesign = new ArrayDesign(); arrayDesign.setName("Test3"); final CaArrayFileSet designFileSet = new CaArrayFileSet(); designFileSet.add(rawFile);/*from w w w. j av a 2 s.co m*/ arrayDesign.setDesignFileSet(designFileSet); this.arrayDesignAction.setArrayDesign(arrayDesign); this.arrayDesignAction.download(); assertEquals("application/zip", this.mockResponse.getContentType()); assertEquals("filename=\"caArray_Test3_file.zip\"", this.mockResponse.getHeader("Content-disposition")); final ZipInputStream zis = new ZipInputStream( new ByteArrayInputStream(this.mockResponse.getContentAsByteArray())); final ZipEntry ze = zis.getNextEntry(); assertNotNull(ze); assertEquals("Test3.CDF", ze.getName()); assertNull(zis.getNextEntry()); IOUtils.closeQuietly(zis); }
From source file:com.esofthead.mycollab.jetty.GenericServerRunner.java
private static void unpackFile(File upgradeFile) throws IOException { File libFolder = new File(System.getProperty("user.dir"), "lib"); File webappFolder = new File(System.getProperty("user.dir"), "webapp"); org.apache.commons.io.FileUtils.deleteDirectory(libFolder); org.apache.commons.io.FileUtils.deleteDirectory(webappFolder); byte[] buffer = new byte[2048]; try (ZipInputStream inputStream = new ZipInputStream(new FileInputStream(upgradeFile))) { ZipEntry entry;// w w w . j a v a 2 s.c o m while ((entry = inputStream.getNextEntry()) != null) { if (!entry.isDirectory() && (entry.getName().startsWith("lib/") || entry.getName().startsWith("webapp"))) { File candidateFile = new File(System.getProperty("user.dir"), entry.getName()); candidateFile.getParentFile().mkdirs(); try (FileOutputStream output = new FileOutputStream(candidateFile)) { int len; while ((len = inputStream.read(buffer)) > 0) { output.write(buffer, 0, len); } } } } } catch (IOException e) { throw new MyCollabException(e); } }
From source file:com.mcleodmoores.mvn.natives.UnpackDependenciesMojo.java
private void unpack(final Artifact artifact, final Map<String, Set<Artifact>> names, final File targetDir) throws MojoFailureException { getLog().info("Unpacking " + ArtifactUtils.key(artifact)); final IOExceptionHandler errorLog = new MojoLoggingErrorCallback(this); check(artifact, (new IOCallback<InputStream, Boolean>(open(artifact)) { @Override//www . ja v a2 s .c o m protected Boolean apply(final InputStream input) throws IOException { final byte[] buffer = new byte[4096]; final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(input)); ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { final String dest = createUniqueName(artifact, entry.getName(), names.get(entry.getName())); getLog().debug("Writing " + entry.getName() + " as " + dest); File targetFile = targetDir; for (final String component : dest.split("/")) { targetFile.mkdir(); targetFile = new File(targetFile, component); } targetFile.getParentFile().mkdirs(); if ((new IOCallback<OutputStream, Boolean>(getOutputStreams().open(targetFile)) { @Override protected Boolean apply(final OutputStream output) throws IOException { int bytes; while ((bytes = zip.read(buffer, 0, buffer.length)) > 0) { output.write(buffer, 0, bytes); } output.close(); return Boolean.TRUE; } }).call(errorLog) != Boolean.TRUE) { return Boolean.FALSE; } } return Boolean.TRUE; } }).call(errorLog)); }