List of usage examples for java.util.jar JarOutputStream closeEntry
public void closeEntry() throws IOException
From source file:org.apache.hadoop.mapred.TestTaskTrackerLocalization.java
/** * @param jobConf/*from w w w .j a v a2 s. c om*/ * @throws IOException * @throws FileNotFoundException */ private void uploadJobJar(JobConf jobConf) throws IOException, FileNotFoundException { File jobJarFile = new File(TEST_ROOT_DIR, "jobjar-on-dfs.jar"); JarOutputStream jstream = new JarOutputStream(new FileOutputStream(jobJarFile)); ZipEntry ze = new ZipEntry("lib/lib1.jar"); jstream.putNextEntry(ze); jstream.closeEntry(); ze = new ZipEntry("lib/lib2.jar"); jstream.putNextEntry(ze); jstream.closeEntry(); jstream.finish(); jstream.close(); jobConf.setJar(jobJarFile.toURI().toString()); }
From source file:org.apache.pig.test.TestJobControlCompiler.java
/** * creates a jar containing a UDF not in the current classloader * @param jarFile the jar to create/*from ww w .j a va 2s.co m*/ * @return the name of the class created (in the default package) * @throws IOException * @throws FileNotFoundException */ private String createTestJar(File jarFile) throws IOException, FileNotFoundException { // creating the source .java file File javaFile = File.createTempFile("TestUDF", ".java"); javaFile.deleteOnExit(); String className = javaFile.getName().substring(0, javaFile.getName().lastIndexOf('.')); FileWriter fw = new FileWriter(javaFile); try { fw.write("import org.apache.pig.EvalFunc;\n"); fw.write("import org.apache.pig.data.Tuple;\n"); fw.write("import java.io.IOException;\n"); fw.write("public class " + className + " extends EvalFunc<String> {\n"); fw.write(" public String exec(Tuple input) throws IOException {\n"); fw.write(" return \"test\";\n"); fw.write(" }\n"); fw.write("}\n"); } finally { fw.close(); } // compiling it JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); Iterable<? extends JavaFileObject> compilationUnits1 = fileManager.getJavaFileObjects(javaFile); CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits1); task.call(); // here is the compiled file File classFile = new File(javaFile.getParentFile(), className + ".class"); Assert.assertTrue(classFile.exists()); // putting it in the jar JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile)); try { jos.putNextEntry(new ZipEntry(classFile.getName())); try { InputStream testClassContentIS = new FileInputStream(classFile); try { byte[] buffer = new byte[64000]; int n; while ((n = testClassContentIS.read(buffer)) != -1) { jos.write(buffer, 0, n); } } finally { testClassContentIS.close(); } } finally { jos.closeEntry(); } } finally { jos.close(); } return className; }
From source file:org.wso2.esb.integration.common.utils.common.FileManager.java
public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory) throws IOException, URISyntaxException { File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI()); File destinationFileDirectory = new File(destinationDirectory); JarFile jarFile = new JarFile(sourceFile); String fileName = jarFile.getName(); String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator)); File destinationFile = new File(destinationFileDirectory, fileNameLastPart); JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile)); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { JarEntry jarEntry = entries.nextElement(); InputStream inputStream = jarFile.getInputStream(jarEntry); //jarOutputStream.putNextEntry(jarEntry); //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName())); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, bytesRead); }//from w w w . ja v a2s . com inputStream.close(); jarOutputStream.flush(); jarOutputStream.closeEntry(); } jarOutputStream.close(); }
From source file:com.netflix.nicobar.core.persistence.JarArchiveRepository.java
@Override public void insertArchive(JarScriptArchive jarScriptArchive) throws IOException { Objects.requireNonNull(jarScriptArchive, "jarScriptArchive"); ScriptModuleSpec moduleSpec = jarScriptArchive.getModuleSpec(); ModuleId moduleId = moduleSpec.getModuleId(); Path moduleJarPath = getModuleJarPath(moduleId); Files.deleteIfExists(moduleJarPath); JarFile sourceJarFile;/*from w w w . ja v a2s .com*/ try { sourceJarFile = new JarFile(jarScriptArchive.getRootUrl().toURI().getPath()); } catch (URISyntaxException e) { throw new IOException(e); } JarOutputStream destJarFile = new JarOutputStream(new FileOutputStream(moduleJarPath.toFile())); try { String moduleSpecFileName = moduleSpecSerializer.getModuleSpecFileName(); Enumeration<JarEntry> sourceEntries = sourceJarFile.entries(); while (sourceEntries.hasMoreElements()) { JarEntry sourceEntry = sourceEntries.nextElement(); if (sourceEntry.getName().equals(moduleSpecFileName)) { // avoid double entry for module spec continue; } destJarFile.putNextEntry(sourceEntry); if (!sourceEntry.isDirectory()) { InputStream inputStream = sourceJarFile.getInputStream(sourceEntry); IOUtils.copy(inputStream, destJarFile); IOUtils.closeQuietly(inputStream); } destJarFile.closeEntry(); } // write the module spec String serialized = moduleSpecSerializer.serialize(moduleSpec); JarEntry moduleSpecEntry = new JarEntry(moduleSpecSerializer.getModuleSpecFileName()); destJarFile.putNextEntry(moduleSpecEntry); IOUtils.write(serialized, destJarFile); destJarFile.closeEntry(); } finally { IOUtils.closeQuietly(sourceJarFile); IOUtils.closeQuietly(destJarFile); } // update the timestamp on the jar file to indicate that the module has been updated Files.setLastModifiedTime(moduleJarPath, FileTime.fromMillis(jarScriptArchive.getCreateTime())); }
From source file:org.nuclos.server.customcode.codegenerator.NuclosJavaCompilerComponent.java
private synchronized void jar(Map<String, byte[]> javacresult, List<CodeGenerator> generators) { try {//from w w w . ja v a 2 s . c om final boolean oldExists = moveJarToOld(); if (javacresult.size() > 0) { final Set<String> entries = new HashSet<String>(); final JarOutputStream jos = new JarOutputStream( new BufferedOutputStream(new FileOutputStream(JARFILE)), getManifest()); try { for (final String key : javacresult.keySet()) { entries.add(key); byte[] bytecode = javacresult.get(key); // create entry for directory (required for classpath scanning) if (key.contains("/")) { String dir = key.substring(0, key.lastIndexOf('/') + 1); if (!entries.contains(dir)) { entries.add(dir); jos.putNextEntry(new JarEntry(dir)); jos.closeEntry(); } } // call postCompile() (weaving) on compiled sources for (CodeGenerator generator : generators) { if (!oldExists || generator.isRecompileNecessary()) { for (JavaSourceAsString src : generator.getSourceFiles()) { final String name = src.getFQName(); if (key.startsWith(name.replaceAll("\\.", "/"))) { LOG.debug("postCompile (weaving) " + key); bytecode = generator.postCompile(key, bytecode); // Can we break here??? // break outer; } } } } jos.putNextEntry(new ZipEntry(key)); LOG.debug("writing to " + key + " to jar " + JARFILE); jos.write(bytecode); jos.closeEntry(); } if (oldExists) { final JarInputStream in = new JarInputStream( new BufferedInputStream(new FileInputStream(JARFILE_OLD))); final byte[] buffer = new byte[2048]; try { int size; JarEntry entry; while ((entry = in.getNextJarEntry()) != null) { if (!entries.contains(entry.getName())) { jos.putNextEntry(entry); LOG.debug("copying " + entry.getName() + " from old jar " + JARFILE_OLD); while ((size = in.read(buffer, 0, buffer.length)) != -1) { jos.write(buffer, 0, size); } jos.closeEntry(); } in.closeEntry(); } } finally { in.close(); } } } finally { jos.close(); } } } catch (IOException ex) { throw new NuclosFatalException(ex); } }
From source file:org.apache.hadoop.mapreduce.v2.TestMRJobs.java
private Path makeJar(Path p, int index) throws FileNotFoundException, IOException { FileOutputStream fos = new FileOutputStream(new File(p.toUri().getPath())); JarOutputStream jos = new JarOutputStream(fos); ZipEntry ze = new ZipEntry("distributed.jar.inside" + index); jos.putNextEntry(ze);//from w ww . j ava2 s . co m jos.write(("inside the jar!" + index).getBytes()); jos.closeEntry(); jos.close(); localFs.setPermission(p, new FsPermission("700")); return p; }
From source file:org.apache.hadoop.mapreduce.v2.TestMRJobs.java
private void createAndAddJarToJar(JarOutputStream jos, File jarFile) throws FileNotFoundException, IOException { FileOutputStream fos2 = new FileOutputStream(jarFile); JarOutputStream jos2 = new JarOutputStream(fos2); // Have to have at least one entry or it will complain ZipEntry ze = new ZipEntry("lib1.inside"); jos2.putNextEntry(ze);//from ww w .j ava2s . c o m jos2.closeEntry(); jos2.close(); ze = new ZipEntry("lib/" + jarFile.getName()); jos.putNextEntry(ze); FileInputStream in = new FileInputStream(jarFile); byte buf[] = new byte[1024]; int numRead; do { numRead = in.read(buf); if (numRead >= 0) { jos.write(buf, 0, numRead); } } while (numRead != -1); in.close(); jos.closeEntry(); jarFile.delete(); }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java
/** * Change the version in jar/* w ww.j av a 2 s . c o m*/ * * @param newVersion * @param file * @return * @throws MojoExecutionException */ protected File changeVersion(File file, String oldVersion, String newVersion) throws MojoExecutionException { String fileName = file.getName(); int pos = fileName.indexOf(oldVersion); fileName = fileName.substring(0, pos) + newVersion + fileName.substring(pos + oldVersion.length()); JarInputStream jis = null; JarOutputStream jos; OutputStream out = null; JarFile sourceJar = null; try { // now create a temporary file and update the version sourceJar = new JarFile(file); final Manifest manifest = sourceJar.getManifest(); manifest.getMainAttributes().putValue("Bundle-Version", newVersion); jis = new JarInputStream(new FileInputStream(file)); final File destJar = new File(file.getParentFile(), fileName); out = new FileOutputStream(destJar); jos = new JarOutputStream(out, manifest); jos.setMethod(JarOutputStream.DEFLATED); jos.setLevel(Deflater.BEST_COMPRESSION); JarEntry entryIn = jis.getNextJarEntry(); while (entryIn != null) { JarEntry entryOut = new JarEntry(entryIn.getName()); entryOut.setTime(entryIn.getTime()); entryOut.setComment(entryIn.getComment()); jos.putNextEntry(entryOut); if (!entryIn.isDirectory()) { IOUtils.copy(jis, jos); } jos.closeEntry(); jis.closeEntry(); entryIn = jis.getNextJarEntry(); } // close the JAR file now to force writing jos.close(); return destJar; } catch (IOException ioe) { throw new MojoExecutionException("Unable to update version in jar file.", ioe); } finally { if (sourceJar != null) { try { sourceJar.close(); } catch (IOException ex) { // close } } IOUtils.closeQuietly(jis); IOUtils.closeQuietly(out); } }
From source file:org.wso2.carbon.automation.test.utils.common.FileManager.java
public void copyJarFile(String sourceFileLocationWithFileName, String destinationDirectory) throws IOException, URISyntaxException { File sourceFile = new File(getClass().getResource(sourceFileLocationWithFileName).toURI()); File destinationFileDirectory = new File(destinationDirectory); JarFile jarFile = new JarFile(sourceFile); String fileName = jarFile.getName(); String fileNameLastPart = fileName.substring(fileName.lastIndexOf(File.separator)); File destinationFile = new File(destinationFileDirectory, fileNameLastPart); JarOutputStream jarOutputStream = null; try {/*from w w w . j a va2s . co m*/ jarOutputStream = new JarOutputStream(new FileOutputStream(destinationFile)); Enumeration<JarEntry> entries = jarFile.entries(); InputStream inputStream = null; while (entries.hasMoreElements()) { try { JarEntry jarEntry = entries.nextElement(); inputStream = jarFile.getInputStream(jarEntry); //jarOutputStream.putNextEntry(jarEntry); //create a new jarEntry to avoid ZipException: invalid jarEntry compressed size jarOutputStream.putNextEntry(new JarEntry(jarEntry.getName())); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { jarOutputStream.write(buffer, 0, bytesRead); } } finally { if (inputStream != null) { inputStream.close(); jarOutputStream.flush(); jarOutputStream.closeEntry(); } } } } finally { if (jarOutputStream != null) { jarOutputStream.close(); } } }
From source file:org.kepler.kar.KARBuilder.java
/** * *///from w w w . j av a 2 s.c om private void writeKARFile() throws IOException { JarOutputStream jos = new JarOutputStream(new FileOutputStream(_karFile), _manifest); Iterator<KAREntry> li = _karItems.keySet().iterator(); while (li.hasNext()) { KAREntry entry = (KAREntry) li.next(); if (isDebugging) log.debug("Writing " + entry.getName()); try { jos.putNextEntry(entry); if (_karItems.get(entry) instanceof InputStream) { // inputstream from a bin file byte[] b = new byte[1024]; InputStream is = (InputStream) _karItems.get(entry); int numread = is.read(b, 0, 1024); while (numread != -1) { jos.write(b, 0, numread); numread = is.read(b, 0, 1024); } is.close(); // jos.flush(); jos.closeEntry(); } } catch (IOException ioe) { log.error(" Tried to write Duplicate Entry to kar " + entry.getName() + " " + entry.getLSID()); ioe.printStackTrace(); } } jos.flush(); jos.close(); log.info("done writing KAR file to " + _karFile.getAbsolutePath()); }