private JarFile createJarFileFromFileEntry(JarEntry entry) throws IOException { if (entry.getMethod() != ZipEntry.STORED) { throw new IllegalStateException("Unable to open nested entry '" + entry.getName() + "'. It has been compressed and nested " + "jar files must be stored without compression. Please check the " + "mechanism used to create your executable jar file"); } RandomAccessData entryData = this.entries.getEntryData(entry.getName()); return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName(), entryData, JarFileType.NESTED_JAR); }
private void setCertificates(JarEntry entry, java.util.jar.JarEntry certEntry) { if (entry != null) { entry.setCertificates(certEntry); } }
JarEntry(JarFile jarFile, CentralDirectoryFileHeader header, AsciiBytes nameAlias) { super((nameAlias != null) ? nameAlias.toString() : header.getName().toString()); this.name = (nameAlias != null) ? nameAlias : header.getName(); this.jarFile = jarFile; this.localHeaderOffset = header.getLocalHeaderOffset(); setCompressedSize(header.getCompressedSize()); setMethod(header.getMethod()); setCrc(header.getCrc()); setComment(header.getComment().toString()); setSize(header.getSize()); setTime(header.getTime()); setExtra(header.getExtra()); }
private JarFile createJarFileFromDirectoryEntry(JarEntry entry) throws IOException { AsciiBytes name = entry.getAsciiBytesName(); JarEntryFilter filter = (candidate) -> { if (candidate.startsWith(name) && !candidate.equals(name)) { return candidate.substring(name.length()); } return null; }; return new JarFile(this.rootFile, this.pathFromRoot + "!/" + entry.getName().substring(0, name.length() - 1), this.data, filter, JarFileType.NESTED_DIRECTORY, this.manifestSupplier); }
/** * Return a {@link URL} for this {@link JarEntry}. * @return the URL for the entry * @throws MalformedURLException if the URL is not valid */ URL getUrl() throws MalformedURLException { return new URL(this.jarFile.getUrl(), getName()); }
@SuppressWarnings("unchecked") private <T extends FileHeader> T getEntry(int index, Class<T> type, boolean cacheEntry, AsciiBytes nameAlias) { try { FileHeader cached = this.entriesCache.get(index); FileHeader entry = (cached != null) ? cached : CentralDirectoryFileHeader.fromRandomAccessData( this.centralDirectoryData, this.centralDirectoryOffsets[index], this.filter); if (CentralDirectoryFileHeader.class.equals(entry.getClass()) && type.equals(JarEntry.class)) { entry = new JarEntry(this.jarFile, (CentralDirectoryFileHeader) entry, nameAlias); } if (cacheEntry && cached != entry) { this.entriesCache.put(index, entry); } return (T) entry; } catch (IOException ex) { throw new IllegalStateException(ex); } }
@Override public long getLastModified() { if (this.jarFile == null || this.jarEntryName.isEmpty()) { return 0; } try { JarEntry entry = getJarEntry(); return (entry != null) ? entry.getTime() : 0; } catch (IOException ex) { return 0; } }
private JarFile createJarFileFromEntry(JarEntry entry) throws IOException { if (entry.isDirectory()) { return createJarFileFromDirectoryEntry(entry); } return createJarFileFromFileEntry(entry); }
@Override public long getContentLengthLong() { if (this.jarFile == null) { return -1; } try { if (this.jarEntryName.isEmpty()) { return this.jarFile.size(); } JarEntry entry = getJarEntry(); return (entry != null) ? (int) entry.getSize() : -1; } catch (IOException ex) { return -1; } }
private <T extends FileHeader> T getEntry(CharSequence name, Class<T> type, boolean cacheEntry) { T entry = doGetEntry(name, type, cacheEntry, null); if (!isMetaInfEntry(name) && isMultiReleaseJar()) { int version = RUNTIME_VERSION; AsciiBytes nameAlias = (entry instanceof JarEntry) ? ((JarEntry) entry).getAsciiBytesName() : new AsciiBytes(name.toString()); while (version > BASE_VERSION) { T versionedEntry = doGetEntry("META-INF/versions/" + version + "/" + name, type, cacheEntry, nameAlias); if (versionedEntry != null) { return versionedEntry; } version--; } } return entry; }
/** * Return a nested {@link JarFile} loaded from the specified entry. * @param entry the zip entry * @return a {@link JarFile} for the entry * @throws IOException if the nested jar file cannot be read */ public synchronized JarFile getNestedJarFile(JarEntry entry) throws IOException { try { return createJarFileFromEntry(entry); } catch (Exception ex) { throw new IOException( "Unable to open nested jar file '" + entry.getName() + "'", ex); } }
@Override public Attributes getAttributes() throws IOException { Manifest manifest = this.jarFile.getManifest(); return (manifest != null) ? manifest.getAttributes(getName()) : null; }
void setupEntryCertificates(JarEntry entry) { // Fallback to JarInputStream to obtain certificates, not fast but hopefully not // happening that often. try { try (JarInputStream inputStream = new JarInputStream( getData().getInputStream())) { java.util.jar.JarEntry certEntry = inputStream.getNextJarEntry(); while (certEntry != null) { inputStream.closeEntry(); if (entry.getName().equals(certEntry.getName())) { setCertificates(entry, certEntry); } setCertificates(getJarEntry(certEntry.getName()), certEntry); certEntry = inputStream.getNextJarEntry(); } } } catch (IOException ex) { throw new IllegalStateException(ex); } }