List of usage examples for java.util.jar JarOutputStream putNextEntry
public void putNextEntry(ZipEntry ze) throws IOException
From source file:org.apache.pig.test.TestJobControlCompiler.java
/** * creates a jar containing a UDF not in the current classloader * @param jarFile the jar to create/*from w w w . j a va2 s .c o m*/ * @return the name of the class created (in the default package) * @throws IOException * @throws FileNotFoundException */ private String createTestJar(File jarFile) throws IOException, FileNotFoundException { // creating the source .java file File javaFile = File.createTempFile("TestUDF", ".java"); javaFile.deleteOnExit(); String className = javaFile.getName().substring(0, javaFile.getName().lastIndexOf('.')); FileWriter fw = new FileWriter(javaFile); try { fw.write("import org.apache.pig.EvalFunc;\n"); fw.write("import org.apache.pig.data.Tuple;\n"); fw.write("import java.io.IOException;\n"); fw.write("public class " + className + " extends EvalFunc<String> {\n"); fw.write(" public String exec(Tuple input) throws IOException {\n"); fw.write(" return \"test\";\n"); fw.write(" }\n"); fw.write("}\n"); } finally { fw.close(); } // compiling it JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjects(javaFile); CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits1); task.call(); // here is the compiled file File classFile = new File(javaFile.getParentFile(), className + ".class"); Assert.assertTrue(classFile.exists()); // putting it in the jar JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile)); try { jos.putNextEntry(new ZipEntry(classFile.getName())); try { InputStream testClassContentIS = new FileInputStream(classFile); try { byte[] buffer = new byte[64000]; int n; while ((n = testClassContentIS.read(buffer)) != -1) { jos.write(buffer, 0, n); } } finally { testClassContentIS.close(); } } finally { jos.closeEntry(); } } finally { jos.close(); } return className; }
From source file:com.francetelecom.clara.cloud.mvn.consumer.maven.MavenDeployer.java
private File populateJarArchive(File archive, List<FileRef> fileset) throws IOException { archive.getParentFile().mkdirs();/* ww w.j a v a2 s.com*/ JarOutputStream target = new JarOutputStream(new FileOutputStream(archive)); for (FileRef fileRef : fileset) { JarEntry jarAdd = new JarEntry(fileRef.getRelativeLocation()); target.putNextEntry(jarAdd); target.write(fileRef.getContent().getBytes()); } target.close(); return archive; }
From source file:com.migratebird.script.repository.impl.ArchiveScriptLocation.java
/** * Writes the entry with the given name and content to the given {@link JarOutputStream} * * @param jarOutputStream {@link OutputStream} to the jar file * @param name Name of the jar file entry * @param timestamp Last modification date of the entry * @param entryContentReader Reader giving access to the content of the jar entry * @throws IOException In case of disk IO problems *//* w w w . j a v a 2 s . com*/ protected void writeJarEntry(JarOutputStream jarOutputStream, String name, long timestamp, Reader entryContentReader) throws IOException { JarEntry jarEntry = new JarEntry(name); jarEntry.setTime(timestamp); jarOutputStream.putNextEntry(jarEntry); InputStream scriptInputStream = new ReaderInputStream(entryContentReader); byte[] buffer = new byte[1024]; int len; while ((len = scriptInputStream.read(buffer, 0, buffer.length)) > -1) { jarOutputStream.write(buffer, 0, len); } scriptInputStream.close(); jarOutputStream.closeEntry(); }
From source file:org.colombbus.tangara.FileUtils.java
public static void makeJar(File directory, File jar, String mainClass, PropertyChangeListener listener, Hashtable<String, String> manifestAttributes) { JarOutputStream jarOutput = null; try {//from w ww . java2 s . co m jarOutput = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(jar))); addDirectoryToJar(directory, jarOutput, null, listener); StringBuffer sbuf = new StringBuffer(); sbuf.append("Manifest-Version: 1.0\n"); sbuf.append("Built-By: Colombbus\n"); if (mainClass != null) sbuf.append("Main-Class: " + mainClass + "\n"); if (manifestAttributes != null) { for (Enumeration<String> keys = manifestAttributes.keys(); keys.hasMoreElements();) { String name = keys.nextElement(); sbuf.append(name + ": " + manifestAttributes.get(name) + "\n"); } } InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8")); JarEntry manifestEntry = new JarEntry("META-INF/MANIFEST.MF"); jarOutput.putNextEntry(manifestEntry); byte buffer[] = new byte[2048]; while (true) { int n = is.read(buffer); if (n <= 0) break; jarOutput.write(buffer, 0, n); } is.close(); jarOutput.close(); } catch (Exception e) { LOG.error("Error while creating JAR file '" + jar.getAbsolutePath() + "'", e); } finally { try { if (jarOutput != null) jarOutput.close(); } catch (IOException e) { } } }
From source file:com.github.sampov2.OneJarMojo.java
private void addToZip(JarOutputStream out, ZipEntry entry, InputStream in) throws IOException { try {//from w ww. j av a2 s .c o m out.putNextEntry(entry); IOUtils.copy(in, out); out.closeEntry(); } catch (ZipException e) { if (e.getMessage().startsWith("duplicate entry")) { // A Jar with the same name was already added. Let's add this one using a modified name: final ZipEntry alternativeEntry = new ZipEntry(entry.getName() + "-DUPLICATE-FILENAME-" + alternativeEntryCounter.incrementAndGet() + ".jar"); addToZip(out, alternativeEntry, in); } else { throw e; } } }
From source file:CreateJarFile.java
protected void createJarArchive(File archiveFile, File[] tobeJared) { try {// w w w .ja v a2s . c o m byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file 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... System.out.println("Adding " + tobeJared[i].getName()); // Add archive entry JarEntry jarAdd = new JarEntry(tobeJared[i].getName()); jarAdd.setTime(tobeJared[i].lastModified()); out.putNextEntry(jarAdd); // Write file to archive 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(); System.out.println("Adding completed OK"); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Error: " + ex.getMessage()); } }
From source file:org.apache.maven.doxia.siterenderer.DefaultSiteRendererTest.java
/** * @throws java.lang.Exception if something goes wrong. * @see org.codehaus.plexus.PlexusTestCase#setUp() *//*ww w. j a v a2 s. c o m*/ @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:org.sonatype.maven.plugin.emma4it.InstrumentProjectArtifactMojo.java
private void append(JarOutputStream output, File jar, boolean excludeMetainf) throws IOException { JarFile ejar = new JarFile(jar); Enumeration<JarEntry> entries = ejar.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); if (jarEntry.isDirectory() || (excludeMetainf && jarEntry.getName().startsWith("META-INF"))) { continue; }//from ww w .jav a2 s .c om output.putNextEntry(jarEntry); InputStream input = ejar.getInputStream(jarEntry); try { IOUtil.copy(input, output); } finally { IOUtil.close(input); } output.flush(); } }
From source file:org.sourcepit.osgifier.core.packaging.Repackager.java
private void copyJarContents(JarInputStream srcJarIn, final JarOutputStream destJarOut, PathMatcher contentMatcher) throws IOException { final Set<String> processedEntires = new HashSet<String>(); JarEntry srcEntry = srcJarIn.getNextJarEntry(); while (srcEntry != null) { final String entryName = srcEntry.getName(); if (contentMatcher.isMatch(entryName)) { if (processedEntires.add(entryName)) { destJarOut.putNextEntry(new JarEntry(srcEntry.getName())); IOUtils.copy(srcJarIn, destJarOut); destJarOut.closeEntry(); } else { logger.warn("Ignored duplicate jar entry: " + entryName); }/*from w ww. j av a 2 s. c om*/ } srcJarIn.closeEntry(); srcEntry = srcJarIn.getNextJarEntry(); } }
From source file:JarHelper.java
/** * Recursively jars up the given path under the given directory. */// ww w . j av 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(); } } }