List of usage examples for java.util.jar JarOutputStream write
public synchronized void write(byte[] b, int off, int len) throws IOException
From source file:com.migratebird.script.repository.impl.ArchiveScriptLocation.java
/** * Writes the entry with the given name and content to the given {@link JarOutputStream} * * @param jarOutputStream {@link OutputStream} to the jar file * @param name Name of the jar file entry * @param timestamp Last modification date of the entry * @param entryContentReader Reader giving access to the content of the jar entry * @throws IOException In case of disk IO problems *//*from w w w. ja va2 s .c o m*/ protected void writeJarEntry(JarOutputStream jarOutputStream, String name, long timestamp, Reader entryContentReader) throws IOException { JarEntry jarEntry = new JarEntry(name); jarEntry.setTime(timestamp); jarOutputStream.putNextEntry(jarEntry); InputStream scriptInputStream = new ReaderInputStream(entryContentReader); byte[] buffer = new byte[1024]; int len; while ((len = scriptInputStream.read(buffer, 0, buffer.length)) > -1) { jarOutputStream.write(buffer, 0, len); } scriptInputStream.close(); jarOutputStream.closeEntry(); }
From source file:edu.stanford.muse.email.JarDocCache.java
@Override public synchronized void saveHeader(Document d, String prefix, int msgNum) throws FileNotFoundException, IOException { JarOutputStream jos = getHeadersJarOS(prefix); // create the bytes ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream headerOOS = new ObjectOutputStream(baos); headerOOS.writeObject(d);/*from ww w.j a va2 s .c o m*/ byte buf[] = baos.toByteArray(); ZipEntry ze = new ZipEntry(msgNum + ".header"); ze.setMethod(ZipEntry.DEFLATED); jos.putNextEntry(ze); jos.write(buf, 0, buf.length); jos.closeEntry(); jos.flush(); }
From source file:net.adamcin.oakpal.testing.TestPackageUtil.java
private static void add(final File root, final File source, final JarOutputStream target) throws IOException { if (root == null || source == null) { throw new IllegalArgumentException("Cannot add from a null file"); }//from w ww. j a v a 2 s . co m if (!(source.getPath() + "/").startsWith(root.getPath() + "/")) { throw new IllegalArgumentException("source must be the same file or a child of root"); } final String relPath; if (!root.getPath().equals(source.getPath())) { relPath = source.getPath().substring(root.getPath().length() + 1).replace(File.separator, "/"); } else { relPath = ""; } if (source.isDirectory()) { if (!relPath.isEmpty()) { String name = relPath; if (!name.endsWith("/")) { name += "/"; } JarEntry entry = new JarEntry(name); entry.setTime(source.lastModified()); target.putNextEntry(entry); target.closeEntry(); } File[] children = source.listFiles(); if (children != null) { for (File nestedFile : children) { add(root, nestedFile, target); } } } else { JarEntry entry = new JarEntry(relPath); entry.setTime(source.lastModified()); target.putNextEntry(entry); try (InputStream in = new BufferedInputStream(new FileInputStream(source))) { byte[] buffer = new byte[1024]; while (true) { int count = in.read(buffer); if (count == -1) break; target.write(buffer, 0, count); } target.closeEntry(); } } }
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 w ww . j a v a 2 s .c om * @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:UnpackedJarFile.java
public static void copyToPackedJar(JarFile inputJar, File outputFile) throws IOException { if (inputJar.getClass() == JarFile.class) { // this is a plain old jar... nothign special copyFile(new File(inputJar.getName()), outputFile); } else if (inputJar instanceof NestedJarFile && ((NestedJarFile) inputJar).isPacked()) { NestedJarFile nestedJarFile = (NestedJarFile) inputJar; JarFile baseJar = nestedJarFile.getBaseJar(); String basePath = nestedJarFile.getBasePath(); if (baseJar instanceof UnpackedJarFile) { // our target jar is just a file in upacked jar (a plain old directory)... now // we just need to find where it is and copy it to the outptu copyFile(((UnpackedJarFile) baseJar).getFile(basePath), outputFile); } else {// w w w . j a va 2s . c o m // out target is just a plain old jar file directly accessabel from the file system copyFile(new File(baseJar.getName()), outputFile); } } else { // copy out the module contents to a standalone jar file (entry by entry) JarOutputStream out = null; try { out = new JarOutputStream(new FileOutputStream(outputFile)); byte[] buffer = new byte[4096]; Enumeration entries = inputJar.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); InputStream in = inputJar.getInputStream(entry); try { out.putNextEntry(new ZipEntry(entry.getName())); try { int count; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } } finally { out.closeEntry(); } } finally { close(in); } } } finally { close(out); } } }
From source file:CreateJarFile.java
protected void createJarArchive(File archiveFile, File[] tobeJared) { try {//from www. j ava 2s . c om byte buffer[] = new byte[BUFFER_SIZE]; // Open archive file FileOutputStream stream = new FileOutputStream(archiveFile); JarOutputStream out = new JarOutputStream(stream, new Manifest()); for (int i = 0; i < tobeJared.length; i++) { if (tobeJared[i] == null || !tobeJared[i].exists() || tobeJared[i].isDirectory()) continue; // Just in case... System.out.println("Adding " + tobeJared[i].getName()); // Add archive entry JarEntry jarAdd = new JarEntry(tobeJared[i].getName()); jarAdd.setTime(tobeJared[i].lastModified()); out.putNextEntry(jarAdd); // Write file to archive FileInputStream in = new FileInputStream(tobeJared[i]); while (true) { int nRead = in.read(buffer, 0, buffer.length); if (nRead <= 0) break; out.write(buffer, 0, nRead); } in.close(); } out.close(); stream.close(); System.out.println("Adding completed OK"); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Error: " + ex.getMessage()); } }
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); }/*w w w . j ava 2s .c om*/ inputStream.close(); jarOutputStream.flush(); jarOutputStream.closeEntry(); } jarOutputStream.close(); }
From source file:au.com.addstar.objects.Plugin.java
/** * Adds the Spigot.ver file to the jar/* ww w . j av a 2s . c o m*/ * * @param ver */ public void addSpigotVer(String ver) { if (latestFile == null) return; File newFile = new File(latestFile.getParentFile(), SpigotUpdater.getFormat().format(Calendar.getInstance().getTime()) + "-" + ver + "-s.jar"); File spigotFile = new File(latestFile.getParentFile(), "spigot.ver"); if (spigotFile.exists()) FileUtils.deleteQuietly(spigotFile); try { JarFile oldjar = new JarFile(latestFile); if (oldjar.getEntry("spigot.ver") != null) return; JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(newFile)); try (Writer wr = new FileWriter(spigotFile); BufferedWriter writer = new BufferedWriter(wr)) { writer.write(ver); writer.newLine(); } try (FileInputStream stream = new FileInputStream(spigotFile)) { byte[] buffer = new byte[1024]; int bytesRead = 0; JarEntry je = new JarEntry(spigotFile.getName()); tempJarOutputStream.putNextEntry(je); while ((bytesRead = stream.read(buffer)) != -1) { tempJarOutputStream.write(buffer, 0, bytesRead); } stream.close(); } Enumeration jarEntries = oldjar.entries(); while (jarEntries.hasMoreElements()) { JarEntry entry = (JarEntry) jarEntries.nextElement(); InputStream entryInputStream = oldjar.getInputStream(entry); tempJarOutputStream.putNextEntry(entry); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = entryInputStream.read(buffer)) != -1) { tempJarOutputStream.write(buffer, 0, bytesRead); } entryInputStream.close(); } tempJarOutputStream.close(); oldjar.close(); FileUtils.deleteQuietly(latestFile); FileUtils.deleteQuietly(spigotFile); FileUtils.moveFile(newFile, latestFile); latestFile = newFile; } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.apache.sqoop.integration.connectorloading.ClasspathTest.java
private void addFileToJar(File source, JarOutputStream target) throws Exception { JarEntry entry = new JarEntry(source.getName()); entry.setTime(source.lastModified()); target.putNextEntry(entry);//from ww w .j a v a2 s . com BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); long bufferSize = source.length(); if (bufferSize < Integer.MIN_VALUE || bufferSize > Integer.MAX_VALUE) { throw new RuntimeException("file to large to be added to jar"); } byte[] buffer = new byte[(int) bufferSize]; while (true) { int count = in.read(buffer); if (count == -1) break; target.write(buffer, 0, count); } target.closeEntry(); if (in != null) in.close(); }
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 {//w w w .ja v a 2s . com 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(); } } }