java.util.jar.JarEntry Java Examples
The following examples show how to use
java.util.jar.JarEntry.
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: URLClassPath.java From Bytecoder with Apache License 2.0 | 6 votes |
@Override Resource getResource(final String name, boolean check) { try { ensureOpen(); } catch (IOException e) { throw new InternalError(e); } final JarEntry entry = jar.getJarEntry(name); if (entry != null) return checkResource(name, check, entry); if (index == null) return null; HashSet<String> visited = new HashSet<>(); return getResource(name, check, visited); }
Example #2
Source File: IjarTests.java From bazel with Apache License 2.0 | 6 votes |
@Test public void testNoStripJarWithoutManifest() throws Exception { JarFile original = new JarFile("third_party/ijar/test/jar-without-manifest.jar"); JarFile stripped = new JarFile("third_party/ijar/test/jar-without-manifest-nostrip.jar"); try { ImmutableList<String> strippedEntries = stripped.stream().map(JarEntry::getName).collect(toImmutableList()); assertThat(strippedEntries.get(0)).isEqualTo("META-INF/"); assertThat(strippedEntries.get(1)).isEqualTo("META-INF/MANIFEST.MF"); Manifest manifest = stripped.getManifest(); Attributes attributes = manifest.getMainAttributes(); assertThat(attributes.getValue("Manifest-Version")).isEqualTo("1.0"); assertThat(attributes.getValue("Created-By")).isEqualTo("bazel"); assertThat(attributes.getValue("Target-Label")).isEqualTo("//foo:foo"); assertNonManifestFilesBitIdentical(original, stripped); } finally { original.close(); stripped.close(); } }
Example #3
Source File: ClassReader.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static void doJar(String a, File source, File dest, ClassReader options, String encoding, Boolean contError) throws IOException { try { JarFile jf = new JarFile(source); for (JarEntry je : Collections.list(jf.entries())) { String name = je.getName(); if (!name.endsWith(".class")) { continue; } try { doStream(name, jf.getInputStream(je), dest, options, encoding); } catch (Exception e) { if (contError) { System.out.println("Error processing " + source + ": " + e); e.printStackTrace(); continue; } } } } catch (IOException ioe) { throw ioe; } }
Example #4
Source File: JarUtils.java From workcraft with MIT License | 6 votes |
private static Set<String> getResourcePaths(String path, URL dirUrl) throws URISyntaxException, IOException { HashSet<String> result = new HashSet<>(); String protocol = dirUrl.getProtocol(); if ("file".equals(protocol)) { File dir = new File(dirUrl.toURI()); for (String fileName: dir.list()) { result.add(path + fileName); } } else if ("jar".equals(protocol)) { String dirPath = dirUrl.getPath(); String jarPath = dirPath.substring(5, dirPath.indexOf("!")); JarFile jarFile = new JarFile(jarPath); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { String entryName = entries.nextElement().getName(); if ((entryName.length() > path.length()) && entryName.startsWith(path)) { result.add(entryName); } } jarFile.close(); } return result; }
Example #5
Source File: AbstractArchiveResource.java From Tomcat8-Source-Read with MIT License | 6 votes |
protected AbstractArchiveResource(AbstractArchiveResourceSet archiveResourceSet, String webAppPath, String baseUrl, JarEntry jarEntry, String codeBaseUrl) { super(archiveResourceSet.getRoot(), webAppPath); this.archiveResourceSet = archiveResourceSet; this.baseUrl = baseUrl; this.resource = jarEntry; this.codeBaseUrl = codeBaseUrl; String resourceName = resource.getName(); if (resourceName.charAt(resourceName.length() - 1) == '/') { resourceName = resourceName.substring(0, resourceName.length() - 1); } String internalPath = archiveResourceSet.getInternalPath(); if (internalPath.length() > 0 && resourceName.equals( internalPath.subSequence(1, internalPath.length()))) { name = ""; } else { int index = resourceName.lastIndexOf('/'); if (index == -1) { name = resourceName; } else { name = resourceName.substring(index + 1); } } }
Example #6
Source File: JarMergingTask.java From javaide with GNU General Public License v3.0 | 6 votes |
private void processFolder(JarOutputStream jos, String path, File folder, byte[] buffer) throws IOException { for (File file : folder.listFiles()) { if (file.isFile()) { // new entry jos.putNextEntry(new JarEntry(path + file.getName())); // put the file content FileInputStream fis = new FileInputStream(file); int count; while ((count = fis.read(buffer)) != -1) { jos.write(buffer, 0, count); } fis.close(); // close the entry jos.closeEntry(); } else if (file.isDirectory()) { processFolder(jos, path + file.getName() + "/", file, buffer); } } }
Example #7
Source File: BootJarLaucherUtils.java From tac with MIT License | 6 votes |
/** * * @param jarFile * @param entry * @param file * @throws IOException */ private static void unpack(JarFile jarFile, JarEntry entry, File file) throws IOException { InputStream inputStream = jarFile.getInputStream(entry, RandomAccessData.ResourceAccess.ONCE); try { OutputStream outputStream = new FileOutputStream(file); try { byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.flush(); } finally { outputStream.close(); } } finally { inputStream.close(); } }
Example #8
Source File: MainTest.java From turbine with Apache License 2.0 | 6 votes |
@Test public void packageInfoSrcjar() throws IOException { Path srcjar = temporaryFolder.newFile("lib.srcjar").toPath(); try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(srcjar))) { jos.putNextEntry(new JarEntry("package-info.java")); jos.write("@Deprecated package test;".getBytes(UTF_8)); } Path output = temporaryFolder.newFile("output.jar").toPath(); Main.compile( optionsWithBootclasspath() .setSourceJars(ImmutableList.of(srcjar.toString())) .setOutput(output.toString()) .build()); Map<String, byte[]> data = readJar(output); assertThat(data.keySet()).containsExactly("test/package-info.class"); }
Example #9
Source File: ClassReader.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private static void doJar(String a, File source, File dest, ClassReader options, String encoding, Boolean contError) throws IOException { try { JarFile jf = new JarFile(source); for (JarEntry je : Collections.list(jf.entries())) { String name = je.getName(); if (!name.endsWith(".class")) { continue; } try { doStream(name, jf.getInputStream(je), dest, options, encoding); } catch (Exception e) { if (contError) { System.out.println("Error processing " + source + ": " + e); e.printStackTrace(); continue; } } } } catch (IOException ioe) { throw ioe; } }
Example #10
Source File: ClassScanner.java From gadtry with Apache License 2.0 | 6 votes |
private static Set<String> scanJarClass(String packagePath, URL url) throws IOException { JarFile jarFile = ((JarURLConnection) url.openConnection()).getJarFile(); Set<String> classSet = new HashSet<>(); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (name.charAt(0) == '/') { name = name.substring(1); } if (!name.startsWith(packagePath)) { continue; } if (name.endsWith(".class") && !entry.isDirectory()) { classSet.add(name); } } return classSet; }
Example #11
Source File: GFECompatibilityTest.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
private List<Class> getRegionEntryClassesFromJar(String jarFile, String pkg) throws Exception { List<Class> regionEntryClasses = new ArrayList<Class>(); JarFile gfJar = new JarFile(jarFile, true); Enumeration<JarEntry> enm = gfJar.entries(); while (enm.hasMoreElements()) { JarEntry je = enm.nextElement(); String name = je.getName().replace('/', '.'); if (name.startsWith(pkg) && name.endsWith(".class")) { Class jeClass = Class.forName(name.replaceAll(".class", "")); if (!jeClass.isInterface() && RegionEntry.class.isAssignableFrom(jeClass) && !isInExclusionList(jeClass)) { int modifiers = jeClass.getModifiers(); if ((modifiers & Modifier.ABSTRACT) == 0) { regionEntryClasses.add(jeClass); } } } } return regionEntryClasses; }
Example #12
Source File: Task.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
static List<Class<?>> getClasses(int count) throws Exception { String resource = ClassLoader.getSystemClassLoader().getResource("java/lang/Object.class").toString(); Pattern pattern = Pattern.compile("jar:file:(.*)!.*"); Matcher matcher = pattern.matcher(resource); matcher.matches(); resource = matcher.group(1); List<Class<?>> classes = new ArrayList<>(); try (JarFile jarFile = new JarFile(resource)) { Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { String name = entries.nextElement().getName(); if (name.startsWith("java") && name.endsWith(".class")) { classes.add(Class.forName(name.substring(0, name.indexOf(".")).replace('/', '.'))); if (count == classes.size()) { break; } } } } return classes; }
Example #13
Source File: Utils.java From watcher with Apache License 2.0 | 6 votes |
private static Set<URL> findPathMatchingJarResources(URL relativeUrl) throws IOException { URL rootUrl = new URL(relativeUrl, "/"); //在jar包中的相对路径 URLConnection con = relativeUrl.openConnection(); JarURLConnection jarCon = (JarURLConnection) con; JarFile jarFile = jarCon.getJarFile(); JarEntry jarEntry = jarCon.getJarEntry(); String rootEntryPath = (jarEntry != null ? jarEntry.getName() : ""); if (!"".equals(rootEntryPath) && !rootEntryPath.endsWith("/")) { rootEntryPath = rootEntryPath + "/"; } Set<URL> result = new LinkedHashSet<>(8); for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); String entryPath = entry.getName(); if (entryPath.startsWith(rootEntryPath)) { if (entryPath.startsWith("/")) { entryPath = entryPath.substring(1); } result.add(new URL(rootUrl, entryPath)); } } return result; }
Example #14
Source File: ExternalLibrary.java From bytecode-viewer with GNU General Public License v3.0 | 6 votes |
@Override public JarContents<ClassNode> load() throws IOException { JarContents<ClassNode> contents = new JarContents<ClassNode>(); JarURLConnection con = (JarURLConnection) getLocation().openConnection(); JarFile jar = con.getJarFile(); Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); byte[] bytes = read(jar.getInputStream(entry)); if (entry.getName().endsWith(".class")) { ClassNode cn = create(bytes); contents.getClassContents().add(cn); } else { JarResource resource = new JarResource(entry.getName(), bytes); contents.getResourceContents().add(resource); } } return contents; }
Example #15
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 #16
Source File: ModulePackager.java From snap-desktop with GNU General Public License v3.0 | 6 votes |
/** * Adds a compiled class file to the target jar stream. * * @param fromClass The class to be added * @param target The target jar stream */ private static void addFile(Class fromClass, JarOutputStream target) throws IOException { String classEntry = fromClass.getName().replace('.', '/') + ".class"; URL classURL = fromClass.getClassLoader().getResource(classEntry); if (classURL != null) { JarEntry entry = new JarEntry(classEntry); target.putNextEntry(entry); if (!classURL.toString().contains("!")) { String fileName = classURL.getFile(); writeBytes(fileName, target); } else { try (InputStream stream = fromClass.getClassLoader().getResourceAsStream(classEntry)) { writeBytes(stream, target); } } target.closeEntry(); } }
Example #17
Source File: JarModifier.java From JReFrameworker with MIT License | 6 votes |
/** * Extracts a Jar file * * @param inputJar * @param outputPath * @throws IOException */ public static void unjar(File inputJar, File outputPath) throws IOException { outputPath.mkdirs(); JarFile jar = new JarFile(inputJar); Enumeration<JarEntry> jarEntries = jar.entries(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); File file = new File(outputPath.getAbsolutePath() + java.io.File.separator + jarEntry.getName()); new File(file.getParent()).mkdirs(); if (jarEntry.isDirectory()) { file.mkdir(); continue; } InputStream is = jar.getInputStream(jarEntry); FileOutputStream fos = new FileOutputStream(file); while (is.available() > 0) { fos.write(is.read()); } fos.close(); is.close(); } jar.close(); }
Example #18
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 #19
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 #20
Source File: URLClassPath.java From javaide with GNU General Public License v3.0 | 6 votes |
Resource getResource(final String name, boolean check) { if (metaIndex != null) { if (!metaIndex.mayContain(name)) { return null; } } try { ensureOpen(); } catch (IOException e) { throw (InternalError) new InternalError().initCause(e); } final JarEntry entry = jar.getJarEntry(name); if (entry != null) return checkResource(name, check, entry); if (index == null) return null; HashSet visited = new HashSet(); return getResource(name, check, visited); }
Example #21
Source File: TestIndexedJarWithBadSignature.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
public static void main(String...args) throws Throwable { try (JarInputStream jis = new JarInputStream( new FileInputStream(System.getProperty("test.src", ".") + System.getProperty("file.separator") + "BadSignedJar.jar"))) { JarEntry je1 = jis.getNextJarEntry(); while(je1!=null){ System.out.println("Jar Entry1==>"+je1.getName()); je1 = jis.getNextJarEntry(); // This should throw Security Exception } throw new RuntimeException( "Test Failed:Security Exception not being thrown"); } catch (IOException ie){ ie.printStackTrace(); } catch (SecurityException e) { System.out.println("Test passed: Security Exception thrown as expected"); } }
Example #22
Source File: JarFileShadingIT.java From glowroot with Apache License 2.0 | 6 votes |
@Test public void shouldCheckThatJarIsWellShaded() throws IOException { File glowrootCoreJarFile = getGlowrootAgentJarFile(); List<String> acceptableEntries = Lists.newArrayList(); acceptableEntries.add("glowroot\\..*"); acceptableEntries.add("org/"); acceptableEntries.add("org/glowroot/"); acceptableEntries.add("org/glowroot/agent/.*"); acceptableEntries.add("META-INF/"); acceptableEntries.add("META-INF/glowroot\\..*"); acceptableEntries.add("META-INF/services/"); acceptableEntries.add("META-INF/services/org\\.glowroot\\..*"); acceptableEntries.add("META-INF/MANIFEST\\.MF"); acceptableEntries.add("META-INF/LICENSE"); acceptableEntries.add("META-INF/NOTICE"); JarFile jarFile = new JarFile(glowrootCoreJarFile); List<String> unacceptableEntries = Lists.newArrayList(); for (Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements();) { JarEntry jarEntry = e.nextElement(); if (!acceptableJarEntry(jarEntry, acceptableEntries)) { unacceptableEntries.add(jarEntry.getName()); } } jarFile.close(); assertThat(unacceptableEntries).isEmpty(); }
Example #23
Source File: ClassCodeLoader.java From nuls-v2 with MIT License | 6 votes |
private static Map<String, ClassCode> loadJar(JarInputStream jarInputStream) { Map<String, ClassCode> map = new HashMap<>(100); try (jarInputStream) { JarEntry jarEntry; while ((jarEntry = jarInputStream.getNextJarEntry()) != null) { if (!jarEntry.isDirectory() && jarEntry.getName().endsWith(Constants.CLASS_SUFFIX)) { byte[] bytes = IOUtils.toByteArray(jarInputStream); ClassCode classCode = load(bytes); map.put(classCode.name, classCode); } } } catch (IOException e) { throw new RuntimeException(e); } return map; }
Example #24
Source File: ResourcesUtils.java From hasor with Apache License 2.0 | 5 votes |
/**创建{@link ScanEvent}*/ ScanEvent(final String name, final JarEntry entry, final InputStream stream) { this.isRead = !entry.isDirectory(); this.isWrite = false; this.stream = stream; this.name = name; }
Example #25
Source File: Util.java From Bytecoder with Apache License 2.0 | 5 votes |
/** * Verifies whether the file resource exists. * * @param uri the URI to locate the resource * @param openJarFile a flag to indicate whether a JAR file should be * opened. This operation may be expensive. * @return true if the resource exists, false otherwise. */ static boolean isFileUriExist(URI uri, boolean openJarFile) { if (uri != null && uri.isAbsolute()) { if (null != uri.getScheme()) { switch (uri.getScheme()) { case SCHEME_FILE: String path = uri.getPath(); File f1 = new File(path); if (f1.isFile()) { return true; } break; case SCHEME_JAR: String tempUri = uri.toString(); int pos = tempUri.indexOf("!"); if (pos < 0) { return false; } if (openJarFile) { String jarFile = tempUri.substring(SCHEME_JARFILE.length(), pos); String entryName = tempUri.substring(pos + 2); try { JarFile jf = new JarFile(jarFile); JarEntry je = jf.getJarEntry(entryName); if (je != null) { return true; } } catch (IOException ex) { return false; } } else { return true; } break; } } } return false; }
Example #26
Source File: JarBackSlash.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
private static File createJarFile() throws IOException { File jarFile = File.createTempFile("JarBackSlashTest", ".jar"); jarFile.deleteOnExit(); try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) { JarEntry entry = new JarEntry(JARBACKSLASH + "/" + DIR + "/" + FILENAME); output.putNextEntry(entry); } return jarFile; }
Example #27
Source File: JarUtils.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * Update content of a jar file. * * @param src the original jar file name * @param dest the new jar file name * @param changes a map of changes, key is jar entry name, value is content. * Value can be Path, byte[] or String. If key exists in * src but value is Boolean FALSE. The entry is removed. * Existing entries in src not a key is unmodified. * @throws IOException */ public static void updateJar(String src, String dest, Map<String,Object> changes) throws IOException { // What if input changes is immutable? changes = new HashMap<>(changes); System.out.printf("Creating %s from %s...\n", dest, src); if (dest.equals(src)) { throw new IOException("src and dest cannot be the same"); } try (JarOutputStream jos = new JarOutputStream( new FileOutputStream(dest))) { try (JarFile srcJarFile = new JarFile(src)) { Enumeration<JarEntry> entries = srcJarFile.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); String name = entry.getName(); if (changes.containsKey(name)) { System.out.println(String.format("- Update %s", name)); updateEntry(jos, name, changes.get(name)); changes.remove(name); } else { System.out.println(String.format("- Copy %s", name)); jos.putNextEntry(entry); Utils.transferTo(srcJarFile.getInputStream(entry), jos); } } } for (Map.Entry<String, Object> e : changes.entrySet()) { System.out.println(String.format("- Add %s", e.getKey())); updateEntry(jos, e.getKey(), e.getValue()); } } System.out.println(); }
Example #28
Source File: ClassPath.java From codebuff with BSD 2-Clause "Simplified" License | 5 votes |
@Override protected void scanJarFile(ClassLoader classloader, JarFile file) { Enumeration<JarEntry> entries = file.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) { continue; } resources.get(classloader).add(entry.getName()); } }
Example #29
Source File: MakeJNLP.java From netbeans with Apache License 2.0 | 5 votes |
/** return alias if signed, or null if not */ private static String isSigned(File f) throws IOException { try (JarFile jar = new JarFile(f)) { Enumeration<JarEntry> en = jar.entries(); while (en.hasMoreElements()) { Matcher m = SF.matcher(en.nextElement().getName()); if (m.matches()) { return m.group(1); } } return null; } }
Example #30
Source File: ClassPathBinderTest.java From turbine with Apache License 2.0 | 5 votes |
@Test public void resources() throws Exception { Path path = temporaryFolder.newFile("tmp.jar").toPath(); try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(path))) { jos.putNextEntry(new JarEntry("foo/bar/hello.txt")); jos.write("hello".getBytes(UTF_8)); jos.putNextEntry(new JarEntry("foo/bar/Baz.class")); jos.write("goodbye".getBytes(UTF_8)); } ClassPath classPath = ClassPathBinder.bindClasspath(ImmutableList.of(path)); assertThat(new String(classPath.resource("foo/bar/hello.txt").get(), UTF_8)).isEqualTo("hello"); assertThat(classPath.resource("foo/bar/Baz.class")).isNull(); }