List of usage examples for java.util.jar JarOutputStream closeEntry
public void closeEntry() throws IOException
From source file:com.netflix.nicobar.core.module.ScriptModuleUtils.java
/** * Convert a ScriptModule to its compiled equivalent ScriptArchive. * <p>// w w w .j a va 2 s . c om * A jar script archive is created containing compiled bytecode * from a script module, as well as resources and other metadata from * the source script archive. * <p> * This involves serializing the class bytes of all the loaded classes in * the script module, as well as copying over all entries in the original * script archive, minus any that have excluded extensions. The module spec * of the source script archive is transferred as is to the target bytecode * archive. * * @param module the input script module containing loaded classes * @param jarPath the path to a destination JarScriptArchive. * @param excludeExtensions a set of extensions with which * source script archive entries can be excluded. * * @throws Exception */ public static void toCompiledScriptArchive(ScriptModule module, Path jarPath, Set<String> excludeExtensions) throws Exception { ScriptArchive sourceArchive = module.getSourceArchive(); JarOutputStream jarStream = new JarOutputStream(new FileOutputStream(jarPath.toFile())); try { // First copy all resources (excluding those with excluded extensions) // from the source script archive, into the target script archive for (String archiveEntry : sourceArchive.getArchiveEntryNames()) { URL entryUrl = sourceArchive.getEntry(archiveEntry); boolean skip = false; for (String extension : excludeExtensions) { if (entryUrl.toString().endsWith(extension)) { skip = true; break; } } if (skip) continue; InputStream entryStream = entryUrl.openStream(); byte[] entryBytes = IOUtils.toByteArray(entryStream); entryStream.close(); jarStream.putNextEntry(new ZipEntry(archiveEntry)); jarStream.write(entryBytes); jarStream.closeEntry(); } // Now copy all compiled / loaded classes from the script module. Set<Class<?>> loadedClasses = module.getModuleClassLoader().getLoadedClasses(); Iterator<Class<?>> iterator = loadedClasses.iterator(); while (iterator.hasNext()) { Class<?> clazz = iterator.next(); String classPath = clazz.getName().replace(".", "/") + ".class"; URL resourceURL = module.getModuleClassLoader().getResource(classPath); if (resourceURL == null) { throw new Exception("Unable to find class resource for: " + clazz.getName()); } InputStream resourceStream = resourceURL.openStream(); jarStream.putNextEntry(new ZipEntry(classPath)); byte[] classBytes = IOUtils.toByteArray(resourceStream); resourceStream.close(); jarStream.write(classBytes); jarStream.closeEntry(); } // Copy the source moduleSpec, but tweak it to specify the bytecode compiler in the // compiler plugin IDs list. ScriptModuleSpec moduleSpec = sourceArchive.getModuleSpec(); ScriptModuleSpec.Builder newModuleSpecBuilder = new ScriptModuleSpec.Builder(moduleSpec.getModuleId()); newModuleSpecBuilder.addCompilerPluginIds(moduleSpec.getCompilerPluginIds()); newModuleSpecBuilder.addCompilerPluginId(BytecodeLoadingPlugin.PLUGIN_ID); newModuleSpecBuilder.addMetadata(moduleSpec.getMetadata()); newModuleSpecBuilder.addModuleDependencies(moduleSpec.getModuleDependencies()); // Serialize the modulespec with GSON and its default spec file name ScriptModuleSpecSerializer specSerializer = new GsonScriptModuleSpecSerializer(); String json = specSerializer.serialize(newModuleSpecBuilder.build()); jarStream.putNextEntry(new ZipEntry(specSerializer.getModuleSpecFileName())); jarStream.write(json.getBytes(Charsets.UTF_8)); jarStream.closeEntry(); } finally { if (jarStream != null) { jarStream.close(); } } }
From source file:org.apache.hadoop.mapred.TestLocalJobSubmission.java
private Path makeJar(Path p) throws IOException { FileOutputStream fos = new FileOutputStream(new File(p.toString())); JarOutputStream jos = new JarOutputStream(fos); ZipEntry ze = new ZipEntry("test.jar.inside"); jos.putNextEntry(ze);// w w w .ja v a 2 s.c o m jos.write(("inside the jar!").getBytes()); jos.closeEntry(); jos.close(); return p; }
From source file:com.asual.summer.onejar.OneJarMojo.java
private void putEntry(JarOutputStream out, InputStream in, ZipEntry entry) throws IOException { out.putNextEntry(entry);// w ww .j a v a2 s . c o m IOUtils.copy(in, out); out.closeEntry(); }
From source file:org.apache.hadoop.util.TestApplicationClassLoader.java
private File makeTestJar() throws IOException { File jarFile = new File(testDir, "test.jar"); JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile)); ZipEntry entry = new ZipEntry("resource.txt"); out.putNextEntry(entry);//from w ww . ja v a 2s .c o m out.write("hello".getBytes()); out.closeEntry(); out.close(); return jarFile; }
From source file:org.apache.hadoop.filecache.TestMRWithDistributedCache.java
private Path makeJar(Path p, int index) throws FileNotFoundException, IOException { FileOutputStream fos = new FileOutputStream(new File(p.toString())); JarOutputStream jos = new JarOutputStream(fos); ZipEntry ze = new ZipEntry("distributed.jar.inside" + index); jos.putNextEntry(ze);/*from ww w . ja va 2s . com*/ jos.write(("inside the jar!" + index).getBytes()); jos.closeEntry(); jos.close(); return p; }
From source file:net.sf.keystore_explorer.crypto.signing.JarSigner.java
private static void writeManifest(byte[] manifest, JarOutputStream jos) throws IOException { // Manifest file entry JarEntry mfJarEntry = new JarEntry(MANIFEST_LOCATION); jos.putNextEntry(mfJarEntry);//from w w w .j a va 2s. c o m // Write content ByteArrayInputStream bais = null; try { bais = new ByteArrayInputStream(manifest); byte[] buffer = new byte[2048]; int read = -1; while ((read = bais.read(buffer)) != -1) { jos.write(buffer, 0, read); } jos.closeEntry(); } finally { IOUtils.closeQuietly(bais); } }
From source file:com.streamsets.datacollector.cluster.TestTarFileCreator.java
private File createJar(File parentDir) throws IOException { if (parentDir.isFile()) { parentDir.delete();//from w ww.ja va2 s.c o m } parentDir.mkdirs(); Assert.assertTrue(parentDir.isDirectory()); String uuid = UUID.randomUUID().toString(); File jar = new File(parentDir, uuid + ".jar"); JarOutputStream out = new JarOutputStream(new FileOutputStream(jar)); JarEntry entry = new JarEntry("sample.txt"); out.putNextEntry(entry); out.write(uuid.getBytes(StandardCharsets.UTF_8)); out.closeEntry(); out.close(); return jar; }
From source file:com.glaf.core.util.ZipUtils.java
public static byte[] getZipBytes(Map<String, InputStream> dataMap) { byte[] bytes = null; try {// w ww . ja v a 2 s. c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedOutputStream bos = new BufferedOutputStream(baos); JarOutputStream jos = new JarOutputStream(bos); if (dataMap != null) { Set<Entry<String, InputStream>> entrySet = dataMap.entrySet(); for (Entry<String, InputStream> entry : entrySet) { String name = entry.getKey(); InputStream inputStream = entry.getValue(); if (name != null && inputStream != null) { BufferedInputStream bis = new BufferedInputStream(inputStream); JarEntry jarEntry = new JarEntry(name); jos.putNextEntry(jarEntry); while ((len = bis.read(buf)) >= 0) { jos.write(buf, 0, len); } bis.close(); jos.closeEntry(); } } } jos.flush(); jos.close(); bos.flush(); bos.close(); bytes = baos.toByteArray(); baos.close(); return bytes; } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:org.sourcepit.osgifier.core.packaging.Repackager.java
private void rePackageJarFile(File srcJarFile, final Manifest manifest, BundleLocalization localization, final JarOutputStream destJarOut, Collection<String> pathFilters) throws IOException { destJarOut.putNextEntry(new JarEntry(JarFile.MANIFEST_NAME)); writeManifest(manifest, destJarOut); destJarOut.closeEntry(); if (localization != null) { final Set<String> paths = BundleLocalizationWriter.write(destJarOut, manifest, localization).keySet(); pathFilters = pathFilters == null ? new HashSet<String>() : new HashSet<String>(pathFilters); for (String path : paths) { pathFilters.add("!" + path); }/* w w w . j a v a 2 s . c om*/ } final PathMatcher pathMatcher = pathFilters == null ? DEFAULT_CONTENT_MATCHER : createJarContentMatcher(pathFilters); new IOOperation<JarInputStream>(jarIn(buffIn(fileIn(srcJarFile)))) { @Override protected void run(JarInputStream srcJarIn) throws IOException { copyJarContents(srcJarIn, destJarOut, pathMatcher); } }.run(); }
From source file:org.apache.apex.malhar.sql.schema.TupleSchemaRegistry.java
public String generateCommonJar() throws IOException { File file = File.createTempFile("schemaSQL", ".jar"); FileSystem fs = FileSystem.newInstance(file.toURI(), new Configuration()); FSDataOutputStream out = fs.create(new Path(file.getAbsolutePath())); JarOutputStream jout = new JarOutputStream(out); for (Schema schema : schemas.values()) { jout.putNextEntry(new ZipEntry(schema.fqcn.replace(".", "/") + ".class")); jout.write(schema.beanClassBytes); jout.closeEntry(); }/* w ww. ja va2 s . c o m*/ jout.close(); out.close(); return file.getAbsolutePath(); }