/** * Peek into the JAR data and make sure that it is valid JAR content. * * @param jarFile Jar containing data to be validated. * @return True if the data has JAR content, false otherwise */ public static boolean hasValidJarContent(File jarFile) { JarInputStream jarInputStream = null; boolean valid = false; try { jarInputStream = new JarInputStream(new FileInputStream(jarFile)); valid = jarInputStream.getNextJarEntry() != null; } catch (IOException ignore) { // Ignore this exception and just return false } finally { try { jarInputStream.close(); } catch (IOException ignored) { // Ignore this exception and just return result } } return valid; }
private PluginInfo readPluginInfoFromJar(File pluginJarFile) throws Exception { try (JarInputStream jarStream = new JarInputStream(new FileInputStream(pluginJarFile))) { Manifest jarManifest = jarStream.getManifest(); if (jarManifest == null) { throw new Exception("Given file is not a valid Syncany plugin file (not a JAR file, or no manifest)."); } String pluginId = jarManifest.getMainAttributes().getValue("Plugin-Id"); if (pluginId == null) { throw new Exception("Given file is not a valid Syncany plugin file (no plugin ID in manifest)."); } PluginInfo pluginInfo = new PluginInfo(); pluginInfo.setPluginId(pluginId); pluginInfo.setPluginName(jarManifest.getMainAttributes().getValue("Plugin-Name")); pluginInfo.setPluginVersion(jarManifest.getMainAttributes().getValue("Plugin-Version")); pluginInfo.setPluginDate(jarManifest.getMainAttributes().getValue("Plugin-Date")); pluginInfo.setPluginAppMinVersion(jarManifest.getMainAttributes().getValue("Plugin-App-Min-Version")); pluginInfo.setPluginRelease(Boolean.parseBoolean(jarManifest.getMainAttributes().getValue("Plugin-Release"))); if (jarManifest.getMainAttributes().getValue("Plugin-Conflicts-With") != null) { pluginInfo.setConflictingPluginIds(Arrays.asList(jarManifest.getMainAttributes().getValue("Plugin-Conflicts-With"))); } return pluginInfo; } }
JarInputStream inputStream = new JarInputStream(new FileInputStream(sourceJar)); try { if (!targetJar.isFile() && !targetJar.createNewFile()) { throw new IllegalArgumentException("Could not create file: " + targetJar); Manifest manifest = inputStream.getManifest(); JarOutputStream outputStream = manifest == null ? new JarOutputStream(new FileOutputStream(targetJar)) : new JarOutputStream(new FileOutputStream(targetJar), manifest); try { Map<TypeDescription, byte[]> rawAuxiliaryTypes = getAuxiliaryTypes(); while ((jarEntry = inputStream.getNextJarEntry()) != null) { byte[] replacement = files.remove(jarEntry.getName()); if (replacement == null) { outputStream.putNextEntry(jarEntry); byte[] buffer = new byte[BUFFER_SIZE]; int index; while ((index = inputStream.read(buffer)) != END_OF_FILE) { outputStream.write(buffer, FROM_BEGINNING, index); outputStream.putNextEntry(new JarEntry(jarEntry.getName())); outputStream.write(replacement); inputStream.closeEntry(); outputStream.closeEntry(); inputStream.close();
JarInputStream jarStream = new JarInputStream(stream); Manifest mf = jarStream.getManifest();
private void explodeNatives() throws IOException { URL urlOfJar = getJarURL(); LOGGER.info("[TFS SDK] Exploding natives from {} to folder {}", urlOfJar.toString(), tempFolder.getAbsolutePath()); JarInputStream jarStream = new JarInputStream(urlOfJar.openStream()); JarEntry entry; while ((entry = jarStream.getNextJarEntry()) != null) { if (!entry.isDirectory() && entry.getName().startsWith("tfssdk/native/")) { File newFile = new File(tempFolder, entry.getName()); newFile.getParentFile().mkdirs(); LOGGER.info("[TFS SDK] Extract {} -> {}", entry.getName(), newFile); try (OutputStream fos = new FileOutputStream(newFile)) { IOUtils.copy(jarStream, fos); } } } }
private List<String> jar(URL location) throws IOException { String jarPath = location.getFile(); if (jarPath.indexOf("!") > -1){ jarPath = jarPath.substring(0, jarPath.indexOf("!")); } URL url = new URL(jarPath); InputStream in = url.openStream(); try { JarInputStream jarStream = new JarInputStream(in); return jar(jarStream); } finally { in.close(); } }
is = jarUrl.openStream(); if (log.isDebugEnabled()) { log.debug("Listing " + url); resources = listResources(new JarInputStream(is), path); is = url.openStream(); JarInputStream jarInput = new JarInputStream(is); if (log.isDebugEnabled()) { log.debug("Listing " + url); for (JarEntry entry; (entry = jarInput.getNextJarEntry()) != null;) { if (log.isDebugEnabled()) { log.debug("Jar entry: " + entry.getName()); children.add(entry.getName()); jarInput.close(); is = url.openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); List<String> lines = new ArrayList<String>(); if (is != null) { try { is.close(); } catch (Exception e) {
LOG.debug("Extract "+this+" to "+directory); throw new IOException("Not a valid jar url: "+urlString); URLConnection c = jarFileURL.openConnection(); c.setUseCaches(false); try (InputStream is = c.getInputStream(); JarInputStream jin = new JarInputStream(is)) while((entry=jin.getNextJarEntry())!=null) String entryName = entry.getName(); if ((subEntryName != null) && (entryName.startsWith(subEntryName))) if (entry.isDirectory()) try (OutputStream fout = new FileOutputStream(file)) if (entry.getTime()>=0) file.setLastModified(entry.getTime()); Manifest manifest = jin.getManifest(); if (manifest != null) manifest.write(fout);
if (file.getName().endsWith(".jar")) JarInputStream stream = new JarInputStream(new FileInputStream(file)); processJar(stream, files, templateDirectory); stream.close(); if (filePath.contains(templateDirectory) && !rootPath.equals(filePath) && isTemplateFile(filePath)) files.add(new URL("file:" + filePath));
private List<String> resolveAppContextLocationInJar(URL resource) throws IOException { String externalForm = resource.toExternalForm(); try { URL jarUrl = new URL("jar:" + externalForm + "!/META-INF/MANIFEST.MF"); InputStream manifestStream = jarUrl.openStream(); Manifest jarManifest = new Manifest(manifestStream); String relativeLocation = jarManifest.getMainAttributes().getValue("Spring-Context"); if (relativeLocation != null) { return Collections.singletonList("!/" + relativeLocation); } } catch (IOException e) { // ignore, no manifest } JarInputStream stream = new JarInputStream(resource.openStream()); JarEntry entry; while ((entry = stream.getNextJarEntry()) != null) { if (entry.getName().contains("/spring/") && entry.getName().endsWith(".xml")) { return Collections.singletonList("!/" + entry.getName()); } } return null; }
private List<String> jar(URL location) throws IOException { URL url = fileManager.normalizeToFileProtocol(location); if (url != null) { InputStream in = url.openStream(); try { JarInputStream jarStream = new JarInputStream(in); return jar(jarStream); } finally { in.close(); } } else { LOG.debug("Unable to read [{}]", location.toExternalForm()); } return Collections.emptyList(); }
@Override public InputStream getInputStream() throws IOException { final URL embulkPluginJarUrl = getURL(); if (!embulkPluginJarUrl.getProtocol().equals(protocol)) { return null; final String[] embulkPluginJarUrlSeparate = embulkPluginJarUrl.getPath().split("!!/"); if (embulkPluginJarUrlSeparate.length != 2) { return null; final URL embeddedJarUrl = new URL(embulkPluginJarUrlSeparate[0]); final String embeddedResourceName = embulkPluginJarUrlSeparate[1]; new JarInputStream(embeddedJarUrlConnection.getInputStream()); JarEntry jarEntry = embeddedJarInputStream.getNextJarEntry(); while (jarEntry != null) { if (jarEntry.getName().equals(embeddedResourceName)) { return embeddedJarInputStream; // The InputStream points the specific "JAR entry". jarEntry = embeddedJarInputStream.getNextJarEntry();
File f = new File(url.getPath()); jis = new JarInputStream(url.openStream()); mf = jis.getManifest(); } catch (IOException ioe) { throw new PluginException("Failed loading manifest for plugin jar: " + url, ioe); FileInputStream is = null; try { is = new FileInputStream(manifest); mf = new Manifest(is); } catch (IOException e) { throw new PluginException("Failed loading manifest for plugin jar: " + url, e);
@Test public void testExistingManifest() throws Exception { File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), TestJarFinder.class.getName() + "-testExistingManifest"); delete(dir); dir.mkdirs(); File metaInfDir = new File(dir, "META-INF"); metaInfDir.mkdirs(); File manifestFile = new File(metaInfDir, "MANIFEST.MF"); Manifest manifest = new Manifest(); OutputStream os = new FileOutputStream(manifestFile); manifest.write(os); os.close(); File propsFile = new File(dir, "props.properties"); Writer writer = new FileWriter(propsFile); new Properties().store(writer, ""); writer.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream zos = new JarOutputStream(baos); JarFinder.jarDir(dir, "", zos); JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.toByteArray())); Assert.assertNotNull(jis.getManifest()); jis.close(); }
while (enumeration.hasMoreElements()) { final URL url = enumeration.nextElement(); final String path = url.getPath(); while (jars.hasMoreElements()) { final URL jar = jars.nextElement(); final InputStream inputStream = classLoader.getResourceAsStream(jar.getPath()); if (inputStream == null) { LOGGER.config(LocalizationMessages.OSGI_REGISTRY_ERROR_OPENING_RESOURCE_STREAM(jar)); jarInputStream = new JarInputStream(inputStream); } catch (final IOException ex) { LOGGER.log(Level.CONFIG, LocalizationMessages.OSGI_REGISTRY_ERROR_PROCESSING_RESOURCE_STREAM(jar), ex); try { inputStream.close(); } catch (final IOException e) { while ((jarEntry = jarInputStream.getNextJarEntry()) != null) { final String jarEntryName = jarEntry.getName(); final String jarEntryNameLeadingSlash = jarEntryName.startsWith("/") ? jarEntryName : "/" + jarEntryName; } finally { try { jarInputStream.close(); } catch (final IOException e) {
while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); files.add(entry.getName()); // add to installed filelist; rely on uninstaller retaining shared files File newFile = new File(maryBase + "/" + entry.getName()); if (entry.isDirectory()) { System.err.println("Extracting directory: " + entry.getName()); newFile.mkdir(); String existingVersion = existingManifest.getMainAttributes().getValue("Specification-Version"); JarInputStream packagedJar = new JarInputStream(zipfile.getInputStream(entry)); Manifest packagedManifest = packagedJar.getManifest(); String packagedVersion = packagedManifest.getMainAttributes().getValue("Specification-Version"); System.err.println("Extracting file: " + entry.getName()); copyInputStream(zipfile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(newFile))); if (entry.getName().startsWith("bin/")) {
/** * Locate all Webjars Maven pom.properties files on the classpath. * * @return a list of Maven pom.properties files on the classpath */ private List<String> locatePomProperties() { List<String> propertiesFiles = new ArrayList<>(); List<URL> packageUrls = ClasspathUtils.getResources(getResourceBasePath()); for (URL packageUrl : packageUrls) { if (packageUrl.getProtocol().equals("jar")) { // We only care about Webjars jar files log.debug("Scanning {}", packageUrl); try { String jar = packageUrl.toString().substring("jar:".length()).split("!")[0]; File file = new File(new URI(jar)); try (JarInputStream is = new JarInputStream(new FileInputStream(file))) { JarEntry entry = null; while ((entry = is.getNextJarEntry()) != null) { if (!entry.isDirectory() && entry.getName().endsWith("pom.properties")) { propertiesFiles.add(entry.getName()); } } } } catch (URISyntaxException | IOException e) { throw new PippoRuntimeException("Failed to get classes for package '{}'", packageUrl); } } } return propertiesFiles; }
private void unpackMainClassAndFilter(URL coredep) { try (JarInputStream in = new JarInputStream(coredep.openStream())){ while (true) { JarEntry entry = in.getNextJarEntry(); if (entry == null) { break; } if (entry.getName().startsWith(MAIN_CLASS) && entry.getName().endsWith(".class")) { File file = new File(workingDirectory, entry.getName()); file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); IOUtils.copy(in, out); out.close(); } } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public InputStream openResource(String name) { try { JarInputStream input = new JarInputStream(new FileInputStream(file)); while (true) { ZipEntry entry = input.getNextEntry(); if (entry == null) { input.close(); throw new IllegalArgumentException("Resource not found: " + name); } if (entry.getName().equals(name)) { return input; } if (name.startsWith("/") && entry.getName().equals(name.substring(1))) { return input; } } } catch (FileNotFoundException e) { throw new IllegalStateException("The underlying file does not exist"); } catch (IOException e) { throw new IllegalStateException("Error reading jar file"); } } }
private static void recurseURL(URL url, List<URL> files, String templateDirectory) throws URISyntaxException, IOException { if ("file".equals(url.getProtocol())) { recurse(new File(url.toURI()), new File(url.toURI()), files, templateDirectory); } else { if (url.getPath().endsWith(".jar")) { JarInputStream stream = new JarInputStream(url.openStream()); processJar(stream, files, templateDirectory); stream.close(); } } }