List of usage examples for java.util.jar JarOutputStream closeEntry
public void closeEntry() throws IOException
From source file:org.apache.openjpa.eclipse.PluginLibrary.java
void copyJar(JarInputStream jar, JarOutputStream out) throws IOException { if (jar == null || out == null) return;//w w w. ja v a 2 s . 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:org.echocat.nodoodle.transport.HandlerPacker.java
protected void writeData(Handler<?> handler, JarOutputStream jar, String dataFileName) throws IOException { if (handler == null) { throw new NullPointerException(); }// www. ja v a2 s . c o m if (jar == null) { throw new NullPointerException(); } final JarEntry entry = new JarEntry(dataFileName); jar.putNextEntry(entry); final ObjectOutputStream oos = new ObjectOutputStream(jar); oos.writeObject(handler); oos.flush(); jar.closeEntry(); }
From source file:com.opensymphony.xwork2.util.fs.JarEntryRevisionTest.java
private String createJarFile(long time) throws Exception { Manifest manifest = new Manifest(); manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); Path jarPath = Paths//w w w. jav a 2 s . c o m .get(Thread.currentThread().getContextClassLoader().getResource("xwork-jar.jar").toURI()) .getParent(); File jarFile = jarPath.resolve("JarEntryRevisionTest_testNeedsReloading.jar").toFile(); FileOutputStream fos = new FileOutputStream(jarFile, false); JarOutputStream target = new JarOutputStream(fos, manifest); target.putNextEntry(new ZipEntry("com/opensymphony/xwork2/util/fs/")); ZipEntry entry = new ZipEntry("com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class"); entry.setTime(time); target.putNextEntry(entry); InputStream source = getClass() .getResourceAsStream("/com/opensymphony/xwork2/util/fs/JarEntryRevisionTest.class"); IOUtils.copy(source, target); source.close(); target.closeEntry(); target.close(); fos.close(); return jarFile.toURI().toURL().toExternalForm(); }
From source file:JarHelper.java
/** * Recursively jars up the given path under the given directory. *///w ww . j a v a 2 s . c om private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException { if (mVerbose) System.out.println("checking " + dirOrFile2jar); if (dirOrFile2jar.isDirectory()) { String[] dirList = dirOrFile2jar.list(); 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(); } for (int i = 0; i < dirList.length; i++) { File f = new File(dirOrFile2jar, dirList[i]); jarDir(f, jos, subPath); } } else { if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) { if (mVerbose) System.out.println("skipping " + dirOrFile2jar.getPath()); return; } if (mVerbose) 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(); } } }
From source file:org.reficio.p2.FeatureBuilder.java
private void addToJar(JarOutputStream jar, File content) throws IOException { for (File f : FileUtils.listFiles(content, null, true)) { String fname = f.getPath().replace("\\", "/"); if (f.isDirectory()) { if (!fname.endsWith("/")) { fname = fname + "/"; }/*from w w w . j a v a2s .c o m*/ JarEntry entry = new JarEntry(fname); entry.setTime(f.lastModified()); jar.putNextEntry(entry); jar.closeEntry(); } else { //must be a file JarEntry entry = new JarEntry(fname); entry.setTime(f.lastModified()); jar.putNextEntry(entry); jar.write(IOUtils.toByteArray(new FileInputStream(f))); jar.closeEntry(); } } }
From source file:com.hellblazer.process.JavaProcessTest.java
protected void copyTestJarFile() throws Exception { String classFileName = HelloWorld.class.getCanonicalName().replace('.', '/') + ".class"; URL classFile = getClass().getResource("/" + classFileName); assertNotNull(classFile);//from w w w . ja v a 2 s . c om Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.putValue("Manifest-Version", "1.0"); attributes.putValue("Main-Class", HelloWorld.class.getCanonicalName()); FileOutputStream fos = new FileOutputStream(new File(testDir, TEST_JAR)); JarOutputStream jar = new JarOutputStream(fos, manifest); JarEntry entry = new JarEntry(classFileName); jar.putNextEntry(entry); InputStream in = classFile.openStream(); byte[] buffer = new byte[1024]; for (int read = in.read(buffer); read != -1; read = in.read(buffer)) { jar.write(buffer, 0, read); } in.close(); jar.closeEntry(); jar.close(); }
From source file:org.apache.maven.doxia.siterenderer.DefaultSiteRendererTest.java
/** * @throws java.lang.Exception if something goes wrong. * @see org.codehaus.plexus.PlexusTestCase#setUp() *///from ww w . jav a 2 s.c om @Override protected void setUp() throws Exception { super.setUp(); renderer = (Renderer) lookup(Renderer.ROLE); // copy the default-site.vm and default-site-macros.vm copyVm("default-site.vm", "\n\n\n\r\n\r\n\r\n"); copyVm("default-site-macros.vm", ""); InputStream skinIS = this.getResourceAsStream("velocity-toolmanager.vm"); JarOutputStream jarOS = new JarOutputStream(new FileOutputStream(skinJar)); try { jarOS.putNextEntry(new ZipEntry("META-INF/maven/site.vm")); IOUtil.copy(skinIS, jarOS); jarOS.closeEntry(); } finally { IOUtil.close(skinIS); IOUtil.close(jarOS); } oldLocale = Locale.getDefault(); Locale.setDefault(Locale.ENGLISH); }
From source file:rapture.kernel.JarApiImplTest.java
private void add(File source, JarOutputStream target) throws IOException { BufferedInputStream in = null; try {/*w w w.j av a 2 s. c om*/ if (source.isDirectory()) { String name = source.getPath().replace("\\", "/"); if (!name.isEmpty()) { if (!name.endsWith("/")) { name += "/"; } JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); } for (File nestedFile : source.listFiles()) { add(nestedFile, target); } return; } JarEntry entry = new JarEntry(source.getPath().replace("\\", "/")); entry.setTime(source.lastModified()); target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source)); byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) { break; } target.write(buffer, 0, count); } target.closeEntry(); } finally { if (in != null) { in.close(); } } }
From source file:org.echocat.nodoodle.transport.HandlerPacker.java
protected void writeJarResource(JarResource jarResource, JarOutputStream jar, String fileName) throws IOException { if (jar == null) { throw new NullPointerException(); }/* w ww . j a v a 2 s . c om*/ if (fileName == null) { throw new NullPointerException(); } if (jarResource != null) { final InputStream inputStream = jarResource.getResourceAsJar(); try { final JarEntry e = new JarEntry(fileName); jar.putNextEntry(e); IOUtils.copy(inputStream, jar); jar.closeEntry(); } finally { inputStream.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 */// w w w.ja v a2s . co 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(); }