List of usage examples for java.util.jar JarInputStream getNextEntry
public ZipEntry getNextEntry() throws IOException
From source file:JarUtils.java
public static void unjar(InputStream in, File dest) throws IOException { if (!dest.exists()) { dest.mkdirs();/* w w w . j a v a 2 s . co m*/ } if (!dest.isDirectory()) { throw new IOException("Destination must be a directory."); } JarInputStream jin = new JarInputStream(in); byte[] buffer = new byte[1024]; ZipEntry entry = jin.getNextEntry(); while (entry != null) { String fileName = entry.getName(); if (fileName.charAt(fileName.length() - 1) == '/') { fileName = fileName.substring(0, fileName.length() - 1); } if (fileName.charAt(0) == '/') { fileName = fileName.substring(1); } if (File.separatorChar != '/') { fileName = fileName.replace('/', File.separatorChar); } File file = new File(dest, fileName); if (entry.isDirectory()) { // make sure the directory exists file.mkdirs(); jin.closeEntry(); } else { // make sure the directory exists File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } // dump the file OutputStream out = new FileOutputStream(file); int len = 0; while ((len = jin.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); out.close(); jin.closeEntry(); file.setLastModified(entry.getTime()); } entry = jin.getNextEntry(); } /* Explicity write out the META-INF/MANIFEST.MF so that any headers such as the Class-Path are see for the unpackaged jar */ Manifest mf = jin.getManifest(); if (mf != null) { File file = new File(dest, "META-INF/MANIFEST.MF"); File parent = file.getParentFile(); if (parent.exists() == false) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); mf.write(out); out.flush(); out.close(); } jin.close(); }
From source file:JarUtil.java
/** * Adds the given file to the specified JAR file. * //w w w. j a v a 2 s. c o m * @param file * the file that should be added * @param jarFile * The JAR to which the file should be added * @param parentDir * the parent directory of the file, this is used to calculate * the path witin the JAR file. When null is given, the file will * be added into the root of the JAR. * @param compress * True when the jar file should be compressed * @throws FileNotFoundException * when the jarFile does not exist * @throws IOException * when a file could not be written or the jar-file could not * read. */ public static void addToJar(File file, File jarFile, File parentDir, boolean compress) throws FileNotFoundException, IOException { File tmpJarFile = File.createTempFile("tmp", ".jar", jarFile.getParentFile()); JarOutputStream out = new JarOutputStream(new FileOutputStream(tmpJarFile)); if (compress) { out.setLevel(ZipOutputStream.DEFLATED); } else { out.setLevel(ZipOutputStream.STORED); } // copy contents of old jar to new jar: JarFile inputFile = new JarFile(jarFile); JarInputStream in = new JarInputStream(new FileInputStream(jarFile)); CRC32 crc = new CRC32(); byte[] buffer = new byte[512 * 1024]; JarEntry entry = (JarEntry) in.getNextEntry(); while (entry != null) { InputStream entryIn = inputFile.getInputStream(entry); add(entry, entryIn, out, crc, buffer); entryIn.close(); entry = (JarEntry) in.getNextEntry(); } in.close(); inputFile.close(); int sourceDirLength; if (parentDir == null) { sourceDirLength = file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1; } else { sourceDirLength = file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1 - parentDir.getAbsolutePath().length(); } addFile(file, out, crc, sourceDirLength, buffer); out.close(); // remove old jar file and rename temp file to old one: if (jarFile.delete()) { if (!tmpJarFile.renameTo(jarFile)) { throw new IOException( "Unable to rename temporary JAR file to [" + jarFile.getAbsolutePath() + "]."); } } else { throw new IOException("Unable to delete old JAR file [" + jarFile.getAbsolutePath() + "]."); } }
From source file:ezbake.frack.submitter.util.JarUtil.java
public static File addFilesToJar(File sourceJar, List<File> newFiles) throws IOException { JarOutputStream target = null; JarInputStream source; File outputJar = new File(sourceJar.getParentFile(), getRepackagedJarName(sourceJar.getAbsolutePath())); log.debug("Output file created at {}", outputJar.getAbsolutePath()); outputJar.createNewFile();//from w w w . j a v a2s . co m try { source = new JarInputStream(new FileInputStream(sourceJar)); target = new JarOutputStream(new FileOutputStream(outputJar)); ZipEntry entry = source.getNextEntry(); while (entry != null) { String name = entry.getName(); // Add ZIP entry to output stream. target.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; byte[] buffer = new byte[BUFFER_SIZE]; while ((len = source.read(buffer)) > 0) { target.write(buffer, 0, len); } entry = source.getNextEntry(); } source.close(); for (File fileToAdd : newFiles) { add(fileToAdd, fileToAdd.getParentFile().getAbsolutePath(), target); } } finally { if (target != null) { log.debug("Closing output stream"); target.close(); } } return outputJar; }
From source file:com.cisco.surf.jenkins.plugins.jenkow.test.eclipse.UpdateSiteTest.java
public void testUpdateSite() throws Exception { URL u = new URL(getURL(), "plugin/" + PLUGIN_NAME + "/eclipse.site/content.jar"); Set<String> names = new HashSet<String>(); JarInputStream jis = new JarInputStream(u.openStream()); try {/*from ww w . j av a 2 s .c om*/ while (true) { ZipEntry ze = jis.getNextEntry(); if (ze == null) break; names.add(ze.getName()); } } finally { IOUtils.closeQuietly(jis); } assertTrue(names.contains("content.xml")); }
From source file:com.cisco.step.jenkins.plugins.jenkow.test.eclipse.UpdateSiteTest.java
public void testUpdateSite() throws Exception { URL u = new URL(getURL(), "plugin/" + Consts.PLUGIN_NAME + "/eclipse.site/content.jar"); Set<String> names = new HashSet<String>(); JarInputStream jis = new JarInputStream(u.openStream()); try {//from w w w.j a v a 2 s . com while (true) { ZipEntry ze = jis.getNextEntry(); if (ze == null) break; names.add(ze.getName()); } } finally { IOUtils.closeQuietly(jis); } assertTrue(names.contains("content.xml")); }
From source file:com.izforge.izpack.compiler.helper.CompilerHelper.java
/** * Returns the qualified class name for the given class. This method expects as the url param a * jar file which contains the given class. It scans the zip entries of the jar file. * * @param url url of the jar file which contains the class * @param className short name of the class for which the full name should be resolved * @return full qualified class name/*from w w w .j av a 2 s. c om*/ * @throws IOException if the jar cannot be read */ public String getFullClassName(URL url, String className) throws IOException { JarInputStream jis = new JarInputStream(url.openStream()); ZipEntry zentry; while ((zentry = jis.getNextEntry()) != null) { String name = zentry.getName(); int lastPos = name.lastIndexOf(".class"); if (lastPos < 0) { continue; // No class file. } name = name.replace('/', '.'); int pos = -1; int nonCasePos = -1; if (className != null) { pos = name.indexOf(className); nonCasePos = name.toLowerCase().indexOf(className.toLowerCase()); } if (pos != -1 && name.length() == pos + className.length() + 6) // "Main" class found { jis.close(); return (name.substring(0, lastPos)); } if (nonCasePos != -1 && name.length() == nonCasePos + className.length() + 6) // "Main" class with different case found { throw new IllegalArgumentException("Fatal error! The declared panel name in the xml file (" + className + ") differs in case to the founded class file (" + name + ")."); } } jis.close(); return (null); }
From source file:com.izforge.izpack.compiler.helper.CompilerHelper.java
/** * Returns a list which contains the pathes of all files which are included in the given url. * This method expects as the url param a jar. * * @param url url of the jar file//from w w w.j a va 2 s.co m * @return full qualified paths of the contained files * @throws IOException if the jar cannot be read */ public List<String> getContainedFilePaths(URL url) throws IOException { JarInputStream jis = new JarInputStream(url.openStream()); ZipEntry zentry; ArrayList<String> fullNames = new ArrayList<String>(); while ((zentry = jis.getNextEntry()) != null) { String name = zentry.getName(); // Add only files, no directory entries. if (!zentry.isDirectory()) { fullNames.add(name); } } jis.close(); return (fullNames); }
From source file:com.asual.summer.bundle.BundleDescriptorMojo.java
public void execute() throws MojoExecutionException { try {/* w ww .j a v a2 s . c o m*/ String webXml = "WEB-INF/web.xml"; File warDir = new File(buildDirectory, finalName); File warFile = new File(buildDirectory, finalName + ".war"); File webXmlFile = new File(warDir, webXml); FileUtils.copyFile(new File(basedir, "src/main/webapp/" + webXml), webXmlFile); Configuration[] configurations = new Configuration[] { new WebXmlConfiguration(), new FragmentConfiguration() }; WebAppContext context = new WebAppContext(); context.setDefaultsDescriptor(null); context.setDescriptor(webXmlFile.getAbsolutePath()); context.setConfigurations(configurations); for (Artifact artifact : artifacts) { JarInputStream in = new JarInputStream(new FileInputStream(artifact.getFile())); while (true) { ZipEntry entry = in.getNextEntry(); if (entry != null) { if ("META-INF/web-fragment.xml".equals(entry.getName())) { Resource fragment = Resource .newResource("file:" + artifact.getFile().getAbsolutePath()); context.getMetaData().addFragment(fragment, Resource .newResource("jar:" + fragment.getURL() + "!/META-INF/web-fragment.xml")); context.getMetaData().addWebInfJar(fragment); } } else { break; } } in.close(); } for (int i = 0; i < configurations.length; i++) { configurations[i].preConfigure(context); } for (int i = 0; i < configurations.length; i++) { configurations[i].configure(context); } for (int i = 0; i < configurations.length; i++) { configurations[i].postConfigure(context); } Descriptor descriptor = context.getMetaData().getWebXml(); Node root = descriptor.getRoot(); List<Object> nodes = new ArrayList<Object>(); List<FragmentDescriptor> fragmentDescriptors = context.getMetaData().getOrderedFragments(); for (FragmentDescriptor fd : fragmentDescriptors) { for (int i = 0; i < fd.getRoot().size(); i++) { Object el = fd.getRoot().get(i); if (el instanceof Node && ((Node) el).getTag().matches("^name|ordering$")) { continue; } nodes.add(el); } } root.addAll(nodes); BufferedWriter writer = new BufferedWriter(new FileWriter(new File(warDir, webXml))); writer.write(root.toString()); writer.close(); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(warFile)); zip(warDir, warDir, zos); zos.close(); } catch (Exception e) { getLog().error(e.getMessage(), e); throw new MojoExecutionException(e.getMessage(), e); } }
From source file:com.speed.ob.api.ClassStore.java
public void init(JarInputStream jarIn, File output, File in) throws IOException { ZipEntry entry;/*w w w. j a v a2 s. co m*/ JarOutputStream out = new JarOutputStream(new FileOutputStream(new File(output, in.getName()))); while ((entry = jarIn.getNextEntry()) != null) { byte[] data = IOUtils.toByteArray(jarIn); if (entry.getName().endsWith(".class")) { ClassReader reader = new ClassReader(data); ClassNode cn = new ClassNode(); reader.accept(cn, ClassReader.EXPAND_FRAMES); store.put(cn.name, cn); } else if (!entry.isDirectory()) { Logger.getLogger(getClass().getName()).finer("Storing " + entry.getName() + " in output file"); JarEntry je = new JarEntry(entry.getName()); out.putNextEntry(je); out.write(data); out.closeEntry(); } } out.close(); }
From source file:com.github.sampov2.OneJarMojo.java
private void copyTemplateFilesToArchive(JarInputStream template, JarOutputStream out) throws IOException { // One-jar stuff debug("Adding one-jar components..."); ZipEntry entry = null;//w w w . j av a 2s . c o m while ((entry = template.getNextEntry()) != null) { // Skip the manifest file, no need to clutter... if (!"boot-manifest.mf".equals(entry.getName())) { addToZip(out, entry, template); } } }