List of usage examples for java.util.jar JarOutputStream closeEntry
public void closeEntry() throws IOException
From source file:org.sourcepit.osgifier.core.packaging.Repackager.java
private void copyJarContents(JarInputStream srcJarIn, final JarOutputStream destJarOut, PathMatcher contentMatcher) throws IOException { final Set<String> processedEntires = new HashSet<String>(); JarEntry srcEntry = srcJarIn.getNextJarEntry(); while (srcEntry != null) { final String entryName = srcEntry.getName(); if (contentMatcher.isMatch(entryName)) { if (processedEntires.add(entryName)) { destJarOut.putNextEntry(new JarEntry(srcEntry.getName())); IOUtils.copy(srcJarIn, destJarOut); destJarOut.closeEntry(); } else { logger.warn("Ignored duplicate jar entry: " + entryName); }/*from ww w .j a v a 2 s . c o m*/ } srcJarIn.closeEntry(); srcEntry = srcJarIn.getNextJarEntry(); } }
From source file:org.apache.pluto.util.assemble.io.JarStreamingAssembly.java
/** * Reads the source JarInputStream, copying entries to the destination JarOutputStream. * The web.xml and portlet.xml are cached, and after the entire archive is copied * (minus the web.xml) a re-written web.xml is generated and written to the * destination JAR./*from w w w . j a v a 2s . c o m*/ * * @param source the WAR source input stream * @param dest the WAR destination output stream * @param dispatchServletClass the name of the dispatch class * @throws IOException */ public static void assembleStream(JarInputStream source, JarOutputStream dest, String dispatchServletClass) throws IOException { try { //Need to buffer the web.xml and portlet.xml files for the rewritting JarEntry servletXmlEntry = null; byte[] servletXmlBuffer = null; byte[] portletXmlBuffer = null; JarEntry originalJarEntry; //Read the source archive entry by entry while ((originalJarEntry = source.getNextJarEntry()) != null) { final JarEntry newJarEntry = smartClone(originalJarEntry); originalJarEntry = null; //Capture the web.xml JarEntry and contents as a byte[], don't write it out now or //update the CRC or length of the destEntry. if (Assembler.SERVLET_XML.equals(newJarEntry.getName())) { servletXmlEntry = newJarEntry; servletXmlBuffer = IOUtils.toByteArray(source); } //Capture the portlet.xml contents as a byte[] else if (Assembler.PORTLET_XML.equals(newJarEntry.getName())) { portletXmlBuffer = IOUtils.toByteArray(source); dest.putNextEntry(newJarEntry); IOUtils.write(portletXmlBuffer, dest); } //Copy all other entries directly to the output archive else { dest.putNextEntry(newJarEntry); IOUtils.copy(source, dest); } dest.closeEntry(); dest.flush(); } // If no portlet.xml was found in the archive, skip the assembly step. if (portletXmlBuffer != null) { // container for assembled web.xml bytes final byte[] webXmlBytes; // Checks to make sure the web.xml was found in the archive if (servletXmlBuffer == null) { throw new FileNotFoundException( "File '" + Assembler.SERVLET_XML + "' could not be found in the source input stream."); } //Create streams of the byte[] data for the updater method final InputStream webXmlIn = new ByteArrayInputStream(servletXmlBuffer); final InputStream portletXmlIn = new ByteArrayInputStream(portletXmlBuffer); final ByteArrayOutputStream webXmlOut = new ByteArrayOutputStream(servletXmlBuffer.length); //Update the web.xml WebXmlStreamingAssembly.assembleStream(webXmlIn, portletXmlIn, webXmlOut, dispatchServletClass); IOUtils.copy(webXmlIn, webXmlOut); webXmlBytes = webXmlOut.toByteArray(); //If no compression is being used (STORED) we have to manually update the size and crc if (servletXmlEntry.getMethod() == ZipEntry.STORED) { servletXmlEntry.setSize(webXmlBytes.length); final CRC32 webXmlCrc = new CRC32(); webXmlCrc.update(webXmlBytes); servletXmlEntry.setCrc(webXmlCrc.getValue()); } //write out the assembled web.xml entry and contents dest.putNextEntry(servletXmlEntry); IOUtils.write(webXmlBytes, dest); if (LOG.isDebugEnabled()) { LOG.debug("Jar stream " + source + " successfully assembled."); } } else { if (LOG.isDebugEnabled()) { LOG.debug("No portlet XML file was found, assembly was not required."); } //copy the original, unmodified web.xml entry to the destination dest.putNextEntry(servletXmlEntry); IOUtils.write(servletXmlBuffer, dest); if (LOG.isDebugEnabled()) { LOG.debug("Jar stream " + source + " successfully assembled."); } } } finally { dest.flush(); dest.close(); } }
From source file:gov.nih.nci.restgen.util.JarHelper.java
/** * Recursively jars up the given path under the given directory. *//*w w w . ja v a2 s . c o m*/ private void jarDir(File dirOrFile2jar, JarOutputStream jos, String path) throws IOException { //if (mVerbose) { //System.out.println("checking " + dirOrFile2jar); } if (dirOrFile2jar.isDirectory()) { String[] dirList = dirOrFile2jar.list(); String subPath = (path == null) ? "" : (path + dirOrFile2jar.getName() + SEP); if (path != null) { JarEntry je = new JarEntry(subPath); je.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(je); jos.flush(); jos.closeEntry(); } for (int i = 0; i < dirList.length; i++) { File f = new File(dirOrFile2jar, dirList[i]); jarDir(f, jos, subPath); } } else { if (dirOrFile2jar.getCanonicalPath().equals(mDestJarName)) { //if (mVerbose) { //System.out.println("skipping " + dirOrFile2jar.getPath()); } return; } if (mVerbose) { //System.out.println("adding " + dirOrFile2jar.getPath()); } FileInputStream fis = new FileInputStream(dirOrFile2jar); try { JarEntry entry = new JarEntry(path + dirOrFile2jar.getName()); entry.setTime(dirOrFile2jar.lastModified()); jos.putNextEntry(entry); while ((mByteCount = fis.read(mBuffer)) != -1) { jos.write(mBuffer, 0, mByteCount); if (mVerbose) { //System.out.println("wrote " + mByteCount + " bytes"); } } jos.flush(); jos.closeEntry(); } catch (IOException ioe) { throw ioe; } finally { fis.close(); } } }
From source file:org.commonjava.web.test.fixture.JarKnockouts.java
public void rewriteJar(final File source, final File targetDir) throws IOException { targetDir.mkdirs();//from w w w .j av a 2 s . c om final File target = new File(targetDir, source.getName()); JarFile in = null; JarOutputStream out = null; try { in = new JarFile(source); final BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target)); out = new JarOutputStream(fos, in.getManifest()); final Enumeration<JarEntry> entries = in.entries(); while (entries.hasMoreElements()) { final JarEntry entry = entries.nextElement(); if (!knockout(entry.getName())) { final InputStream stream = in.getInputStream(entry); out.putNextEntry(entry); copy(stream, out); out.closeEntry(); } } } finally { closeQuietly(out); if (in != null) { try { in.close(); } catch (final IOException e) { } } } }
From source file:edu.stanford.muse.email.JarDocCache.java
@Override public synchronized void saveContents(String contents, String prefix, int msgNum) throws IOException, GeneralSecurityException { JarOutputStream jos = getContentsJarOS(prefix); // create the bytes ZipEntry ze = new ZipEntry(msgNum + ".content"); ze.setMethod(ZipEntry.DEFLATED); // byte[] buf = CryptoUtils.getEncryptedBytes(contents.getBytes("UTF-8")); byte[] buf = contents.getBytes("UTF-8"); jos.putNextEntry(ze);/*from w w w. ja va2 s . c o m*/ jos.write(buf, 0, buf.length); jos.closeEntry(); jos.flush(); }
From source file:org.apache.solr.core.TestCoreContainer.java
@Test public void testSharedLib() throws Exception { Path tmpRoot = createTempDir("testSharedLib"); File lib = new File(tmpRoot.toFile(), "lib"); lib.mkdirs();/*from w w w .ja v a 2s .c om*/ JarOutputStream jar1 = new JarOutputStream(new FileOutputStream(new File(lib, "jar1.jar"))); jar1.putNextEntry(new JarEntry("defaultSharedLibFile")); jar1.closeEntry(); jar1.close(); File customLib = new File(tmpRoot.toFile(), "customLib"); customLib.mkdirs(); JarOutputStream jar2 = new JarOutputStream(new FileOutputStream(new File(customLib, "jar2.jar"))); jar2.putNextEntry(new JarEntry("customSharedLibFile")); jar2.closeEntry(); jar2.close(); final CoreContainer cc1 = init(tmpRoot, "<solr></solr>"); try { cc1.loader.openResource("defaultSharedLibFile").close(); } finally { cc1.shutdown(); } final CoreContainer cc2 = init(tmpRoot, "<solr><str name=\"sharedLib\">lib</str></solr>"); try { cc2.loader.openResource("defaultSharedLibFile").close(); } finally { cc2.shutdown(); } final CoreContainer cc3 = init(tmpRoot, "<solr><str name=\"sharedLib\">customLib</str></solr>"); try { cc3.loader.openResource("customSharedLibFile").close(); } finally { cc3.shutdown(); } }
From source file:edu.stanford.muse.email.JarDocCache.java
/** returns a jar outputstream for the given filename. * copies over jar entries if the file was already existing. * unfortunately, we can't append to the end of a jar file easily. * see e.g.http://stackoverflow.com/questions/2223434/appending-files-to-a-zip-file-with-java * and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4129445 * may consider using truezip at some point, but the doc is dense. *//* ww w. j a va 2s . c o m*/ private static JarOutputStream appendOrCreateJar(String filename) throws IOException { JarOutputStream jos; File f = new File(filename); if (!f.exists()) return new JarOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); // bak* is going to be all the previous file entries String bakFilename = filename + ".bak"; File bakFile = new File(bakFilename); f.renameTo(new File(bakFilename)); jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); JarFile bakJF; try { bakJF = new JarFile(bakFilename); } catch (Exception e) { log.warn("Bad jar file! " + bakFilename + " size " + new File(filename).length() + " bytes"); Util.print_exception(e, log); return new JarOutputStream(new BufferedOutputStream(new FileOutputStream(filename))); } // copy all entries from bakJF to jos Enumeration<JarEntry> bakEntries = bakJF.entries(); while (bakEntries.hasMoreElements()) { JarEntry je = bakEntries.nextElement(); jos.putNextEntry(je); InputStream is = bakJF.getInputStream(je); // copy over everything from is to jos byte buf[] = new byte[32 * 1024]; // randomly 32K int nBytes; while ((nBytes = is.read(buf)) != -1) jos.write(buf, 0, nBytes); jos.closeEntry(); } bakFile.delete(); return jos; }
From source file:ezbake.deployer.publishers.EzAzkabanPublisher.java
/** * This will publish the artifact to Azkaban for scheduled running. The artifact should be of the format * <p/>/*w w w . j av a 2s. c om*/ * <p/> * The artifact at this point in time will already have included the SSL certs. * <p/> * Its up to the publisher to reorganize the tar file if needed for its PaaS * * @param artifact The artifact to deploy * @param callerToken - The token of the user or application that initiated this call * @throws DeploymentException - On any exceptions */ @Override public void publish(DeploymentArtifact artifact, EzSecurityToken callerToken) throws DeploymentException { File unzippedPack = null; File azkabanZip = null; ZipOutputStream zipOutputStream = null; String flowName; final BatchJobInfo jobInfo = artifact.getMetadata().getManifest().getBatchJobInfo(); // Get the Azkaban authentication token final AuthenticationResult authenticatorResult; try { authenticatorResult = new AuthenticationManager(new URI(azConf.getAzkabanUrl()), azConf.getUsername(), azConf.getPassword()).login(); } catch (URISyntaxException e) { throw new DeploymentException(e.getMessage()); } if (authenticatorResult.hasError()) { log.error("Could not log into Azkaban: " + authenticatorResult.getError()); throw new DeploymentException(authenticatorResult.getError()); } log.info("Successfully logged into Azkaban. Now creating .zip to upload"); try { // Unzip the artifact unzippedPack = UnzipUtil.unzip(new File(unzipDir), ByteBuffer.wrap(artifact.getArtifact())); log.info("Unzipped artifact to: " + unzippedPack.getAbsolutePath()); // Create a .zip file to submit to Azkaban azkabanZip = File.createTempFile("ezbatch_", ".zip"); log.info("Created temporary zip file: " + azkabanZip.getCanonicalPath()); zipOutputStream = new ZipOutputStream(new FileOutputStream(azkabanZip)); // Copy the configs from the artifact to the top level of the zip. This should contain the Azkaban // .jobs and .properties final String configDir = UnzipUtil.getConfDirectory(unzippedPack).get(); final File configDirFile = new File(configDir); for (File f : FileUtils.listFiles(configDirFile, TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream.putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(configDir, ""))); IOUtils.copy(new FileInputStream(f), zipOutputStream); zipOutputStream.closeEntry(); } log.info("Copied configs to the .zip"); // Copy the jars from bin/ in the artifact to lib/ in the .zip file and other things to the jar as needed final String dirPrefix = unzippedPack.getAbsolutePath() + "/bin/"; for (File f : FileUtils.listFiles(new File(dirPrefix), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { zipOutputStream .putNextEntry(new ZipArchiveEntry(f.getCanonicalPath().replaceFirst(dirPrefix, "lib/"))); final JarInputStream jarInputStream = new JarInputStream(new FileInputStream(f)); final JarOutputStream jarOutputStream = new JarOutputStream(zipOutputStream); JarEntry je; while ((je = jarInputStream.getNextJarEntry()) != null) { jarOutputStream.putNextEntry(je); IOUtils.copy(jarInputStream, jarOutputStream); jarOutputStream.closeEntry(); } log.info("Created Jar file"); // Add the SSL certs to the jar final String sslPath = UnzipUtil.getSSLPath(configDirFile).get(); for (File sslFile : FileUtils.listFiles(new File(sslPath), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) { if (sslFile.isFile()) { jarOutputStream.putNextEntry(new JarArchiveEntry("ssl/" + sslFile.getName())); IOUtils.copy(new FileInputStream(sslFile), jarOutputStream); jarOutputStream.closeEntry(); } } log.info("Added SSL certs to jar"); // Add the application.properties to the jar file so the jobs can read it final File appProps = new File(configDir, "application.properties"); final Properties adjustedProperties = new Properties(); adjustedProperties.load(new FileInputStream(appProps)); adjustedProperties.setProperty("ezbake.security.ssl.dir", "/ssl/"); jarOutputStream.putNextEntry(new JarArchiveEntry("application.properties")); adjustedProperties.store(jarOutputStream, null); jarOutputStream.closeEntry(); jarOutputStream.finish(); zipOutputStream.closeEntry(); } // Check to see if there are any .job files. If there aren't, this is an external job and we need to create // one for the .zip file final Collection<File> jobFiles = FileUtils.listFiles(configDirFile, new String[] { "job" }, false); if (jobFiles.isEmpty()) { // If there are no job files present then we need to create one for the user final StringBuilder sb = new StringBuilder( "type=hadoopJava\n" + "job.class=ezbatch.amino.api.EzFrameworkDriver\n" + "classpath=./lib/*\n" + "main.args=-d /ezbatch/amino/config"); for (File xmlConfig : FileUtils.listFiles(configDirFile, new String[] { "xml" }, false)) { sb.append(" -c ").append(xmlConfig.getName()); } zipOutputStream.putNextEntry(new ZipEntry("Analytic.job")); IOUtils.copy(new StringReader(sb.toString()), zipOutputStream); zipOutputStream.closeEntry(); log.info("There was no .job file so one was created for the .zip"); flowName = "Analytic"; } else { flowName = jobInfo.getFlowName(); if (flowName == null) { log.warn("Manifest did not contain flow_name. Guessing what it should be"); flowName = FilenameUtils.getBaseName(jobFiles.toArray(new File[jobFiles.size()])[0].getName()); log.info("Guessing the flow name should be:" + flowName); } } zipOutputStream.finish(); log.info("Finished creating .zip"); // Now that we've created the zip to upload, attempt to create a project for it to be uploaded to. Every .zip // file needs to be uploaded to a project, and the project may or may not already exist. final String projectName = ArtifactHelpers.getAppId(artifact) + "_" + ArtifactHelpers.getServiceId(artifact); final ProjectManager projectManager = new ProjectManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); final ManagerResult managerResult = projectManager.createProject(projectName, "EzBatch Deployed"); // If the project already exists, it will return an error, but really it's not a problem if (managerResult.hasError()) { if (!managerResult.getMessage().contains("already exists")) { log.error("Could not create project: " + managerResult.getMessage()); throw new DeploymentException(managerResult.getMessage()); } else { log.info("Reusing the existing project: " + projectName); } } else { log.info("Created new project: " + projectName); log.info("Path: " + managerResult.getPath()); } // Upload the .zip file to the project final UploadManager uploader = new UploadManager(authenticatorResult.getSessionId(), azConf.getAzkabanUrl(), projectName, azkabanZip); final UploaderResult uploaderResult = uploader.uploadZip(); if (uploaderResult.hasError()) { log.error("Could not upload the zip file: " + uploaderResult.getError()); throw new DeploymentException(uploaderResult.getError()); } log.info("Successfully submitted zip file to Azkaban"); // Schedule the jar to run. If the start times aren't provided, it will run in 2 minutes final ScheduleManager scheduler = new ScheduleManager(authenticatorResult.getSessionId(), new URI(azConf.getAzkabanUrl())); // Add the optional parameters if they are present if (jobInfo.isSetStartDate()) { scheduler.setScheduleDate(jobInfo.getStartDate()); } if (jobInfo.isSetStartTime()) { scheduler.setScheduleTime(jobInfo.getStartTime()); } if (jobInfo.isSetRepeat()) { scheduler.setPeriod(jobInfo.getRepeat()); } final SchedulerResult schedulerResult = scheduler.scheduleFlow(projectName, flowName, uploaderResult.getProjectId()); if (schedulerResult.hasError()) { log.error("Failure to schedule job: " + schedulerResult.getError()); throw new DeploymentException(schedulerResult.getError()); } log.info("Successfully scheduled flow: " + flowName); } catch (Exception ex) { log.error("No Nos!", ex); throw new DeploymentException(ex.getMessage()); } finally { IOUtils.closeQuietly(zipOutputStream); FileUtils.deleteQuietly(azkabanZip); FileUtils.deleteQuietly(unzippedPack); } }
From source file:org.atricore.josso.tooling.wrapper.InstallCommand.java
private void createJar(File outFile, String resource) throws Exception { if (!outFile.exists()) { System.out.println(Ansi.ansi().a("Creating file: ").a(Ansi.Attribute.INTENSITY_BOLD) .a(outFile.getPath()).a(Ansi.Attribute.RESET).toString()); InputStream is = getClass().getClassLoader().getResourceAsStream(resource); if (is == null) { throw new IllegalStateException("Resource " + resource + " not found!"); }//from ww w.j a v a2 s. com try { JarOutputStream jar = new JarOutputStream(new FileOutputStream(outFile)); int idx = resource.indexOf('/'); while (idx > 0) { jar.putNextEntry(new ZipEntry(resource.substring(0, idx))); jar.closeEntry(); idx = resource.indexOf('/', idx + 1); } jar.putNextEntry(new ZipEntry(resource)); int c; while ((c = is.read()) >= 0) { jar.write(c); } jar.closeEntry(); jar.close(); } finally { safeClose(is); } } }
From source file:com.speed.ob.api.ClassStore.java
public void dump(File in, File out, Config config) throws IOException { if (in.isDirectory()) { for (ClassNode node : nodes()) { String[] parts = node.name.split("\\."); String dirName = node.name.substring(0, node.name.lastIndexOf(".")); dirName = dirName.replace(".", "/"); File dir = new File(out, dirName); if (!dir.exists()) { if (!dir.mkdirs()) throw new IOException("Could not make output dir: " + dir.getAbsolutePath()); }/*w ww .j av a2 s. c om*/ ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); node.accept(writer); byte[] data = writer.toByteArray(); FileOutputStream fOut = new FileOutputStream( new File(dir, node.name.substring(node.name.lastIndexOf(".") + 1))); fOut.write(data); fOut.flush(); fOut.close(); } } else if (in.getName().endsWith(".jar")) { File output = new File(out, in.getName()); JarFile jf = new JarFile(in); HashMap<JarEntry, Object> existingData = new HashMap<>(); if (output.exists()) { try { JarInputStream jarIn = new JarInputStream(new FileInputStream(output)); JarEntry entry; while ((entry = jarIn.getNextJarEntry()) != null) { if (!entry.isDirectory()) { byte[] data = IOUtils.toByteArray(jarIn); existingData.put(entry, data); jarIn.closeEntry(); } } jarIn.close(); } catch (IOException e) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "Could not read existing output file, overwriting", e); } } FileOutputStream fout = new FileOutputStream(output); Manifest manifest = null; if (jf.getManifest() != null) { manifest = jf.getManifest(); if (!config.getBoolean("ClassNameTransform.keep_packages") && config.getBoolean("ClassNameTransform.exclude_mains")) { manifest = new Manifest(manifest); if (manifest.getMainAttributes().getValue("Main-Class") != null) { String manifestName = manifest.getMainAttributes().getValue("Main-Class"); if (manifestName.contains(".")) { manifestName = manifestName.substring(manifestName.lastIndexOf(".") + 1); manifest.getMainAttributes().putValue("Main-Class", manifestName); } } } } jf.close(); JarOutputStream jarOut = manifest == null ? new JarOutputStream(fout) : new JarOutputStream(fout, manifest); Logger.getLogger(getClass().getName()).fine("Restoring " + existingData.size() + " existing files"); if (!existingData.isEmpty()) { for (Map.Entry<JarEntry, Object> entry : existingData.entrySet()) { Logger.getLogger(getClass().getName()).fine("Restoring " + entry.getKey().getName()); jarOut.putNextEntry(entry.getKey()); jarOut.write((byte[]) entry.getValue()); jarOut.closeEntry(); } } for (ClassNode node : nodes()) { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); node.accept(writer); byte[] data = writer.toByteArray(); int index = node.name.lastIndexOf("/"); String fileName; if (index > 0) { fileName = node.name.substring(0, index + 1).replace(".", "/"); fileName += node.name.substring(index + 1).concat(".class"); } else { fileName = node.name.concat(".class"); } JarEntry entry = new JarEntry(fileName); jarOut.putNextEntry(entry); jarOut.write(data); jarOut.closeEntry(); } jarOut.close(); } else { if (nodes().size() == 1) { File outputFile = new File(out, in.getName()); ClassNode node = nodes().iterator().next(); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); byte[] data = writer.toByteArray(); FileOutputStream stream = new FileOutputStream(outputFile); stream.write(data); stream.close(); } } }