java.util.jar.JarOutputStream Java Examples
The following examples show how to use
java.util.jar.JarOutputStream.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Content.java From SikuliNG with MIT License | 6 votes |
private static void addToJar(JarOutputStream jar, File dir, String prefix) throws IOException { File[] content; prefix = prefix == null ? "" : prefix; if (dir.isDirectory()) { content = dir.listFiles(); for (int i = 0, l = content.length; i < l; ++i) { if (content[i].isDirectory()) { jar.putNextEntry(new ZipEntry(prefix + (prefix.equals("") ? "" : "/") + content[i].getName() + "/")); addToJar(jar, content[i], prefix + (prefix.equals("") ? "" : "/") + content[i].getName()); } else { addToJarWriteFile(jar, content[i], prefix); } } } else { addToJarWriteFile(jar, dir, prefix); } }
Example #2
Source File: ActivityClassGenerator.java From AndroidPlugin with MIT License | 6 votes |
public static void createActivityDex(String superClassName, String targetClassName, File saveTo, String pluginId, String pkgName) throws IOException { byte[] dex = createActivityDex(superClassName, targetClassName, pluginId, pkgName); if (saveTo.getName().endsWith(".dex")) { FileUtil.writeToFile(dex, saveTo); } else { JarOutputStream jarOut = new JarOutputStream(new FileOutputStream( saveTo)); jarOut.putNextEntry(new JarEntry(DexFormat.DEX_IN_JAR_NAME)); jarOut.write(dex); jarOut.closeEntry(); jarOut.close(); } }
Example #3
Source File: JarUtils.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * Create jar file with specified files. If a specified file does not exist, * a new jar entry will be created with the file name itself as the content. */ public static void createJar(String dest, String... files) throws IOException { try (JarOutputStream jos = new JarOutputStream( new FileOutputStream(dest), new Manifest())) { for (String file : files) { System.out.println(String.format("Adding %s to %s", file, dest)); // add an archive entry, and write a file jos.putNextEntry(new JarEntry(file)); try (FileInputStream fis = new FileInputStream(file)) { fis.transferTo(jos); } catch (FileNotFoundException e) { jos.write(file.getBytes()); } } } System.out.println(); }
Example #4
Source File: MergeAndShadeTransformer.java From metron with Apache License 2.0 | 6 votes |
/** * Merges two jars. The first jar will get merged into the output jar. * A running set of jar entries is kept so that duplicates are skipped. * This has the side-effect that the first instance of a given entry will be added * and all subsequent entries are skipped. * * @param jin The input jar * @param jout The output jar * @param entries The set of existing entries. Note that this set will be mutated as part of this call. * @return The set of entries. * @throws IOException */ private Set<String> copy(JarInputStream jin, JarOutputStream jout, Set<String> entries) throws IOException { byte[] buffer = new byte[1024]; for(JarEntry entry = jin.getNextJarEntry(); entry != null; entry = jin.getNextJarEntry()) { if(entries.contains(entry.getName())) { continue; } LOG.debug("Merging jar entry {}", entry.getName()); entries.add(entry.getName()); jout.putNextEntry(entry); int len = 0; while( (len = jin.read(buffer)) > 0 ) { jout.write(buffer, 0, len); } } return entries; }
Example #5
Source File: MainTest.java From turbine with Apache License 2.0 | 6 votes |
@Test public void sourceJarClash() throws IOException { Path sourcesa = temporaryFolder.newFile("sourcesa.jar").toPath(); try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(sourcesa))) { jos.putNextEntry(new JarEntry("Test.java")); jos.write("class Test { public static final String CONST = \"ONE\"; }".getBytes(UTF_8)); } Path sourcesb = temporaryFolder.newFile("sourcesb.jar").toPath(); try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(sourcesb))) { jos.putNextEntry(new JarEntry("Test.java")); jos.write("class Test { public static final String CONST = \"TWO\"; }".getBytes(UTF_8)); } Path output = temporaryFolder.newFile("output.jar").toPath(); try { Main.compile( optionsWithBootclasspath() .setSourceJars(ImmutableList.of(sourcesa.toString(), sourcesb.toString())) .setOutput(output.toString()) .build()); fail(); } catch (TurbineError e) { assertThat(e).hasMessageThat().contains("error: duplicate declaration of Test"); } }
Example #6
Source File: ByteClassLoader.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Make a new ByteClassLoader. * * @param jar_name Basename of jar file to be read/written by this classloader. * @param read If true, read classes from jar file instead of from parameter. * @param write If true, write classes to jar files for offline study/use. * * @throws FileNotFoundException * @throws IOException */ public ByteClassLoader(String jar_name, boolean read, boolean write) throws FileNotFoundException, IOException { super(read ? new URL[]{new URL("file:" + jar_name + ".jar")} : new URL[0]); this.read = read; this.jar_name = jar_name; this.jos = write ? new JarOutputStream( new BufferedOutputStream( new FileOutputStream(jar_name + ".jar"))) : null; if (read && write) { throw new Error("At most one of read and write may be true."); } }
Example #7
Source File: JarUtils.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * Create jar file with specified files. If a specified file does not exist, * a new jar entry will be created with the file name itself as the content. */ public static void createJar(String dest, String... files) throws IOException { try (JarOutputStream jos = new JarOutputStream( new FileOutputStream(dest), new Manifest())) { for (String file : files) { System.out.println(String.format("Adding %s to %s", file, dest)); // add an archive entry, and write a file jos.putNextEntry(new JarEntry(file)); try (FileInputStream fis = new FileInputStream(file)) { Utils.transferTo(fis, jos); } catch (FileNotFoundException e) { jos.write(file.getBytes()); } } } System.out.println(); }
Example #8
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
private File writeJarFile(String name, JEntry... entries) throws IOException { File f = new File(getWorkDir(), name); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(f)); try { for (JEntry entry : entries) { JarEntry jarEntry = new JarEntry(entry.name); jarOut.putNextEntry(jarEntry); jarOut.write(entry.content.getBytes("UTF-8")); jarOut.closeEntry(); } } finally { jarOut.close(); } return f; }
Example #9
Source File: ModuleFormatSatisfiedTest.java From netbeans with Apache License 2.0 | 6 votes |
@Override protected void setUp() throws Exception { super.setUp(); System.setProperty("org.netbeans.core.modules.NbInstaller.noAutoDeps", "true"); Manifest man = new Manifest (); man.getMainAttributes ().putValue ("Manifest-Version", "1.0"); man.getMainAttributes ().putValue ("OpenIDE-Module", "org.test.FormatDependency/1"); String req = "org.openide.modules.ModuleFormat1, org.openide.modules.ModuleFormat2"; man.getMainAttributes ().putValue ("OpenIDE-Module-Requires", req); clearWorkDir(); moduleJarFile = new File(getWorkDir(), "ModuleFormatTest.jar"); JarOutputStream os = new JarOutputStream(new FileOutputStream(moduleJarFile), man); os.putNextEntry (new JarEntry ("empty/test.txt")); os.close (); }
Example #10
Source File: PackTestZip64.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
static void generateLargeJar(File result, File source) throws IOException { if (result.exists()) { result.delete(); } try (JarOutputStream copyTo = new JarOutputStream(new FileOutputStream(result)); JarFile srcJar = new JarFile(source)) { for (JarEntry je : Collections.list(srcJar.entries())) { copyTo.putNextEntry(je); if (!je.isDirectory()) { copyStream(srcJar.getInputStream(je), copyTo); } copyTo.closeEntry(); } int many = Short.MAX_VALUE * 2 + 2; for (int i = 0 ; i < many ; i++) { JarEntry e = new JarEntry("F-" + i + ".txt"); copyTo.putNextEntry(e); } copyTo.flush(); copyTo.close(); } }
Example #11
Source File: ByteClassLoader.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * Make a new ByteClassLoader. * * @param jar_name Basename of jar file to be read/written by this classloader. * @param read If true, read classes from jar file instead of from parameter. * @param write If true, write classes to jar files for offline study/use. * * @throws FileNotFoundException * @throws IOException */ public ByteClassLoader(String jar_name, boolean read, boolean write) throws FileNotFoundException, IOException { super(read ? new URL[]{new URL("file:" + jar_name + ".jar")} : new URL[0]); this.read = read; this.jar_name = jar_name; this.jos = write ? new JarOutputStream( new BufferedOutputStream( new FileOutputStream(jar_name + ".jar"))) : null; if (read && write) { throw new Error("At most one of read and write may be true."); } }
Example #12
Source File: SimpleServiceLoaderTest.java From auto with Apache License 2.0 | 6 votes |
private static URL urlForJarWithEntries(String service, String... lines) throws IOException { File jar = File.createTempFile("SimpleServiceLoaderTest", "jar"); jar.deleteOnExit(); try (JarOutputStream out = new JarOutputStream(new FileOutputStream(jar))) { JarEntry jarEntry = new JarEntry("META-INF/services/" + service); out.putNextEntry(jarEntry); // It would be bad practice to use try-with-resources below, because closing the PrintWriter // would close the JarOutputStream. PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)); for (String line : lines) { writer.println(line); } writer.flush(); } return jar.toURI().toURL(); }
Example #13
Source File: DefineClass.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private static void loadInstrumentationAgent(String myName, byte[] buf) throws Exception { // Create agent jar file on the fly Manifest m = new Manifest(); m.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); m.getMainAttributes().put(new Attributes.Name("Agent-Class"), myName); m.getMainAttributes().put(new Attributes.Name("Can-Redefine-Classes"), "true"); File jarFile = File.createTempFile("agent", ".jar"); jarFile.deleteOnExit(); JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarFile), m); jar.putNextEntry(new JarEntry(myName.replace('.', '/') + ".class")); jar.write(buf); jar.close(); String pid = Long.toString(ProcessTools.getProcessId()); System.out.println("Our pid is = " + pid); VirtualMachine vm = VirtualMachine.attach(pid); vm.loadAgent(jarFile.getAbsolutePath()); }
Example #14
Source File: RedefineIntrinsicTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Adds the class file bytes for a given class to a JAR stream. */ static void add(JarOutputStream jar, Class<?> c) throws IOException { String name = c.getName(); String classAsPath = name.replace('.', '/') + ".class"; jar.putNextEntry(new JarEntry(classAsPath)); InputStream stream = c.getClassLoader().getResourceAsStream(classAsPath); int nRead; byte[] buf = new byte[1024]; while ((nRead = stream.read(buf, 0, buf.length)) != -1) { jar.write(buf, 0, nRead); } jar.closeEntry(); }
Example #15
Source File: Utils.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException { if (in.getManifest() != null) { ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME); out.putNextEntry(me); in.getManifest().write(out); out.closeEntry(); } byte[] buffer = new byte[1 << 14]; for (JarEntry je; (je = in.getNextJarEntry()) != null; ) { out.putNextEntry(je); for (int nr; 0 < (nr = in.read(buffer)); ) { out.write(buffer, 0, nr); } } in.close(); markJarFile(out); // add PACK200 comment }
Example #16
Source File: IResultSaverImpl.java From bistoury with GNU General Public License v3.0 | 6 votes |
@Override public void createArchive(String path, String archiveName, Manifest manifest) { File file = new File(getAbsolutePath(path), archiveName); try { if (!(file.createNewFile() || file.isFile())) { throw new IOException("Cannot create file " + file); } FileOutputStream fileStream = new FileOutputStream(file); @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") ZipOutputStream zipStream = manifest != null ? new JarOutputStream(fileStream, manifest) : new ZipOutputStream(fileStream); mapArchiveStreams.put(file.getPath(), zipStream); } catch (IOException ex) { DecompilerContext.getLogger().writeMessage("Cannot create archive " + file, ex); } }
Example #17
Source File: NonIncrementalJarDexArchiveHook.java From atlas with Apache License 2.0 | 6 votes |
public NonIncrementalJarDexArchiveHook(Path targetPath) throws IOException { this.targetPath = targetPath; if (Files.isRegularFile(targetPath)) { // we should read this file this.readOnlyZipFile = new ZipFile(targetPath.toFile()); } else { // we are creating this file this.jarOutputStream = new JarOutputStream( new BufferedOutputStream( Files.newOutputStream( targetPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW))); } }
Example #18
Source File: YarnTwillPreparer.java From twill with Apache License 2.0 | 6 votes |
private void createRuntimeConfigJar(Path dir, Map<String, LocalFile> localFiles) throws IOException { LOG.debug("Create and copy {}", Constants.Files.RUNTIME_CONFIG_JAR); // Jar everything under the given directory, which contains different files needed by AM/runnable containers Location location = createTempLocation(Constants.Files.RUNTIME_CONFIG_JAR); try ( JarOutputStream jarOutput = new JarOutputStream(location.getOutputStream()); DirectoryStream<Path> stream = Files.newDirectoryStream(dir) ) { for (Path path : stream) { jarOutput.putNextEntry(new JarEntry(path.getFileName().toString())); Files.copy(path, jarOutput); jarOutput.closeEntry(); } } LOG.debug("Done {}", Constants.Files.RUNTIME_CONFIG_JAR); localFiles.put(Constants.Files.RUNTIME_CONFIG_JAR, createLocalFile(Constants.Files.RUNTIME_CONFIG_JAR, location, true)); }
Example #19
Source File: NetigsoLayerTest.java From netbeans with Apache License 2.0 | 6 votes |
private File changeManifest(File orig, String manifest) throws IOException { File f = new File(getWorkDir(), orig.getName()); Manifest mf = new Manifest(new ByteArrayInputStream(manifest.getBytes("utf-8"))); mf.getMainAttributes().putValue("Manifest-Version", "1.0"); JarOutputStream os = new JarOutputStream(new FileOutputStream(f), mf); JarFile jf = new JarFile(orig); Enumeration<JarEntry> en = jf.entries(); InputStream is; while (en.hasMoreElements()) { JarEntry e = en.nextElement(); if (e.getName().equals("META-INF/MANIFEST.MF")) { continue; } os.putNextEntry(e); is = jf.getInputStream(e); FileUtil.copy(is, os); is.close(); os.closeEntry(); } os.close(); return f; }
Example #20
Source File: Utils.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 5 votes |
private static void unpack0(File inFile, JarOutputStream jarStream, boolean useJavaUnpack) throws IOException { // Unpack the files Pack200.Unpacker unpacker = Pack200.newUnpacker(); Map<String, String> props = unpacker.properties(); if (useJavaUnpack) { props.put("com.sun.java.util.jar.pack.disable.native", "true"); } // Call the unpacker unpacker.unpack(inFile, jarStream); }
Example #21
Source File: FileUtils.java From jadx with Apache License 2.0 | 5 votes |
public static void addFileToJar(JarOutputStream jar, File source, String entryName) throws IOException { try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source))) { JarEntry entry = new JarEntry(entryName); entry.setTime(source.lastModified()); jar.putNextEntry(entry); copyStream(in, jar); jar.closeEntry(); } }
Example #22
Source File: ExecMojo.java From exec-maven-plugin with Apache License 2.0 | 5 votes |
/** * Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for all * classpath elements. Copied from surefire (ForkConfiguration#createJar()) * * @param classPath List<String> of all classpath elements. * @return * @throws IOException */ private File createJar( List<String> classPath, String mainClass ) throws IOException { File file = File.createTempFile( "maven-exec", ".jar" ); file.deleteOnExit(); FileOutputStream fos = new FileOutputStream( file ); JarOutputStream jos = new JarOutputStream( fos ); jos.setLevel( JarOutputStream.STORED ); JarEntry je = new JarEntry( "META-INF/MANIFEST.MF" ); jos.putNextEntry( je ); Manifest man = new Manifest(); // we can't use StringUtils.join here since we need to add a '/' to // the end of directory entries - otherwise the jvm will ignore them. StringBuilder cp = new StringBuilder(); for ( String el : classPath ) { // NOTE: if File points to a directory, this entry MUST end in '/'. cp.append( new URL( new File( el ).toURI().toASCIIString() ).toExternalForm() + " " ); } man.getMainAttributes().putValue( "Manifest-Version", "1.0" ); man.getMainAttributes().putValue( "Class-Path", cp.toString().trim() ); man.getMainAttributes().putValue( "Main-Class", mainClass ); man.write( jos ); jos.close(); return file; }
Example #23
Source File: LocalFileTestRun.java From twill with Apache License 2.0 | 5 votes |
public LocalFileApplication(File headerFile) throws Exception { // Create a jar file that contains the header.txt file inside. headerJar = tmpFolder.newFile("header.jar"); try (JarOutputStream os = new JarOutputStream(new FileOutputStream(headerJar))) { os.putNextEntry(new JarEntry(headerFile.getName())); Files.copy(headerFile, os); } }
Example #24
Source File: UpdateWarTask.java From lams with GNU General Public License v2.0 | 5 votes |
/** This entry in the JAR/WAR file doesn't change so copy it from the old archive to the new archive */ protected void copyEntryToWar(JarOutputStream newWarOutputStream, ZipInputStream warInputStream, ZipEntry entry) throws IOException { ZipEntry newEntry = new ZipEntry(entry.getName()); newWarOutputStream.putNextEntry(newEntry); byte[] data = new byte[BUFFER_SIZE]; int count = -1; while ((count = warInputStream.read(data, 0, BUFFER_SIZE)) != -1) { newWarOutputStream.write(data, 0, count); } newWarOutputStream.closeEntry(); }
Example #25
Source File: ClassBuilder.java From gemfirexd-oss with Apache License 2.0 | 5 votes |
/** * Create a JAR using the given file contents and with the given file name. * * @param fileName * Name of the file to create * @param content * Content of the created file * @return The JAR file content * @throws IOException * If there is a problem creating the output stream for the JAR file. */ public byte[] createJarFromFileContent(final String fileName, final String content) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); JarOutputStream jarOutputStream = new JarOutputStream(byteArrayOutputStream); JarEntry entry = new JarEntry(fileName); entry.setTime(System.currentTimeMillis()); jarOutputStream.putNextEntry(entry); jarOutputStream.write(content.getBytes()); jarOutputStream.closeEntry(); jarOutputStream.close(); return byteArrayOutputStream.toByteArray(); }
Example #26
Source File: URLMapperTest.java From netbeans with Apache License 2.0 | 5 votes |
/** * Check that jar: URLs are correctly mapped back into JarFileSystem resources. * @see "#39190" */ public void testJarMapping() throws Exception { clearWorkDir(); File workdir = getWorkDir(); File jar = new File(workdir, "test.jar"); String textPath = "x.txt"; OutputStream os = new FileOutputStream(jar); try { JarOutputStream jos = new JarOutputStream(os); jos.setMethod(ZipEntry.STORED); JarEntry entry = new JarEntry(textPath); entry.setSize(0L); entry.setTime(System.currentTimeMillis()); entry.setCrc(new CRC32().getValue()); jos.putNextEntry(entry); jos.flush(); jos.close(); } finally { os.close(); } assertTrue("JAR was created", jar.isFile()); assertTrue("JAR is not empty", jar.length() > 0L); JarFileSystem jfs = new JarFileSystem(); jfs.setJarFile(jar); Repository.getDefault().addFileSystem(jfs); FileObject rootFO = jfs.getRoot(); FileObject textFO = jfs.findResource(textPath); assertNotNull("JAR contains a/b.txt", textFO); String rootS = "jar:" + BaseUtilities.toURI(jar) + "!/"; URL rootU = new URL(rootS); URL textU = new URL(rootS + textPath); assertEquals("correct FO -> URL for root", rootU, URLMapper.findURL(rootFO, URLMapper.EXTERNAL)); assertEquals("correct FO -> URL for " + textPath, textU, URLMapper.findURL(textFO, URLMapper.EXTERNAL)); assertTrue("correct URL -> FO for root", Arrays.asList(URLMapper.findFileObjects(rootU)).contains(rootFO)); assertTrue("correct URL -> FO for " + textPath, Arrays.asList(URLMapper.findFileObjects(textU)).contains(textFO)); }
Example #27
Source File: IoTestUtil.java From consulo with Apache License 2.0 | 5 votes |
public static void writeEntry(@Nonnull File jarFile, @Nonnull String name, @Nonnull String content) throws IOException { JarOutputStream stream = new JarOutputStream(new FileOutputStream(jarFile)); try { stream.putNextEntry(new JarEntry(name)); stream.write(content.getBytes("UTF-8")); stream.closeEntry(); } finally { stream.close(); } }
Example #28
Source File: TaobaoInstantRunDex.java From atlas with Apache License 2.0 | 5 votes |
private static void copyFileInJar(File inputDir, File inputFile, JarOutputStream jarOutputStream) throws IOException { String entryName = inputFile.getPath().substring(inputDir.getPath().length() + 1); JarEntry jarEntry = new JarEntry(entryName); jarOutputStream.putNextEntry(jarEntry); Files.copy(inputFile, jarOutputStream); jarOutputStream.closeEntry(); }
Example #29
Source File: TestJarFile.java From ArchUnit with Apache License 2.0 | 5 votes |
JarFile create(File jarFile) { try (JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest)) { for (String entry : entries) { write(jarOut, entry); } } catch (IOException e) { throw new RuntimeException(e); } return newJarFile(jarFile); }
Example #30
Source File: Utils.java From samoa with Apache License 2.0 | 5 votes |
private static void addEntries(JarOutputStream jo, File[] files, String baseDir, String rootDir){ for (File file : files) { if (!file.isDirectory()) { addEntry(jo, file, baseDir, rootDir); } else { File dir = new File(file.getAbsolutePath()); addEntries(jo, dir.listFiles(), baseDir, rootDir); } } }