use of java.util.jar.JarInputStream in project jetty.project by eclipse.
the class JarResource method copyTo.
/* ------------------------------------------------------------ */
@Override
public void copyTo(File directory) throws IOException {
if (!exists())
return;
if (LOG.isDebugEnabled())
LOG.debug("Extract " + this + " to " + directory);
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
String subEntryName = (endOfJarUrl + 2 < urlString.length() ? urlString.substring(endOfJarUrl + 2) : null);
boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/") ? true : false);
if (LOG.isDebugEnabled())
LOG.debug("Extracting entry = " + subEntryName + " from jar " + jarFileURL);
URLConnection c = jarFileURL.openConnection();
c.setUseCaches(false);
try (InputStream is = c.getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
boolean shouldExtract;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
if ((subEntryName != null) && (entryName.startsWith(subEntryName))) {
// is the subentry really a dir?
if (!subEntryIsDir && subEntryName.length() + 1 == entryName.length() && entryName.endsWith("/"))
subEntryIsDir = true;
//extract it.
if (subEntryIsDir) {
//if it is a subdirectory we are looking for, then we
//are looking to extract its contents into the target
//directory. Remove the name of the subdirectory so
//that we don't wind up creating it too.
entryName = entryName.substring(subEntryName.length());
if (!entryName.equals("")) {
//the entry is
shouldExtract = true;
} else
shouldExtract = false;
} else
shouldExtract = true;
} else if ((subEntryName != null) && (!entryName.startsWith(subEntryName))) {
//there is a particular entry we are looking for, and this one
//isn't it
shouldExtract = false;
} else {
//we are extracting everything
shouldExtract = true;
}
if (!shouldExtract) {
if (LOG.isDebugEnabled())
LOG.debug("Skipping entry: " + entryName);
continue;
}
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
if (LOG.isDebugEnabled())
LOG.debug("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
// Make directory
if (!file.exists())
file.mkdirs();
} else {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
}
}
if ((subEntryName == null) || (subEntryName != null && subEntryName.equalsIgnoreCase("META-INF/MANIFEST.MF"))) {
Manifest manifest = jin.getManifest();
if (manifest != null) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
use of java.util.jar.JarInputStream in project jetty.project by eclipse.
the class SelectiveJarResource method copyTo.
/**
* @see org.eclipse.jetty.util.resource.JarResource#copyTo(java.io.File)
*/
@Override
public void copyTo(File directory) throws IOException {
if (_includes == null)
_includes = DEFAULT_INCLUDES;
if (_excludes == null)
_excludes = DEFAULT_EXCLUDES;
//parts of the jar file are copied
if (!exists())
return;
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
try (InputStream is = jarFileURL.openConnection().getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
LOG.debug("Looking at " + entryName);
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
LOG.info("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// Make directory
if (!file.exists())
file.mkdirs();
} else
LOG.debug("{} dir is excluded", entryName);
} else
LOG.debug("{} dir is NOT included", entryName);
} else {
//entry is a file, is it included?
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
} else
LOG.debug("{} file is excluded", entryName);
} else
LOG.debug("{} file is NOT included", entryName);
}
}
Manifest manifest = jin.getManifest();
if (manifest != null) {
if (isIncluded("META-INF") && !isExcluded("META-INF")) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
use of java.util.jar.JarInputStream in project jetty.project by eclipse.
the class WarURLConnection method substitueManifest.
/**
* Use PipedOuputStream and PipedInputStream to do the transformation without making
* a new temporary file ust to replace the manifest.
* @param newmanifest The new manifest
* @param rawIn The file input stream or equivalent. not the jar input stream.
*/
public static InputStream substitueManifest(final Manifest newmanifest, final InputStream rawIn) throws IOException {
final PipedOutputStream pOut = new PipedOutputStream();
PipedInputStream pIn = new PipedInputStream(pOut);
Runnable run = new Runnable() {
public void run() {
JarInputStream jin = null;
JarOutputStream dest = null;
try {
jin = new JarInputStream(rawIn, false);
dest = new JarOutputStream(pOut, newmanifest);
ZipEntry next = jin.getNextEntry();
while (next != null) {
if (next.getName().equalsIgnoreCase(JarFile.MANIFEST_NAME)) {
continue;
}
dest.putNextEntry(next);
if (next.getSize() > 0) {
IO.copy(jin, dest, next.getSize());
}
next = jin.getNextJarEntry();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (dest != null)
IO.close(dest);
if (jin != null)
IO.close(jin);
IO.close(pOut);
}
}
};
Thread th = new Thread(run);
th.start();
return pIn;
}
use of java.util.jar.JarInputStream in project jetty.project by eclipse.
the class JarScanner method matched.
public void matched(URI uri) throws Exception {
LOG.debug("Search of {}", uri);
if (uri.toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
InputStream in = Resource.newResource(uri).getInputStream();
if (in == null)
return;
JarInputStream jar_in = new JarInputStream(in);
try {
JarEntry entry = jar_in.getNextJarEntry();
while (entry != null) {
processEntry(uri, entry);
entry = jar_in.getNextJarEntry();
}
} finally {
jar_in.close();
}
}
}
use of java.util.jar.JarInputStream in project buck by facebook.
the class HashingDeterministicJarWriterTest method entriesAreWrittenAsTheyAreEncounteredWithManifestLast.
@Test
public void entriesAreWrittenAsTheyAreEncounteredWithManifestLast() throws IOException {
writer.writeEntry("Z", new ByteArrayInputStream("Z's contents".getBytes(StandardCharsets.UTF_8)));
writer.writeEntry("A", new ByteArrayInputStream("A's contents".getBytes(StandardCharsets.UTF_8)));
writer.close();
try (JarInputStream jar = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) {
JarEntry entry;
entry = jar.getNextJarEntry();
assertEquals("Z", entry.getName());
entry = jar.getNextJarEntry();
assertEquals("A", entry.getName());
entry = jar.getNextJarEntry();
assertEquals(JarFile.MANIFEST_NAME, entry.getName());
}
}
Aggregations