/** * {@inheritDoc} */ public Sink write(Manifest manifest) throws IOException { return manifest == null ? new Sink.ForJarOutputStream(new JarOutputStream(new FileOutputStream(file))) : new Sink.ForJarOutputStream(new JarOutputStream(new FileOutputStream(file), manifest)); } }
/** * {@inheritDoc} */ public File toJar(File file, Manifest manifest) throws IOException { if (!file.isFile() && !file.createNewFile()) { throw new IllegalArgumentException("Could not create file: " + file); } JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(file), manifest); try { for (Map.Entry<TypeDescription, byte[]> entry : getAuxiliaryTypes().entrySet()) { outputStream.putNextEntry(new JarEntry(entry.getKey().getInternalName() + CLASS_FILE_EXTENSION)); outputStream.write(entry.getValue()); outputStream.closeEntry(); } outputStream.putNextEntry(new JarEntry(typeDescription.getInternalName() + CLASS_FILE_EXTENSION)); outputStream.write(binaryRepresentation); outputStream.closeEntry(); } finally { outputStream.close(); } return file; }
/** * Jars a given directory or single file into a JarOutputStream. */ public void jarDir(File dirOrFile2Jar, File destJar) throws IOException { if (dirOrFile2Jar == null || destJar == null) { throw new IllegalArgumentException(); } mDestJarName = destJar.getCanonicalPath(); FileOutputStream fout = new FileOutputStream(destJar); JarOutputStream jout = new JarOutputStream(fout); //jout.setLevel(0); try { jarDir(dirOrFile2Jar, jout, null); } catch (IOException ioe) { throw ioe; } finally { jout.close(); fout.close(); } }
@Override public void createArchive(String path, String archiveName, Manifest manifest) { File file = new File(getAbsolutePath(path), archiveName); try { if (!(file.createNewFile() || file.isFile())) { throw new IOException("Cannot create file " + file); } FileOutputStream fileStream = new FileOutputStream(file); ZipOutputStream zipStream = manifest != null ? new JarOutputStream(fileStream, manifest) : new ZipOutputStream(fileStream); mapArchiveStreams.put(file.getPath(), zipStream); } catch (IOException ex) { DecompilerContext.getLogger().writeMessage("Cannot create archive " + file, ex); } }
JarURLConnection connection = (JarURLConnection) resource.openConnection(); URL url = connection.getJarFileURL(); return new File(url.toURI()); JarOutputStream jar = new JarOutputStream(output)) { Path resourceRoot = Paths.get(uri).getParent();
/** * The minimum requirement to have a "valid" archive plugin is to include * findbugs.xml, messages.xml and MANIFEST.MF files. The rest of the * resources are load using the parent ClassLoader (Not requires to be in * the jar). * <p> * Instead of building a file on disk, the result of the stream is kept in * memory and return as a byte array. * * @return * @throws IOException * @throws URISyntaxException */ private byte[] buildFakePluginJar() throws IOException, URISyntaxException { ClassLoader cl = getClass().getClassLoader(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); JarOutputStream jar = new JarOutputStream(buffer); final URL metadata = cl.getResource("metadata"); if (metadata != null) { final File dir = new File(metadata.toURI()); //Add files to the jar stream addFilesToStream(cl, jar, dir, ""); } jar.finish(); jar.close(); return buffer.toByteArray(); }
private JarOutputStream createJarWithConfigXmlEntry(File file) throws IOException { JarOutputStream jar = new JarOutputStream(new FileOutputStream(file)); ZipEntry config = new ZipEntry("config.xml"); jar.putNextEntry(config); return jar; }
private static Optional<Path> getOverrideClasspath(@Nullable List<Class<?>> overrideClasspath) { if (overrideClasspath == null) { return Optional.empty(); } try { Path tempJarFile = Files.createTempFile(/* prefix = */ null, /* suffix = */ ".jar"); try (OutputStream os = Files.newOutputStream(tempJarFile); JarOutputStream jos = new JarOutputStream(os)) { for (Class<?> clazz : overrideClasspath) { String entryPath = clazz.getName().replace('.', '/') + ".class"; jos.putNextEntry(new JarEntry(entryPath)); try (InputStream is = clazz.getClassLoader().getResourceAsStream(entryPath)) { ByteStreams.copy(is, jos); } } } return Optional.of(tempJarFile); } catch (IOException e) { throw new UncheckedIOException(e); } }
JarOutputStream jarStream = new JarOutputStream(fileStream); InputStream classStream = Files.newInputStream(fullClassFile)) { ZipEntry entry = new ZipEntry(classFile.toString()); jarStream.putNextEntry(entry);
/** * {@inheritDoc} */ public Map<String, Class<?>> injectRaw(Map<? extends String, byte[]> types) { File jarFile = new File(folder, JAR + randomString.nextString() + "." + JAR); try { if (!jarFile.createNewFile()) { throw new IllegalStateException("Cannot create file " + jarFile); } JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(jarFile)); try { for (Map.Entry<? extends String, byte[]> entry : types.entrySet()) { jarOutputStream.putNextEntry(new JarEntry(entry.getKey().replace('.', '/') + CLASS_FILE_EXTENSION)); jarOutputStream.write(entry.getValue()); } } finally { jarOutputStream.close(); } target.inject(instrumentation, new JarFile(jarFile)); Map<String, Class<?>> result = new HashMap<String, Class<?>>(); for (String name : types.keySet()) { result.put(name, Class.forName(name, false, ClassLoader.getSystemClassLoader())); } return result; } catch (IOException exception) { throw new IllegalStateException("Cannot write jar file to disk", exception); } catch (ClassNotFoundException exception) { throw new IllegalStateException("Cannot load injected class", exception); } }
@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(); }
try (JarOutputStream jarStream = new JarOutputStream( Files.newOutputStream(jarfile.toPath()), manifest)) { jarStream.setMethod(JarOutputStream.DEFLATED); entryFile = new File(config.srcDir, entryName);
private static void createJar(File dir, File jarFile) throws IOException { Preconditions.checkNotNull(dir, "dir"); Preconditions.checkNotNull(jarFile, "jarFile"); File jarDir = jarFile.getParentFile(); if (!jarDir.exists()) { if (!jarDir.mkdirs()) { throw new IOException(MessageFormat.format("could not create dir [{0}]", jarDir)); } } JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile)); jarDir(dir, StringUtils.EMPTY, zos); } }
try (FileOutputStream outputStream = new FileOutputStream(jarFile)) { JarOutputStream jarOutputStream = manifest != null ? new JarOutputStream(outputStream, manifest) : new JarOutputStream(outputStream); jarOutputStream.close();
private JarOutputStream createJarWithConfigXmlEntry(File file) throws IOException { JarOutputStream jar = new JarOutputStream(new FileOutputStream(file)); ZipEntry config = new ZipEntry("config.xml"); jar.putNextEntry(config); return jar; }
JAR_FILE = JAR_DIR.resolve(JAR_FILE_NAME); try (JarOutputStream jarOs = new JarOutputStream(Files.newOutputStream(JAR_FILE))) { ClassLoader loader = ForkServer.class.getClassLoader(); for (Class<?> klass : getClasses("org.apache.tika")) { String path = klass.getName().replace('.', '/') + ".class"; try (InputStream input = loader.getResourceAsStream(path)) { jarOs.putNextEntry(new JarEntry(path)); IOUtils.copy(input, jarOs); jarOs.putNextEntry(new JarEntry("org/apache/tika/parser/TIKA-2653-vowel-parser-ae.xml")); IOUtils.copy(input, jarOs); jarOs.putNextEntry(new JarEntry("org/apache/tika/mime/tika-mimetypes.xml")); IOUtils.copy(input, jarOs);
private void packageTopology(Path jar, AbstractStreamsProcessor processor) throws IOException { Manifest manifest = new Manifest(); Attributes attr = manifest.getMainAttributes(); attr.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attr.put(Attributes.Name.MAIN_CLASS, processor.getClass().getCanonicalName()); try (JarOutputStream out = new JarOutputStream( new BufferedOutputStream(new FileOutputStream(jar.toFile())), manifest)) { List<CompilingClassLoader> classLoaders = processor.getClassLoaders(); if (classLoaders != null && !classLoaders.isEmpty()) { for (CompilingClassLoader classLoader : classLoaders) { for (Map.Entry<String, ByteArrayOutputStream> e : classLoader.getClasses().entrySet()) { out.putNextEntry(new ZipEntry(e.getKey().replace(".", "/") + ".class")); out.write(e.getValue().toByteArray()); out.closeEntry(); } } } } } }
public static File createClasspathJarFile(String classpath) throws IOException { final Manifest manifest = new Manifest(); final Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); String classpathForManifest = ""; int idx = 0; int endIdx = 0; while (endIdx >= 0) { endIdx = classpath.indexOf(File.pathSeparator, idx); String path = endIdx < 0 ? classpath.substring(idx) : classpath.substring(idx, endIdx); if (classpathForManifest.length() > 0) { classpathForManifest += " "; } classpathForManifest += new File(path).toURI().toURL().toString(); idx = endIdx + File.pathSeparator.length(); } attributes.put(Attributes.Name.CLASS_PATH, classpathForManifest); File jarFile = File.createTempFile("classpath", ".jar"); try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jarFile)); ZipOutputStream jarPlugin = new JarOutputStream(out, manifest); ) { jarFile.deleteOnExit(); } return jarFile; }
@Test public void testNoManifest() throws Exception { File dir = new File(System.getProperty("test.build.dir", "target/test-dir"), TestJarFinder.class.getName() + "-testNoManifest"); delete(dir); dir.mkdirs(); 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(); } }
private static Dex loadFromClassFile(File file) throws IOException, DecodeException { File outFile = FileUtils.createTempFile("cls.jar"); try (JarOutputStream jo = new JarOutputStream(new FileOutputStream(outFile))) { String clsName = AsmUtils.getNameFromClassFile(file); if (clsName == null || !ZipSecurity.isValidZipEntryName(clsName)) { throw new IOException("Can't read class name from file: " + file); } FileUtils.addFileToJar(jo, file, clsName + ".class"); } return loadFromJar(outFile); }