List of usage examples for java.util.jar JarOutputStream close
public void close() throws IOException
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 w w. j a va2 s . 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:JarUtils.java
/** * <P>This function will create a Jar archive containing the src * file/directory. The archive will be written to the specified * OutputStream. Directories are processed recursively, applying the * specified filter if it exists.// www . ja v a 2 s. c om * * @param out The output stream to which the generated Jar archive is * written. * @param src The file or directory to jar up. Directories will be * processed recursively. * @param filter The filter to use while processing directories. Only * those files matching will be included in the jar archive. If * null, then all files are included. * @param prefix The name of an arbitrary directory that will precede all * entries in the jar archive. If null, then no prefix will be * used. * @param man The manifest to use for the Jar archive. If null, then no * manifest will be included. * @throws IOException */ public static void jar(OutputStream out, File[] src, FileFilter filter, String prefix, Manifest man) throws IOException { for (int i = 0; i < src.length; i++) { if (!src[i].exists()) { throw new FileNotFoundException(src.toString()); } } JarOutputStream jout; if (man == null) { jout = new JarOutputStream(out); } else { jout = new JarOutputStream(out, man); } if (prefix != null && prefix.length() > 0 && !prefix.equals("/")) { // strip leading '/' if (prefix.charAt(0) == '/') { prefix = prefix.substring(1); } // ensure trailing '/' if (prefix.charAt(prefix.length() - 1) != '/') { prefix = prefix + "/"; } } else { prefix = ""; } JarInfo info = new JarInfo(jout, filter); for (int i = 0; i < src.length; i++) { jar(src[i], prefix, info); } jout.close(); }
From source file:gov.nih.nci.restgen.util.GeneratorUtil.java
public static void createJarArchive(File jarFile, File[] listFiles) throws IOException { byte b[] = new byte[10240]; FileOutputStream fout = new FileOutputStream(jarFile); JarOutputStream out = new JarOutputStream(fout, new Manifest()); for (int i = 0; i < listFiles.length; i++) { if (listFiles[i] == null || !listFiles[i].exists() || listFiles[i].isDirectory()) { System.out.println(); }/*from ww w . j a v a 2 s.c o m*/ JarEntry addFiles = new JarEntry(listFiles[i].getName()); addFiles.setTime(listFiles[i].lastModified()); out.putNextEntry(addFiles); FileInputStream fin = new FileInputStream(listFiles[i]); while (true) { int len = fin.read(b, 0, b.length); if (len <= 0) break; out.write(b, 0, len); } fin.close(); } out.close(); fout.close(); }
From source file:com.digitalreasoning.herman.JarCreater.java
public static void createJar(File outputJarFile, List<Entry> entries) throws IOException { if (!outputJarFile.getParentFile().exists()) { outputJarFile.getParentFile().mkdirs(); }/*from ww w . jav a2 s . co m*/ if (!outputJarFile.exists()) { outputJarFile.createNewFile(); } FileOutputStream fOut = new FileOutputStream(outputJarFile); JarOutputStream jarOut = new JarOutputStream(fOut); Set<String> packageSet = new HashSet<String>(); try { for (Entry folderFile : entries) { InputStream inputStream = folderFile.resource.openStream(); try { if (!packageSet.contains(folderFile.parentFolderName)) { jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName)); jarOut.closeEntry(); packageSet.add(folderFile.parentFolderName); } jarOut.putNextEntry(new ZipEntry(folderFile.parentFolderName + (folderFile.parentFolderName.endsWith("/") ? "" : "/") + folderFile.fileName)); IOUtils.copy(inputStream, jarOut); jarOut.closeEntry(); } finally { inputStream.close(); } } } finally { jarOut.close(); fOut.close(); } }
From source file:org.apache.hadoop.hbase.util.ClassLoaderTestHelper.java
/** * Jar a list of files into a jar archive. * * @param archiveFile the target jar archive * @param tobejared a list of files to be jared *//*www . j a va 2 s . c o m*/ private static boolean createJarArchive(File archiveFile, File[] tobeJared) { try { 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; } // 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(); LOG.info("Adding classes to jar file completed"); return true; } catch (Exception ex) { LOG.error("Error: " + ex.getMessage()); return false; } }
From source file:org.eclipse.gemini.blueprint.test.internal.util.ManifestUtilsTest.java
private void createJar(Manifest mf) throws Exception { mf.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); JarOutputStream out = new JarOutputStream(storage.getOutputStream(), mf); out.flush();/* ww w . ja v a2 s . c o m*/ out.close(); }
From source file:net.technicpack.launchercore.util.ZipUtils.java
public static void copyMinecraftJar(File minecraft, File output) throws IOException { String[] security = { "MOJANG_C.DSA", "MOJANG_C.SF", "CODESIGN.RSA", "CODESIGN.SF" }; JarFile jarFile = new JarFile(minecraft); try {/*from ww w . j a v a2 s . co m*/ String fileName = jarFile.getName(); String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator)); JarOutputStream jos = new JarOutputStream(new FileOutputStream(output)); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (containsAny(entry.getName(), security)) { continue; } InputStream is = jarFile.getInputStream(entry); //jos.putNextEntry(entry); //create a new entry to avoid ZipException: invalid entry compressed size jos.putNextEntry(new JarEntry(entry.getName())); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = is.read(buffer)) != -1) { jos.write(buffer, 0, bytesRead); } is.close(); jos.flush(); jos.closeEntry(); } jos.close(); } finally { jarFile.close(); } }
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());/* w w w . java 2 s . c o m*/ jarOut.close(); // 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.maven.plugins.shade.resource.ServiceResourceTransformerTest.java
@Test public void relocatedClasses() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, Arrays.asList("org.foo.exclude.*")); List<Relocator> relocators = Lists.<Relocator>newArrayList(relocator); String content = "org.foo.Service\norg.foo.exclude.OtherService\n"; byte[] contentBytes = content.getBytes("UTF-8"); InputStream contentStream = new ByteArrayInputStream(contentBytes); String contentResource = "META-INF/services/org.foo.something.another"; String contentResourceShaded = "META-INF/services/borg.foo.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); xformer.processResource(contentResource, contentStream, relocators); contentStream.close();//from w w w. ja va 2s .c o m File tempJar = File.createTempFile("shade.", ".jar"); tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); JarOutputStream jos = new JarOutputStream(fos); try { xformer.modifyOutputStream(jos, false); jos.close(); jos = null; JarFile jarFile = new JarFile(tempJar); JarEntry jarEntry = jarFile.getJarEntry(contentResourceShaded); assertNotNull(jarEntry); InputStream entryStream = jarFile.getInputStream(jarEntry); try { String xformedContent = IOUtils.toString(entryStream, "utf-8"); assertEquals("borg.foo.Service" + System.getProperty("line.separator") + "org.foo.exclude.OtherService" + System.getProperty("line.separator"), xformedContent); } finally { IOUtils.closeQuietly(entryStream); jarFile.close(); } } finally { if (jos != null) { IOUtils.closeQuietly(jos); } tempJar.delete(); } }
From source file:org.apache.maven.plugins.shade.resource.ServiceResourceTransformerTest.java
@Test public void concatenation() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null); List<Relocator> relocators = Lists.<Relocator>newArrayList(relocator); String content = "org.foo.Service\n"; byte[] contentBytes = content.getBytes("UTF-8"); InputStream contentStream = new ByteArrayInputStream(contentBytes); String contentResource = "META-INF/services/org.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); xformer.processResource(contentResource, contentStream, relocators); contentStream.close();//from w w w . ja va 2s. co m content = "org.blah.Service\n"; contentBytes = content.getBytes("UTF-8"); contentStream = new ByteArrayInputStream(contentBytes); contentResource = "META-INF/services/org.something.another"; xformer.processResource(contentResource, contentStream, relocators); contentStream.close(); File tempJar = File.createTempFile("shade.", ".jar"); tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); JarOutputStream jos = new JarOutputStream(fos); try { xformer.modifyOutputStream(jos, false); jos.close(); jos = null; JarFile jarFile = new JarFile(tempJar); JarEntry jarEntry = jarFile.getJarEntry(contentResource); assertNotNull(jarEntry); InputStream entryStream = jarFile.getInputStream(jarEntry); try { String xformedContent = IOUtils.toString(entryStream, "utf-8"); // must be two lines, with our two classes. String[] classes = xformedContent.split("\r?\n"); boolean h1 = false; boolean h2 = false; for (String name : classes) { if ("org.blah.Service".equals(name)) { h1 = true; } else if ("borg.foo.Service".equals(name)) { h2 = true; } } assertTrue(h1 && h2); } finally { IOUtils.closeQuietly(entryStream); jarFile.close(); } } finally { if (jos != null) { IOUtils.closeQuietly(jos); } tempJar.delete(); } }