JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile)); for (File f : files) { String name = f.getPath(); ZipEntry e = new ZipEntry(name.replace(File.separatorChar, '/')); e.setTime(f.lastModified()); jos.putNextEntry(e); FileInputStream fis = new FileInputStream(f); byte[] buffer = new byte[64 * 1024]; int length; while ((length = fis.read(buffer)) != -1) { jos.write(buffer, 0, length); fis.close(); jos.closeEntry(); jos.close();
String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP); if (path != null) { JarEntry je = new JarEntry(subPath); je.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(je); jos.flush(); jos.closeEntry(); System.out.println("adding " + dirOrFile2jar.getPath()); FileInputStream fis = new FileInputStream(dirOrFile2jar); try { JarEntry entry = new JarEntry(path + dirOrFile2jar.getName()); entry.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(entry); while ((mByteCount = fis.read(mBuffer)) != -1) { jos.write(mBuffer, 0, mByteCount); if (mVerbose) { System.out.println("wrote " + mByteCount + " bytes"); jos.flush(); jos.closeEntry(); } catch (IOException ioe) { throw ioe; } finally { fis.close();
public static void addFileToJar(JarOutputStream jar, File source, String entryName) throws IOException { try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source))) { JarEntry entry = new JarEntry(entryName); entry.setTime(source.lastModified()); jar.putNextEntry(entry); copyStream(in, jar); jar.closeEntry(); } }
@Override public void modifyOutputStream(JarOutputStream jarOut) throws IOException { for (String key : this.entries.keySet()) { jarOut.putNextEntry(new JarEntry(key)); jarOut.write(this.entries.get(key).getBytes()); } } }
public void testJar(JarOutputStream jos) throws Exception { JarEntry je = new JarEntry("foo"); jos.putNextEntry(je); jos.closeEntry(); } }
Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); FileOutputStream fos = new FileOutputStream(path); JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest); jarOutputStream.putNextEntry(new JarEntry(pathInJar)); jarOutputStream.closeEntry(); jarOutputStream.putNextEntry( new JarEntry(fileAndPath.path + file.getName())); byte[] allBytes = new byte[(int)file.length()]; FileInputStream fis = new FileInputStream(file); fis.read(allBytes); fis.close(); jarOutputStream.write(allBytes); jarOutputStream.closeEntry(); jarOutputStream.close(); fos.close();
FileOutputStream stream = new FileOutputStream(targetJar); JarOutputStream out = new JarOutputStream(stream, new Manifest()); byte buffer[] = new byte[BUFFER_SIZE]; JarEntry jarAdd = new JarEntry(libPrefix + jarFile.getName()); jarAdd.setTime(jarFile.lastModified()); out.putNextEntry(jarAdd); FileInputStream in = new FileInputStream(jarFile); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) break; out.write(buffer, 0, nRead); in.close(); out.close(); stream.close(); LOG.info("Adding jar file to outer jar file completed");
while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry)entries.nextElement(); if (entry.getName().contains("..")) { System.err.println("ERROR: Refusing to extract a zip entry with '..' in it [" + entry.getName() + "]"); return false; if (entry.getName().indexOf(0) >= 0) { System.err.println("ERROR: Refusing to extract a zip entry with null in it [" + entry.getName() + "]"); return false; if (entry.getName().endsWith(".jar.pack") || entry.getName().endsWith(".war.pack")) { target = new File(targetDir, entry.getName().substring(0, entry.getName().length() - ".pack".length())); jos = new JarOutputStream(new FileOutputStream(target)); unpack(in, jos); if (logLevel <= Log.INFO) System.err.println("INFO: File [" + entry.getName() + "] extracted and unpacked"); } else { fos = new FileOutputStream(target); while ((read = in.read(buf)) != -1) { fos.write(buf, 0, read); try { if (in != null) in.close(); } catch (IOException ioe) {} try { if (fos != null) fos.close(); } catch (IOException ioe) {} try { if (jos != null) jos.close(); } catch (IOException ioe) {}
public void groupFile(File file) throws FileNotFoundException, IOException{ //StringTokenizer tokenizer = new StringTokenizer(Files.getName(file), "_"); if (file.exists() && file.isFile()){ String name = Files.getName(file); Matcher matcher = pattern.matcher(name); String localeString; if (matcher.find()){ localeString = matcher.group(); localeString = localeString.replaceFirst("_", ""); localeString = localeString.replaceAll("_", "-"); } else { localeString = DEFAULT; } if (! jars.containsKey(localeString)){ jars.put(localeString, new JarOutputStream(new BufferedOutputStream( new FileOutputStream(new File(getTargetDir(), getJarName()+"-"+localeString+".jar"))), new Manifest())); } JarOutputStream jos = jars.get(localeString); InputStream is = new BufferedInputStream(new FileInputStream(file)); byte[] b = new byte[(int) file.length()]; is.read(b); jos.putNextEntry(new JarEntry(file.getPath())); jos.write(b); } }
public static void putNextJarEntry(JarOutputStream modelStream, String name, File file) throws IOException { JarEntry entry = new JarEntry(name); entry.setSize(file.length()); modelStream.putNextEntry(entry); InputStream fileStream = new BufferedInputStream(new FileInputStream(file)); IOUtils.copy(fileStream, modelStream); fileStream.close(); }
private void createJar(String className, OutputStream outStream, byte[] bytes) throws IOException { JarOutputStream jarOutputStream = new JarOutputStream(outStream); // Add the class file to the JAR file String formattedName = className; if (!formattedName.endsWith(".class")) { formattedName = formattedName.concat(".class"); } JarEntry entry = new JarEntry(formattedName); entry.setTime(System.currentTimeMillis()); jarOutputStream.putNextEntry(entry); jarOutputStream.write(bytes); jarOutputStream.closeEntry(); jarOutputStream.close(); }
? new JarOutputStream(new FileOutputStream(targetJar)) : new JarOutputStream(new FileOutputStream(targetJar), manifest); try { Map<TypeDescription, byte[]> rawAuxiliaryTypes = getAuxiliaryTypes(); JarEntry jarEntry; 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); outputStream.closeEntry(); outputStream.putNextEntry(new JarEntry(entry.getKey())); outputStream.write(entry.getValue()); outputStream.closeEntry(); outputStream.close();
Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest); add(new File("inputDirectory"), target); target.close(); JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); JarEntry entry = new JarEntry(source.getPath().replace("\\", "/")); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source));
.collect(Collectors.toMap(je -> je.getName().replace('\\', '/'), je -> je, (a, b) -> b)); JarEntry wlEntry = wlEntries.get(filepath); if (genericEntry.getCrc() != wlEntry.getCrc() || genericEntry.getSize() != wlEntry.getSize()) { newJarStream = new JarOutputStream(Files.newOutputStream(newWLJarFile.toPath())); newJarStream.setLevel(0); if (je.getCompressedSize() == -1 || je.getCompressedSize() == je.getSize()) { newJarStream.setLevel(0); } else { newJarStream.setLevel(JAR_COMPRESS_LEVEL); newJarStream.putNextEntry(new JarEntry(je.getName())); while ((bytesRead = is.read(buffer)) != -1) { newJarStream.write(buffer, 0, bytesRead); is.close();
private static Set<String> buildJar(ClassManager classManager, ClassBuilder builder, String outPath) throws IOException { Set<String> classNames = classManager.getFrameworkClassNames(); JarOutputStream out = new JarOutputStream(new FileOutputStream(outPath)); for (String className : classNames) { VirtualClass virtualClass = classManager.getVirtualClass(className); ClassDef classDef = virtualClass.getClassDef(); byte[] b = builder.build(classDef); String entryName = className.substring(1, className.length() - 1); out.putNextEntry(new ZipEntry(entryName + ".class")); out.write(b); out.closeEntry(); } out.close(); return classNames; }
private static File createJarFile(File jar) throws IOException, CannotCompileException, NotFoundException { Manifest manifest = new Manifest(); Attributes attrs = manifest.getMainAttributes(); attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); attrs.put(new Attributes.Name("Premain-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Agent-Class"), HotSwapAgent.class.getName()); attrs.put(new Attributes.Name("Can-Retransform-Classes"), "true"); attrs.put(new Attributes.Name("Can-Redefine-Classes"), "true"); JarOutputStream jos = null; try { jos = new JarOutputStream(new FileOutputStream(jar), manifest); String cname = HotSwapAgent.class.getName(); JarEntry e = new JarEntry(cname.replace('.', '/') + ".class"); jos.putNextEntry(e); ClassPool pool = ClassPool.getDefault(); CtClass clazz = pool.get(cname); jos.write(clazz.toBytecode()); jos.closeEntry(); } finally { if (jos != null) jos.close(); } return jar; } }
JarOutputStream jout = new JarOutputStream(new FileOutputStream(targetJar)); ArrayList done = new ArrayList(); for (int i = 0; i < jars.length; i++) { log.debug("buildJar: adding:\n%s", jars[i]); BufferedInputStream bin = new BufferedInputStream(new FileInputStream(jars[i])); ZipInputStream zin = new ZipInputStream(bin); for (ZipEntry zipentry = zin.getNextEntry(); zipentry != null; zipentry = zin.getNextEntry()) { if (filter == null || filter.accept(zipentry, jars[i])) { if (!done.contains(zipentry.getName())) { jout.putNextEntry(zipentry); if (!zipentry.isDirectory()) { bufferedWrite(zin, jout); done.add(zipentry.getName()); log.trace("adding: %s", zipentry.getName()); jout.close(); } catch (Exception ex) { log.error("buildJar: %s", ex);
manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_TITLE, Information.NAME + " Compiler"); manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VENDOR, Information.NAME); manifest.getMainAttributes().put(Attributes.Name.IMPLEMENTATION_VERSION, Information.CORE_VERSION); manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClassName); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); try (FileOutputStream outputStream = new FileOutputStream(jarFile)) { JarOutputStream jarOutputStream = manifest != null ? new JarOutputStream(outputStream, manifest) : new JarOutputStream(outputStream); jarOutputStream.close();
@Override public void transform(InputStream input, OutputStream output) throws IOException { String extraJars = System.getenv().get(EXTRA_JARS_ENV); if(extraJars == null || extraJars.length() == 0) { underlyingTransformer.transform(input, output); return; } File tmpFile = File.createTempFile("metron", "jar"); tmpFile.deleteOnExit(); Set<String> entries = new HashSet<>(); try (JarOutputStream jout = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tmpFile)))) { try (JarInputStream jin = new JarInputStream(new BufferedInputStream(input))){ copy(jin, jout, entries); } for (String fileStr : Splitter.on(",").split(extraJars)) { File f = new File(fileStr); if (!f.exists()) { continue; } LOG.info("Merging jar {} from {}", f.getName(), f.getAbsolutePath()); try (JarInputStream jin = new JarInputStream(new BufferedInputStream(new FileInputStream(f)))) { copy(jin, jout, entries); } } } underlyingTransformer.transform(new BufferedInputStream(new FileInputStream(tmpFile)), output); }
/** * 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(); } }