List of usage examples for java.util.jar JarOutputStream close
public void close() throws IOException
From source file:org.apache.hadoop.hbase.TestClassFinder.java
/** * Makes a jar out of some class files. Unfortunately it's very tedious. * @param filesInJar Files created via compileTestClass. * @return path to the resulting jar file. *//*from w w w. j a v a 2 s . c o m*/ private static String packageAndLoadJar(FileAndPath... filesInJar) throws Exception { // First, write the bogus jar file. String path = basePath + "jar" + jarCounter.incrementAndGet() + ".jar"; Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); FileOutputStream fos = new FileOutputStream(path); JarOutputStream jarOutputStream = new JarOutputStream(fos, manifest); // Directory entries for all packages have to be added explicitly for // resources to be findable via ClassLoader. Directory entries must end // with "/"; the initial one is expected to, also. Set<String> pathsInJar = new HashSet<String>(); for (FileAndPath fileAndPath : filesInJar) { String pathToAdd = fileAndPath.path; while (pathsInJar.add(pathToAdd)) { int ix = pathToAdd.lastIndexOf('/', pathToAdd.length() - 2); if (ix < 0) { break; } pathToAdd = pathToAdd.substring(0, ix); } } for (String pathInJar : pathsInJar) { jarOutputStream.putNextEntry(new JarEntry(pathInJar)); jarOutputStream.closeEntry(); } for (FileAndPath fileAndPath : filesInJar) { File file = fileAndPath.file; 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(); // Add the file to classpath. File jarFile = new File(path); assertTrue(jarFile.exists()); URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); method.setAccessible(true); method.invoke(urlClassLoader, new Object[] { jarFile.toURI().toURL() }); return jarFile.getAbsolutePath(); }
From source file:com.glaf.core.util.ZipUtils.java
public static byte[] toZipBytes(Map<String, byte[]> zipMap) { byte[] bytes = null; InputStream inputStream = null; BufferedInputStream bis = null; ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; JarOutputStream jos = null; try {//from www . j av a 2s .c om baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos); jos = new JarOutputStream(bos); if (zipMap != null) { Set<Entry<String, byte[]>> entrySet = zipMap.entrySet(); for (Entry<String, byte[]> entry : entrySet) { String name = entry.getKey(); byte[] x_bytes = entry.getValue(); inputStream = new ByteArrayInputStream(x_bytes); if (name != null && inputStream != null) { bis = new BufferedInputStream(inputStream); JarEntry jarEntry = new JarEntry(name); jos.putNextEntry(jarEntry); while ((len = bis.read(buf)) >= 0) { jos.write(buf, 0, len); } IOUtils.closeStream(bis); jos.closeEntry(); } IOUtils.closeStream(inputStream); } } jos.flush(); jos.close(); bytes = baos.toByteArray(); IOUtils.closeStream(baos); IOUtils.closeStream(bos); return bytes; } catch (Exception ex) { throw new RuntimeException(ex); } finally { IOUtils.closeStream(inputStream); IOUtils.closeStream(baos); IOUtils.closeStream(bos); } }
From source file:Main.java
void createJarArchive(File archiveFile, File[] tobeJared) throws Exception { byte buffer[] = new byte[BUFFER_SIZE]; FileOutputStream stream = new FileOutputStream(archiveFile); JarOutputStream out = new JarOutputStream(stream, new Manifest()); for (int i = 0; i < tobeJared.length; i++) { if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) continue; // Just in case... JarEntry jarAdd = new JarEntry(tobeJared[i].getName()); jarAdd.setTime(tobeJared[i].lastModified()); out.putNextEntry(jarAdd);/*w w w .ja va 2 s .co m*/ FileInputStream in = new FileInputStream(tobeJared[i]); 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(); }
From source file:org.apache.openjpa.eclipse.PluginLibrary.java
void copyJar(JarInputStream jar, JarOutputStream out) throws IOException { if (jar == null || out == null) return;//from w ww . ja v a 2s . c om try { JarEntry entry = null; while ((entry = jar.getNextJarEntry()) != null) { out.putNextEntry(entry); int b = -1; while ((b = jar.read()) != -1) { out.write(b); } } out.closeEntry(); } finally { out.finish(); out.flush(); out.close(); jar.close(); } }
From source file:com.aliyun.odps.mapred.BridgeJobRunner.java
/** * Create jar with jobconf.// w ww . j a v a2 s. com * * @return * @throws OdpsException */ private ByteArrayOutputStream createJarArchive() throws OdpsException { try { ByteArrayOutputStream archiveOut = new ByteArrayOutputStream(); // Open archive file JarOutputStream out = new JarOutputStream(archiveOut, new Manifest()); ByteArrayOutputStream jobOut = new ByteArrayOutputStream(); job.writeXml(jobOut); // Add jobconf entry JarEntry jobconfEntry = new JarEntry("jobconf.xml"); out.putNextEntry(jobconfEntry); out.write(jobOut.toByteArray()); out.close(); return archiveOut; } catch (IOException ex) { throw new OdpsException(ErrorCode.UNEXPECTED.toString(), ex); } }
From source file:JarMaker.java
/** * Combine several jar files and some given files into one jar file. * @param outJar The output jar file's filename. * @param inJarsList The jar files to be combined * @param filter A filter that exclude some entries of input jars. User * should implement the EntryFilter interface. * @param inFileList The files to be added into the jar file. * @param prefixs The prefixs of files to be added into the jar file. * inFileList and prefixs should be paired. * @throws FileNotFoundException//from ww w . j a va 2s .co m * @throws IOException */ @SuppressWarnings("unchecked") public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList, String[] prefixs) throws FileNotFoundException, IOException { JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar)); ArrayList entryList = new ArrayList(); for (int i = 0; i < inJarsList.length; i++) { BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i])); ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream); byte abyte0[] = new byte[1024 * 4]; for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream .getNextEntry()) { if (filter.accept(zipentry)) { if (!entryList.contains(zipentry.getName())) { jout.putNextEntry(zipentry); if (!zipentry.isDirectory()) { int j; try { while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1) jout.write(abyte0, 0, j); } catch (IOException ie) { throw ie; } } entryList.add(zipentry.getName()); } } } zipinputstream.close(); bufferedinputstream.close(); } for (int i = 0; i < inFileList.length; i++) { add(jout, new File(inFileList[i]), prefixs[i]); } jout.close(); }
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 ww . ja v a 2 s .c om*/ 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(); }
From source file:org.apache.solr.core.TestCoreContainer.java
@Test public void testSharedLib() throws Exception { Path tmpRoot = createTempDir("testSharedLib"); File lib = new File(tmpRoot.toFile(), "lib"); lib.mkdirs();/*from ww w.ja va2 s . c o m*/ JarOutputStream jar1 = new JarOutputStream(new FileOutputStream(new File(lib, "jar1.jar"))); jar1.putNextEntry(new JarEntry("defaultSharedLibFile")); jar1.closeEntry(); jar1.close(); File customLib = new File(tmpRoot.toFile(), "customLib"); customLib.mkdirs(); JarOutputStream jar2 = new JarOutputStream(new FileOutputStream(new File(customLib, "jar2.jar"))); jar2.putNextEntry(new JarEntry("customSharedLibFile")); jar2.closeEntry(); jar2.close(); final CoreContainer cc1 = init(tmpRoot, "<solr></solr>"); try { cc1.loader.openResource("defaultSharedLibFile").close(); } finally { cc1.shutdown(); } final CoreContainer cc2 = init(tmpRoot, "<solr><str name=\"sharedLib\">lib</str></solr>"); try { cc2.loader.openResource("defaultSharedLibFile").close(); } finally { cc2.shutdown(); } final CoreContainer cc3 = init(tmpRoot, "<solr><str name=\"sharedLib\">customLib</str></solr>"); try { cc3.loader.openResource("customSharedLibFile").close(); } finally { cc3.shutdown(); } }
From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java
@Test public void testJarUploadNoMvnPath() throws Exception { String jarPath = "acme-core-1.0.jar"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream jas = new JarOutputStream(baos); addEntry(jas, "hello.txt", "Hello!".getBytes()); jas.close(); byte[] contents = baos.toByteArray(); testUpload(jarPath, contents, true); }
From source file:io.fabric8.maven.proxy.impl.MavenProxyServletSupportTest.java
@Test public void testJarUploadFullMvnPath() throws Exception { String jarPath = "org.acme/acme-core/1.0/acme-core-1.0.jar"; ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream jas = new JarOutputStream(baos); addEntry(jas, "hello.txt", "Hello!".getBytes()); jas.close(); byte[] contents = baos.toByteArray(); testUpload(jarPath, contents, false); }