List of usage examples for java.util.jar JarInputStream read
public int read(byte b[]) throws IOException
b.length
bytes of data from this input stream into an array of bytes. From source file:ezbake.frack.submitter.util.JarUtil.java
public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException { JarOutputStream target = null; JarInputStream source; File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath())); log.debug("Output file created at {}", outputJar.getAbsolutePath()); outputJar.createNewFile();//from ww w. j a v a 2s . co m try { source = new JarInputStream(new FileInputStream(sourceJar)); target = new JarOutputStream(new FileOutputStream(outputJar)); ZipEntry entry = source.getNextEntry(); while (entry != null) { String name = entry.getName(); // Add ZIP entry to output stream. target.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; byte[] buffer = new byte[BUFFER_SIZE]; while ((len = source.read(buffer)) > 0) { target.write(buffer, 0, len); } entry = source.getNextEntry(); } source.close(); for (File fileToAdd : newFiles) { add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target); } } finally { if (target != null) { log.debug("Closing output stream"); target.close(); } } return outputJar; }
From source file:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java
private static void unjar(Resource jar, File baseDir) throws IOException { JarInputStream jis = new JarInputStream(jar.getInputStream()); JarEntry entry = null;//from ww w .j a v a 2 s . c om try { while ((entry = jis.getNextJarEntry()) != null) { if (!entry.isDirectory()) { File file = new File(baseDir, entry.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = jis.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { IOUtils.closeStream(out); } } } } finally { IOUtils.closeStream(jis); } }
From source file:ezbake.frack.submitter.util.JarUtilTest.java
@Test public void addFileToJar() throws IOException { File tmpDir = new File("jarUtilTest"); try {/*from w ww. j a v a 2 s .c o m*/ tmpDir.mkdirs(); // Push the example jar to a file byte[] testJar = IOUtils.toByteArray(this.getClass().getResourceAsStream("/example.jar")); File jarFile = new File(tmpDir, "example.jar"); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(jarFile)); os.write(testJar); os.close(); // Push the test content to a file byte[] testContent = IOUtils.toByteArray(this.getClass().getResourceAsStream("/test.txt")); String stringTestContent = new String(testContent); File testFile = new File(tmpDir, "test.txt"); os = new BufferedOutputStream(new FileOutputStream(testFile)); os.write(testContent); os.close(); assertTrue(jarFile.exists()); assertTrue(testFile.exists()); // Add the new file to the jar File newJar = JarUtil.addFilesToJar(jarFile, Lists.newArrayList(testFile)); assertTrue("New jar file exists", newJar.exists()); assertTrue("New jar is a file", newJar.isFile()); // Roll through the entries of the new jar and JarInputStream is = new JarInputStream(new FileInputStream(newJar)); JarEntry entry = is.getNextJarEntry(); boolean foundNewFile = false; String content = ""; while (entry != null) { String name = entry.getName(); if (name.endsWith("test.txt")) { foundNewFile = true; byte[] buffer = new byte[1]; while ((is.read(buffer)) > 0) { content += new String(buffer); } break; } entry = is.getNextJarEntry(); } is.close(); assertTrue("New file was in repackaged jar", foundNewFile); assertEquals("Content of added file is the same as the retrieved new file", stringTestContent, content); } finally { FileUtils.deleteDirectory(tmpDir); } }
From source file:com.ikon.util.cl.BinaryClassLoader.java
/** * Create internal classes and resources cache *//*w w w.ja v a 2s. co m*/ private void createCache(byte[] buf) throws IOException { ByteArrayInputStream bais = null; JarInputStream jis = null; byte[] buffer = new byte[1024 * 4]; try { bais = new ByteArrayInputStream(buf); jis = new JarInputStream(bais); Attributes attr = jis.getManifest().getMainAttributes(); mainClassName = attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null; for (JarEntry entry = null; (entry = jis.getNextJarEntry()) != null;) { String name = entry.getName(); if (!entry.isDirectory()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); for (int n = 0; -1 != (n = jis.read(buffer));) { byteStream.write(buffer, 0, n); } if (name.endsWith(".class")) { String className = name.substring(0, name.indexOf('.')).replace('/', '.'); resources.put(className, byteStream.toByteArray()); } else { resources.put(name, byteStream.toByteArray()); } byteStream.close(); } } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(jis); IOUtils.closeQuietly(bais); } }
From source file:org.exist.repo.Deployment.java
public DocumentImpl getDescriptor(File jar) throws IOException, PackageException { final InputStream istream = new BufferedInputStream(new FileInputStream(jar)); final JarInputStream jis = new JarInputStream(istream); JarEntry entry;// w w w . java 2 s .co m DocumentImpl doc = null; while ((entry = jis.getNextJarEntry()) != null) { if (!entry.isDirectory() && "expath-pkg.xml".equals(entry.getName())) { final ByteArrayOutputStream bos = new ByteArrayOutputStream(); int c; final byte[] b = new byte[4096]; while ((c = jis.read(b)) > 0) { bos.write(b, 0, c); } bos.close(); final byte[] data = bos.toByteArray(); final ByteArrayInputStream bis = new ByteArrayInputStream(data); try { doc = DocUtils.parse(broker.getBrokerPool(), null, bis); } catch (final XPathException e) { throw new PackageException("Error while parsing expath-pkg.xml: " + e.getMessage(), e); } break; } } jis.close(); return doc; }
From source file:com.amalto.core.jobox.watch.JoboxListener.java
public void contextChanged(String jobFile, String context) { File entity = new File(jobFile); String sourcePath = jobFile;// w w w . j a va2 s. co m int dotMark = jobFile.lastIndexOf("."); //$NON-NLS-1$ int separateMark = jobFile.lastIndexOf(File.separatorChar); if (dotMark != -1) { sourcePath = System.getProperty("java.io.tmpdir") + File.separatorChar //$NON-NLS-1$ + jobFile.substring(separateMark, dotMark); } try { JoboxUtil.extract(jobFile, System.getProperty("java.io.tmpdir") + File.separatorChar); //$NON-NLS-1$ } catch (Exception e1) { LOGGER.error("Extraction exception occurred.", e1); return; } List<File> resultList = new ArrayList<File>(); JoboxUtil.findFirstFile(null, new File(sourcePath), "classpath.jar", resultList); //$NON-NLS-1$ if (!resultList.isEmpty()) { JarInputStream jarIn = null; JarOutputStream jarOut = null; try { JarFile jarFile = new JarFile(resultList.get(0)); Manifest mf = jarFile.getManifest(); jarIn = new JarInputStream(new FileInputStream(resultList.get(0))); Manifest newManifest = jarIn.getManifest(); if (newManifest == null) { newManifest = new Manifest(); } newManifest.getMainAttributes().putAll(mf.getMainAttributes()); newManifest.getMainAttributes().putValue("activeContext", context); //$NON-NLS-1$ jarOut = new JarOutputStream(new FileOutputStream(resultList.get(0)), newManifest); byte[] buf = new byte[4096]; JarEntry entry; while ((entry = jarIn.getNextJarEntry()) != null) { if ("META-INF/MANIFEST.MF".equals(entry.getName())) { //$NON-NLS-1$ continue; } jarOut.putNextEntry(entry); int read; while ((read = jarIn.read(buf)) != -1) { jarOut.write(buf, 0, read); } jarOut.closeEntry(); } } catch (Exception e) { LOGGER.error("Extraction exception occurred.", e); } finally { IOUtils.closeQuietly(jarIn); IOUtils.closeQuietly(jarOut); } // re-zip file if (entity.getName().endsWith(".zip")) { //$NON-NLS-1$ File sourceFile = new File(sourcePath); try { JoboxUtil.zip(sourceFile, jobFile); } catch (Exception e) { LOGGER.error("Zip exception occurred.", e); } } } }
From source file:fr.gael.dhus.server.http.TomcatServer.java
public void install(WebApplication web_application) throws TomcatException { try {// ww w . j a v a 2s .c o m String folder = web_application.getName() == "" ? "ROOT" : web_application.getName(); if (web_application.hasWarStream()) { InputStream stream = web_application.getWarStream(); if (stream == null) { throw new TomcatException("Cannot install WebApplication " + web_application.getName() + ". The referenced war " + "file does not exist."); } JarInputStream jis = new JarInputStream(stream); File destDir = new File(tomcatpath, "webapps/" + folder); byte[] buffer = new byte[4096]; JarEntry file; while ((file = jis.getNextJarEntry()) != null) { File f = new File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdirs(); continue; } if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } java.io.FileOutputStream fos = new java.io.FileOutputStream(f); int read; while ((read = jis.read(buffer)) != -1) { fos.write(buffer, 0, read); } fos.flush(); fos.close(); } jis.close(); } web_application.configure(new File(tomcatpath, "webapps/" + folder).getPath()); StandardEngine engine = (StandardEngine) cat.getServer().findServices()[0].getContainer(); Container container = engine.findChild(engine.getDefaultHost()); StandardContext ctx = new StandardContext(); String url = (web_application.getName() == "" ? "" : "/") + web_application.getName(); ctx.setName(url); ctx.setPath(url); ctx.setDocBase(new File(tomcatpath, "webapps/" + folder).getPath()); ctx.addLifecycleListener(new DefaultWebXmlListener()); ctx.setConfigFile(getWebappConfigFile(new File(tomcatpath, "webapps/" + folder).getPath(), url)); ContextConfig ctxCfg = new ContextConfig(); ctx.addLifecycleListener(ctxCfg); ctxCfg.setDefaultWebXml("fr/gael/dhus/server/http/global-web.xml"); container.addChild(ctx); contexts.add(ctx); List<WebServlet> servlets = web_application.getServlets(); for (WebServlet servlet : servlets) { addServlet(ctx, servlet.getServletName(), servlet.getUrlPattern(), servlet.getServlet(), servlet.isLoadOnStartup()); } List<String> welcomeFiles = web_application.getWelcomeFiles(); for (String welcomeFile : welcomeFiles) { ctx.addWelcomeFile(welcomeFile); } if (web_application.getAllow() != null || web_application.getDeny() != null) { RemoteIpValve valve = new RemoteIpValve(); valve.setRemoteIpHeader("x-forwarded-for"); valve.setProxiesHeader("x-forwarded-by"); valve.setProtocolHeader("x-forwarded-proto"); ctx.addValve(valve); RemoteAddrValve valve_addr = new RemoteAddrValve(); valve_addr.setAllow(web_application.getAllow()); valve_addr.setDeny(web_application.getDeny()); ctx.addValve(valve_addr); } } catch (Exception e) { throw new TomcatException("Cannot install service", e); } }
From source file:fr.gael.dhus.server.http.TomcatServer.java
public void install(fr.gael.dhus.server.http.WebApplication web_application) throws TomcatException { logger.info("Installing webapp " + web_application); String appName = web_application.getName(); String folder;/*from ww w.ja v a2 s. c o m*/ if (appName.trim().isEmpty()) { folder = "ROOT"; } else { folder = appName; } try { if (web_application.hasWarStream()) { InputStream stream = web_application.getWarStream(); if (stream == null) { throw new TomcatException("Cannot install webApplication " + web_application.getName() + ". The referenced war file does not exist."); } JarInputStream jis = new JarInputStream(stream); File destDir = new File(tomcatpath, "webapps/" + folder); byte[] buffer = new byte[4096]; JarEntry file; while ((file = jis.getNextJarEntry()) != null) { File f = new File(destDir + java.io.File.separator + file.getName()); if (file.isDirectory()) { // if its a directory, create it f.mkdirs(); continue; } if (!f.getParentFile().exists()) { f.getParentFile().mkdirs(); } java.io.FileOutputStream fos = new java.io.FileOutputStream(f); int read; while ((read = jis.read(buffer)) != -1) { fos.write(buffer, 0, read); } fos.flush(); fos.close(); } jis.close(); } web_application.configure(new File(tomcatpath, "webapps/" + folder).getPath()); StandardEngine engine = (StandardEngine) cat.getServer().findServices()[0].getContainer(); Container container = engine.findChild(engine.getDefaultHost()); StandardContext ctx = new StandardContext(); String url = (web_application.getName() == "" ? "" : "/") + web_application.getName(); ctx.setName(url); ctx.setPath(url); ctx.setDocBase(new File(tomcatpath, "webapps/" + folder).getPath()); ctx.addLifecycleListener(new DefaultWebXmlListener()); ctx.setConfigFile(getWebappConfigFile(new File(tomcatpath, "webapps/" + folder).getPath(), url)); ContextConfig ctxCfg = new ContextConfig(); ctx.addLifecycleListener(ctxCfg); ctxCfg.setDefaultWebXml("fr/gael/dhus/server/http/global-web.xml"); container.addChild(ctx); List<String> welcomeFiles = web_application.getWelcomeFiles(); for (String welcomeFile : welcomeFiles) { ctx.addWelcomeFile(welcomeFile); } if (web_application.getAllow() != null || web_application.getDeny() != null) { RemoteIpValve valve = new RemoteIpValve(); valve.setRemoteIpHeader("x-forwarded-for"); valve.setProxiesHeader("x-forwarded-by"); valve.setProtocolHeader("x-forwarded-proto"); ctx.addValve(valve); RemoteAddrValve valve_addr = new RemoteAddrValve(); valve_addr.setAllow(web_application.getAllow()); valve_addr.setDeny(web_application.getDeny()); ctx.addValve(valve_addr); } web_application.checkInstallation(); } catch (Exception e) { throw new TomcatException("Cannot install webApplication " + web_application.getName(), e); } }
From source file:org.hecl.jarhack.JarHack.java
/** * The <code>substHecl</code> method takes the filenames of two * .jar's - one as input, the second as output, in addition to the * name of the application. Where it counts, the old name (Hecl, * usually) is overridden with the new name, and the new .jar file * is written to the specified outfile. Via the iconname argument * it is also possible to specify a new icon file to use. * * @param infile a <code>FileInputStream</code> value * @param outfile a <code>String</code> value * @param newname a <code>String</code> value * @param iconname a <code>String</code> value * @exception IOException if an error occurs *///from w w w. j av a 2 s. c o m public static void substHecl(InputStream infile, String outfile, String newname, String iconname, String scriptfile) throws IOException { JarInputStream jif = new JarInputStream(infile); Manifest mf = jif.getManifest(); Attributes attrs = mf.getMainAttributes(); Set keys = attrs.keySet(); Iterator it = keys.iterator(); while (it.hasNext()) { Object key = it.next(); Object value = attrs.get(key); String keyname = key.toString(); /* These are the three cases that interest us in * particular, where we need to make changes. */ if (keyname.equals("MIDlet-Name")) { attrs.putValue(keyname, newname); } else if (keyname.equals("MIDlet-1")) { String valuestr = value.toString(); /* FIXME - the stringsplit method is used for older * versions of GCJ. Once newer versions are common, * it can go away. Or not - it works just fine. */ String properties[] = stringsplit(valuestr, ", "); attrs.putValue(keyname, newname + ", " + properties[1] + ", " + properties[2]); } else if (keyname.equals("MicroEdition-Configuration")) { cldcversion = value.toString(); } else if (keyname.equals("MicroEdition-Profile")) { midpversion = value.toString(); } else if (keyname.equals("MIDlet-Jar-URL")) { attrs.put(key, newname + ".jar"); } } JarOutputStream jof = new JarOutputStream(new FileOutputStream(outfile), mf); byte[] buf = new byte[4096]; /* Go through the various entries. */ JarEntry entry; int read; while ((entry = jif.getNextJarEntry()) != null) { /* Don't copy the manifest file. */ if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue; /* Insert our own icon */ if (iconname != null && "Hecl.png".equals(entry.getName())) { jof.putNextEntry(new JarEntry("Hecl.png")); FileInputStream inf = new FileInputStream(iconname); while ((read = inf.read(buf)) != -1) { jof.write(buf, 0, read); } inf.close(); } /* Insert our own copy of the script file. */ else if ("script.hcl".equals(entry.getName())) { jof.putNextEntry(new JarEntry("script.hcl")); FileInputStream inf = new FileInputStream(scriptfile); while ((read = inf.read(buf)) != -1) { jof.write(buf, 0, read); } inf.close(); } else { /* Otherwise, just copy the entry. */ jof.putNextEntry(entry); while ((read = jif.read(buf)) != -1) { jof.write(buf, 0, read); } } jof.closeEntry(); } jof.flush(); jof.close(); jif.close(); }