List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:io.sledge.core.impl.extractor.SledgeApplicationPackageExtractor.java
@Override public DeploymentConfiguration getDeploymentConfiguration(InputStream appPackageInputStream) { DeploymentConfiguration deploymentConfig = null; ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(appPackageInputStream), Charset.forName("UTF-8")); try {// w ww. j a v a2 s . co m byte[] buffer = new byte[2048]; ZipEntry zipEntry = null; while ((zipEntry = zipStream.getNextEntry()) != null) { if (zipEntry.isDirectory()) { zipStream.closeEntry(); continue; } if (zipEntry.getName().startsWith(SLEDGEFILE_XML)) { ByteArrayOutputStream output = new ByteArrayOutputStream(); int length; while ((length = zipStream.read(buffer, 0, buffer.length)) >= 0) { output.write(buffer, 0, length); } DeploymentConfigurationReader deploymentConfigReader = new DeploymentConfigurationReaderXml(); deploymentConfig = deploymentConfigReader .parseDeploymentConfiguration(new ByteArrayInputStream(output.toByteArray())); zipStream.closeEntry(); // Stop here, the file is read break; } } } catch (IOException e) { log.error(e.getMessage(), e); } finally { try { zipStream.close(); appPackageInputStream.reset(); } catch (IOException e) { log.error(e.getMessage(), e); } } return deploymentConfig; }
From source file:com.metamx.druid.loading.S3ZippedSegmentGetter.java
@Override public File getSegmentFiles(Map<String, Object> loadSpec) throws StorageAdapterLoadingException { String s3Bucket = MapUtils.getString(loadSpec, "bucket"); String s3Path = MapUtils.getString(loadSpec, "key"); if (s3Path.startsWith("/")) { s3Path = s3Path.substring(1); }/*from w w w .j ava 2 s. c o m*/ log.info("Loading index at path[s3://%s/%s]", s3Bucket, s3Path); S3Object s3Obj = null; File tmpFile = null; try { if (!s3Client.isObjectInBucket(s3Bucket, s3Path)) { throw new StorageAdapterLoadingException("IndexFile[s3://%s/%s] does not exist.", s3Bucket, s3Path); } File cacheFile = new File(config.getCacheDirectory(), computeCacheFilePath(s3Bucket, s3Path)); if (cacheFile.exists()) { S3Object objDetails = s3Client.getObjectDetails(new S3Bucket(s3Bucket), s3Path); DateTime cacheFileLastModified = new DateTime(cacheFile.lastModified()); DateTime s3ObjLastModified = new DateTime(objDetails.getLastModifiedDate().getTime()); if (cacheFileLastModified.isAfter(s3ObjLastModified)) { log.info("Found cacheFile[%s] with modified[%s], which is after s3Obj[%s]. Using.", cacheFile, cacheFileLastModified, s3ObjLastModified); return cacheFile; } FileUtils.deleteDirectory(cacheFile); } long currTime = System.currentTimeMillis(); tmpFile = File.createTempFile(s3Bucket, new DateTime().toString()); log.info("Downloading file[s3://%s/%s] to local tmpFile[%s] for cacheFile[%s]", s3Bucket, s3Path, tmpFile, cacheFile); s3Obj = s3Client.getObject(new S3Bucket(s3Bucket), s3Path); StreamUtils.copyToFileAndClose(s3Obj.getDataInputStream(), tmpFile); final long downloadEndTime = System.currentTimeMillis(); log.info("Download of file[%s] completed in %,d millis", cacheFile, downloadEndTime - currTime); if (cacheFile.exists()) { FileUtils.deleteDirectory(cacheFile); } cacheFile.mkdirs(); ZipInputStream zipIn = null; OutputStream out = null; ZipEntry entry = null; try { zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(tmpFile))); while ((entry = zipIn.getNextEntry()) != null) { out = new FileOutputStream(new File(cacheFile, entry.getName())); IOUtils.copy(zipIn, out); zipIn.closeEntry(); Closeables.closeQuietly(out); out = null; } } finally { Closeables.closeQuietly(out); Closeables.closeQuietly(zipIn); } long endTime = System.currentTimeMillis(); log.info("Local processing of file[%s] done in %,d millis", cacheFile, endTime - downloadEndTime); log.info("Deleting tmpFile[%s]", tmpFile); tmpFile.delete(); return cacheFile; } catch (Exception e) { throw new StorageAdapterLoadingException(e, e.getMessage()); } finally { S3Utils.closeStreamsQuietly(s3Obj); if (tmpFile != null && tmpFile.exists()) { log.warn("Deleting tmpFile[%s] in finally block. Why?", tmpFile); tmpFile.delete(); } } }
From source file:com.metamx.druid.loading.S3ZippedSegmentPuller.java
@Override public File getSegmentFiles(DataSegment segment) throws StorageAdapterLoadingException { Map<String, Object> loadSpec = segment.getLoadSpec(); String s3Bucket = MapUtils.getString(loadSpec, "bucket"); String s3Path = MapUtils.getString(loadSpec, "key"); if (s3Path.startsWith("/")) { s3Path = s3Path.substring(1); }/*w ww. j a v a2s . co m*/ log.info("Loading index at path[s3://%s/%s]", s3Bucket, s3Path); S3Object s3Obj = null; File tmpFile = null; try { if (!s3Client.isObjectInBucket(s3Bucket, s3Path)) { throw new StorageAdapterLoadingException("IndexFile[s3://%s/%s] does not exist.", s3Bucket, s3Path); } File cacheFile = new File(config.getCacheDirectory(), computeCacheFilePath(s3Bucket, s3Path)); if (cacheFile.exists()) { S3Object objDetails = s3Client.getObjectDetails(new S3Bucket(s3Bucket), s3Path); DateTime cacheFileLastModified = new DateTime(cacheFile.lastModified()); DateTime s3ObjLastModified = new DateTime(objDetails.getLastModifiedDate().getTime()); if (cacheFileLastModified.isAfter(s3ObjLastModified)) { log.info("Found cacheFile[%s] with modified[%s], which is after s3Obj[%s]. Using.", cacheFile, cacheFileLastModified, s3ObjLastModified); return cacheFile; } FileUtils.deleteDirectory(cacheFile); } long currTime = System.currentTimeMillis(); tmpFile = File.createTempFile(s3Bucket, new DateTime().toString()); log.info("Downloading file[s3://%s/%s] to local tmpFile[%s] for cacheFile[%s]", s3Bucket, s3Path, tmpFile, cacheFile); s3Obj = s3Client.getObject(new S3Bucket(s3Bucket), s3Path); StreamUtils.copyToFileAndClose(s3Obj.getDataInputStream(), tmpFile); final long downloadEndTime = System.currentTimeMillis(); log.info("Download of file[%s] completed in %,d millis", cacheFile, downloadEndTime - currTime); if (cacheFile.exists()) { FileUtils.deleteDirectory(cacheFile); } cacheFile.mkdirs(); ZipInputStream zipIn = null; OutputStream out = null; ZipEntry entry = null; try { zipIn = new ZipInputStream(new BufferedInputStream(new FileInputStream(tmpFile))); while ((entry = zipIn.getNextEntry()) != null) { out = new FileOutputStream(new File(cacheFile, entry.getName())); IOUtils.copy(zipIn, out); zipIn.closeEntry(); Closeables.closeQuietly(out); out = null; } } finally { Closeables.closeQuietly(out); Closeables.closeQuietly(zipIn); } long endTime = System.currentTimeMillis(); log.info("Local processing of file[%s] done in %,d millis", cacheFile, endTime - downloadEndTime); log.info("Deleting tmpFile[%s]", tmpFile); tmpFile.delete(); return cacheFile; } catch (Exception e) { throw new StorageAdapterLoadingException(e, e.getMessage()); } finally { S3Utils.closeStreamsQuietly(s3Obj); if (tmpFile != null && tmpFile.exists()) { log.warn("Deleting tmpFile[%s] in finally block. Why?", tmpFile); tmpFile.delete(); } } }
From source file:com.android.build.gradle.internal.transforms.ExtractJarsTransform.java
private static void extractJar(@NonNull File outJarFolder, @NonNull File jarFile, boolean extractCode) throws IOException { mkdirs(outJarFolder);/*from w ww. ja v a 2s . c om*/ HashSet<String> lowerCaseNames = new HashSet<>(); boolean foundCaseInsensitiveIssue = false; try (Closer closer = Closer.create()) { FileInputStream fis = closer.register(new FileInputStream(jarFile)); ZipInputStream zis = closer.register(new ZipInputStream(fis)); // loop on the entries of the intermediary package and put them in the final package. ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { try { String name = entry.getName(); // do not take directories if (entry.isDirectory()) { continue; } foundCaseInsensitiveIssue = foundCaseInsensitiveIssue || !lowerCaseNames.add(name.toLowerCase(Locale.US)); Action action = getAction(name, extractCode); if (action == Action.COPY) { File outputFile = new File(outJarFolder, name.replace('/', File.separatorChar)); mkdirs(outputFile.getParentFile()); try (Closer closer2 = Closer.create()) { java.io.OutputStream outputStream = closer2 .register(new BufferedOutputStream(new FileOutputStream(outputFile))); ByteStreams.copy(zis, outputStream); outputStream.flush(); } } } finally { zis.closeEntry(); } } } if (foundCaseInsensitiveIssue) { LOGGER.error( "Jar '{}' contains multiple entries which will map to " + "the same file on case insensitive file systems.\n" + "This can be caused by obfuscation with useMixedCaseClassNames.\n" + "This build will be incorrect on case insensitive " + "file systems.", jarFile.getAbsolutePath()); } }
From source file:org.commonjava.aprox.subsys.git.GitManagerTest.java
private File unpackRepo(final String resource) throws Exception { final URL url = Thread.currentThread().getContextClassLoader().getResource(resource); final InputStream stream = url.openStream(); final ZipInputStream zstream = new ZipInputStream(stream); final File dir = temp.newFolder(); ZipEntry entry = null;//ww w. j a v a 2 s. c o m while ((entry = zstream.getNextEntry()) != null) { final File f = new File(dir, entry.getName()); if (entry.isDirectory()) { f.mkdirs(); } else { f.getParentFile().mkdirs(); final OutputStream out = new FileOutputStream(f); copy(zstream, out); closeQuietly(out); } zstream.closeEntry(); } closeQuietly(zstream); final File root = new File(dir, "test-aprox-data/.git"); assertThat(root.exists(), equalTo(true)); assertThat(root.isDirectory(), equalTo(true)); return root; }
From source file:org.xwiki.test.misc.HTMLExportTest.java
private void assertHTMLExportURL(String htmlExportURL) throws Exception { URL url = new URL(htmlExportURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream is = connection.getInputStream(); ZipInputStream zis = new ZipInputStream(is); boolean foundWebHome = false; boolean foundResourcesDirectory = false; boolean foundSkinsDirectory = false; boolean foundSkinCSS = false; boolean foundWebjars = false; // We must read the full stream as otherwise if we close it before we've fully read it // then the server side will get a broken pipe since it's still trying to send data on it. for (ZipEntry entry; (entry = zis.getNextEntry()) != null; zis.closeEntry()) { if (entry.getName().equals("xwiki.Main.WebHome.html")) { String content = IOUtils.toString(zis); // Verify that the content was rendered properly assertTrue("Should have contained 'Welcome to your wiki'", content.contains("Welcome to your wiki")); // Ensure that the translations have been rendered properly assertFalse("$services.localization.render should have been expanded", content.contains("$services.localization.render")); foundWebHome = true;/* ww w. j a v a 2s . c o m*/ } else if (entry.getName().endsWith(".vm")) { fail("There shouldn't be any *.vm files in the generated zip!"); } else if (entry.getName().endsWith(".less")) { fail("There shouldn't be any *.less files in the generated zip!"); } else if (entry.getName().equals("xwiki.properties")) { fail("There shouldn't be any xwiki.properties file in the generated zip!"); } else if (entry.getName().startsWith("resources/")) { foundResourcesDirectory = true; IOUtils.readLines(zis); } else if (entry.getName().startsWith("skins/")) { foundSkinsDirectory = true; // Verify that the skin is correctly going to be applied by verifying the flamingo/style.css file is // found // and is correctly referenced. This fixes http://jira.xwiki.org/browse/XWIKI-9145 if (entry.getName().equals("skins/flamingo/style.css")) { assertSkinIsActive(IOUtils.readLines(zis)); foundSkinCSS = true; } else { IOUtils.readLines(zis); } } else if (entry.getName().startsWith("webjars")) { // We verify here that webjars URLs have been properly exported foundWebjars = true; IOUtils.readLines(zis); } else { IOUtils.readLines(zis); } } assertTrue("Failed to find the xwiki.Main.WebHome.html entry", foundWebHome); assertTrue("Failed to find the resources/ directory entry", foundResourcesDirectory); assertTrue("Failed to find the skins/ directory entry", foundSkinsDirectory); assertTrue("Failed to find the link to colibri.css in style.css", foundSkinCSS); assertTrue("Failed to find webjar resources in the HTML export", foundWebjars); zis.close(); }
From source file:com.nextep.designer.repository.services.impl.RepositoryUpdaterService.java
/** * Creates the specified delivery (ZIP resource file) on the temporary directory of the local * file system.//from ww w. ja v a2 s . c o m * * @param deliveryResource java resource zip file * @return a <code>String</code> representing the absolute path to the delivery root directory. */ private static String createTempDelivery(String deliveryResource) { InputStream is = RepositoryUpdaterService.class.getResourceAsStream(deliveryResource); if (is == null) { throw new ErrorException("Unable to load delivery file: " + deliveryResource); //$NON-NLS-1$ } final String exportLoc = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$ ZipInputStream zipInput = new ZipInputStream(is); ZipEntry entry = null; String rootDeliveryDir = null; try { while ((entry = zipInput.getNextEntry()) != null) { File targetFile = new File(exportLoc, entry.getName()); if (rootDeliveryDir == null) { /* * Initialize the delivery root directory value by searching recursively for the * shallowest directory in the path. */ rootDeliveryDir = getDeliveryRootPath(targetFile, new File(exportLoc)); } if (entry.isDirectory()) { targetFile.mkdirs(); } else { File targetDir = targetFile.getParentFile(); if (!targetDir.exists()) { /* * Creates the directory including any necessary but nonexistent parent * directories. */ targetDir.mkdirs(); } FileOutputStream outFile = new FileOutputStream(targetFile); copyStreams(zipInput, outFile); outFile.close(); } zipInput.closeEntry(); } } catch (IOException e) { throw new ErrorException(e); } finally { try { zipInput.close(); } catch (IOException e) { throw new ErrorException(e); } } return rootDeliveryDir; }
From source file:org.wso2.carbon.automation.engine.frameworkutils.ArchiveExtractorUtil.java
public static void extractFile(String sourceFilePath, String extractedDir) throws Exception { FileOutputStream fileoutputstream = null; String fileDestination = extractedDir + File.separator; byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry;/*from w w w . j a v a2 s. c o m*/ try { zipinputstream = new ZipInputStream(new FileInputStream(sourceFilePath)); zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { //for each entry to be extracted String entryName = fileDestination + zipentry.getName(); entryName = entryName.replace('/', File.separatorChar); entryName = entryName.replace('\\', File.separatorChar); int n; File newFile = new File(entryName); if (zipentry.isDirectory()) { if (!newFile.exists()) { if (!newFile.mkdirs()) { throw new Exception("Error occurred created new directory"); } } zipentry = zipinputstream.getNextEntry(); continue; } else { File resourceFile = new File(entryName.substring(0, entryName.lastIndexOf(File.separator))); if (!resourceFile.exists()) { if (!resourceFile.mkdirs()) { break; } } } fileoutputstream = new FileOutputStream(entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (IOException e) { log.error("Error on archive extraction ", e); throw new IOException("Error on archive extraction ", e); } finally { if (fileoutputstream != null) { fileoutputstream.close(); } if (zipinputstream != null) { zipinputstream.close(); } } }
From source file:org.xwiki.flamingo.test.ui.HTMLExportTest.java
private void assertHTMLExportURL(String htmlExportURL, List<PageValidator> validators) throws Exception { URL url = new URL(htmlExportURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); InputStream is = connection.getInputStream(); ZipInputStream zis = new ZipInputStream(is); boolean foundResourcesDirectory = false; boolean foundSkinsDirectory = false; boolean foundSkinCSS = false; boolean foundWebjars = false; // We must read the full stream as otherwise if we close it before we've fully read it // then the server side will get a broken pipe since it's still trying to send data on it. for (ZipEntry entry; (entry = zis.getNextEntry()) != null; zis.closeEntry()) { for (PageValidator validator : validators) { validator.validate(zis, entry); }/*from w w w. jav a2 s .co m*/ if (entry.getName().endsWith(".vm")) { fail("There shouldn't be any *.vm files in the generated zip!"); } else if (entry.getName().endsWith(".less")) { fail("There shouldn't be any *.less files in the generated zip!"); } else if (entry.getName().equals("xwiki.properties")) { fail("There shouldn't be any xwiki.properties file in the generated zip!"); } else if (entry.getName().startsWith("resources/")) { foundResourcesDirectory = true; IOUtils.readLines(zis, Charset.defaultCharset()); } else if (entry.getName().startsWith("skins/")) { foundSkinsDirectory = true; // Verify that the skin is correctly going to be applied by verifying the flamingo/style.css file is // found and is correctly referenced. This fixes https://jira.xwiki.org/browse/XWIKI-9145 if (entry.getName().equals("skins/flamingo/style.css")) { assertSkinIsActive(IOUtils.readLines(zis, Charset.defaultCharset())); foundSkinCSS = true; } else { IOUtils.readLines(zis, Charset.defaultCharset()); } } else if (entry.getName().startsWith("webjars")) { // We verify here that webjars URLs have been properly exported foundWebjars = true; IOUtils.readLines(zis, Charset.defaultCharset()); } else { IOUtils.readLines(zis, Charset.defaultCharset()); } } for (PageValidator validator : validators) { validator.assertResult(); } assertTrue("Failed to find the resources/ directory entry", foundResourcesDirectory); assertTrue("Failed to find the skins/ directory entry", foundSkinsDirectory); assertTrue("Failed to find the link to colibri.css in style.css", foundSkinCSS); assertTrue("Failed to find webjar resources in the HTML export", foundWebjars); zis.close(); }
From source file:de.micromata.genome.db.jpa.genomecore.chronos.JobStoreTest.java
License:asdf
public void findClassPathInJars() { try {// w w w . j a va 2 s . c o m Iterator it = FileUtils.iterateFiles(new File("."), new String[] { "jar" }, true); for (; it.hasNext();) { File f = (File) it.next(); ZipInputStream zf = new ZipInputStream(new FileInputStream(f)); ZipEntry ze; while ((ze = zf.getNextEntry()) != null) { String name = ze.getName(); if (name.startsWith("org/objectweb/asm") == true) { System.out.println("Found: " + f.getCanonicalPath()); zf.closeEntry(); break; } zf.closeEntry(); } zf.close(); } } catch (Exception ex) { throw new RuntimeException(ex); } }