List of usage examples for java.util.jar JarOutputStream write
public void write(int b) throws IOException
From source file:com.samczsun.helios.utils.Utils.java
public static void save(File dest, Map<String, byte[]> data, Predicate<String> accept) { try {//from w w w.jav a 2s .c o m JarOutputStream out = new JarOutputStream(new FileOutputStream(dest)); Set<String> added = new HashSet<>(); for (Entry<String, byte[]> entry : data.entrySet()) { String name = entry.getKey(); if (added.add(name) && accept.test(name)) { out.putNextEntry(new ZipEntry(name)); out.write(entry.getValue()); out.closeEntry(); } } out.close(); } catch (IOException e) { ExceptionHandler.handle(e); } }
From source file:org.eclipse.wb.tests.designer.TestUtils.java
/** * @return the path to the temporary "jar" file with single entry. * //from ww w . java 2 s .c o m * @param entryName * the name of entry, for example <code>"myFolder/subFolder/file.txt"</code>. * @param content * the {@link String} content of entry. */ public static String createTemporaryJar(String entryName, String content) throws Exception { File tempFile = File.createTempFile("wbpTests", ".jar"); tempFile.deleteOnExit(); // create "jar" with single entry { JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(tempFile)); jarOutputStream.putNextEntry(new ZipEntry(entryName)); jarOutputStream.write(content.getBytes()); jarOutputStream.closeEntry(); jarOutputStream.close(); } // return path to "jar" return tempFile.getAbsolutePath(); }
From source file:org.apache.hadoop.yarn.util.TestFSDownload.java
static LocalResource createJarFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException, URISyntaxException { byte[] bytes = new byte[len]; r.nextBytes(bytes);//from w w w . j a va 2s . co m File archiveFile = new File(p.toUri().getPath() + ".jar"); archiveFile.createNewFile(); JarOutputStream out = new JarOutputStream(new FileOutputStream(archiveFile)); out.putNextEntry(new JarEntry(p.getName())); out.write(bytes); out.closeEntry(); out.close(); LocalResource ret = recordFactory.newRecordInstance(LocalResource.class); ret.setResource(URL.fromPath(new Path(p.toString() + ".jar"))); ret.setSize(len); ret.setType(LocalResourceType.ARCHIVE); ret.setVisibility(vis); ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".jar")).getModificationTime()); return ret; }
From source file:org.bonitasoft.engine.io.IOUtil.java
public static byte[] generateJar(final Map<String, byte[]> resources) throws IOException { if (resources == null || resources.isEmpty()) { final String message = "No resources available"; throw new IOException(message); }/*from w ww . j a v a2s. c o m*/ ByteArrayOutputStream baos = null; JarOutputStream jarOutStream = null; try { baos = new ByteArrayOutputStream(); jarOutStream = new JarOutputStream(new BufferedOutputStream(baos)); for (final Map.Entry<String, byte[]> resource : resources.entrySet()) { jarOutStream.putNextEntry(new JarEntry(resource.getKey())); jarOutStream.write(resource.getValue()); } jarOutStream.flush(); baos.flush(); } finally { if (jarOutStream != null) { jarOutStream.close(); } if (baos != null) { baos.close(); } } return baos.toByteArray(); }
From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java
private static void addEntry(JarOutputStream jas, String name, byte[] content) throws Exception { JarEntry entry = new JarEntry(name); jas.putNextEntry(entry);//from w w w . ja v a 2 s . com if (content != null) { jas.write(content); } jas.closeEntry(); }
From source file:com.dragome.callbackevictor.serverside.utils.RewritingUtils.java
public static boolean rewriteJar(final JarInputStream pInput, final ResourceTransformer transformer, final JarOutputStream pOutput, final Matcher pMatcher) throws IOException { boolean changed = false; while (true) { final JarEntry entry = pInput.getNextJarEntry(); if (entry == null) { break; }/*from ww w . j av a2s . c o m*/ if (entry.isDirectory()) { pOutput.putNextEntry(new JarEntry(entry)); continue; } final String name = entry.getName(); pOutput.putNextEntry(new JarEntry(name)); if (name.endsWith(".class")) { if (pMatcher.isMatching(name)) { if (log.isDebugEnabled()) { log.debug("transforming " + name); } final byte[] original = toByteArray(pInput); byte[] transformed = transformer.transform(original); pOutput.write(transformed); changed |= transformed.length != original.length; continue; } } else if (name.endsWith(".jar") || name.endsWith(".ear") || name.endsWith(".zip") || name.endsWith(".war")) { changed |= rewriteJar(new JarInputStream(pInput), transformer, new JarOutputStream(pOutput), pMatcher); continue; } int length = copy(pInput, pOutput); log.debug("copied " + name + "(" + length + ")"); } pInput.close(); pOutput.close(); return changed; }
From source file:org.commonjava.indy.ftest.core.content.StoreAndVerifyJarViaDirectDownloadTest.java
@Test public void storeFileThenDownloadAndVerifyContentViaDirectDownload() throws Exception { final String content = "This is a test: " + System.nanoTime(); String entryName = "org/something/foo.class"; ByteArrayOutputStream out = new ByteArrayOutputStream(); JarOutputStream jarOut = new JarOutputStream(out); jarOut.putNextEntry(new JarEntry(entryName)); jarOut.write(content.getBytes()); jarOut.close();//from w w w . j a v a2 s . c o m // Used to visually inspect the jars moving up... // String userDir = System.getProperty( "user.home" ); // File dir = new File( userDir, "temp" ); // dir.mkdirs(); // // FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-in.jar" ), out.toByteArray() ); final InputStream stream = new ByteArrayInputStream(out.toByteArray()); final String path = "/path/to/" + getClass().getSimpleName() + "-" + name.getMethodName() + ".jar"; assertThat(client.content().exists(hosted, STORE, path), equalTo(false)); client.content().store(hosted, STORE, path, stream); assertThat(client.content().exists(hosted, STORE, path), equalTo(true)); final URL url = new URL(client.content().contentUrl(hosted, STORE, path)); final InputStream is = url.openStream(); byte[] result = IOUtils.toByteArray(is); is.close(); assertThat(result, equalTo(out.toByteArray())); // ...and down // FileUtils.writeByteArrayToFile( new File( dir, name.getMethodName() + "-out.jar" ), result ); JarInputStream jarIn = new JarInputStream(new ByteArrayInputStream(result)); JarEntry jarEntry = jarIn.getNextJarEntry(); assertThat(jarEntry.getName(), equalTo(entryName)); String contentResult = IOUtils.toString(jarIn); assertThat(contentResult, equalTo(content)); }
From source file:org.apache.brooklyn.rest.resources.BundleAndTypeResourcesTest.java
private static File createJar(Map<String, String> files) throws Exception { File f = Os.newTempFile("osgi", "jar"); JarOutputStream zip = new JarOutputStream(new FileOutputStream(f)); for (Map.Entry<String, String> entry : files.entrySet()) { JarEntry ze = new JarEntry(entry.getKey()); zip.putNextEntry(ze);/*from w w w . ja va2 s . c om*/ zip.write(entry.getValue().getBytes()); } zip.closeEntry(); zip.flush(); zip.close(); return f; }
From source file:com.netflix.nicobar.core.module.ScriptModuleUtils.java
/** * Convert a ScriptModule to its compiled equivalent ScriptArchive. * <p>/*from w w w . j av a2 s.c o m*/ * 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);//from w w w .j a v a 2 s . c o m jos.write(("inside the jar!").getBytes()); jos.closeEntry(); jos.close(); return p; }