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:lu.fisch.moenagade.model.Project.java
private void addToJar(JarOutputStream jo, String baseDir, File directory, String[] excludeExtention) throws FileNotFoundException, IOException { // get all files File[] files = directory.listFiles(); for (int f = 0; f < files.length; f++) { if (files[f].isDirectory()) { String entry = files[f].getAbsolutePath(); entry = entry.substring(directory.getAbsolutePath().length() + 1); addToJar(jo, baseDir + entry + "/", files[f], excludeExtention); } else {//from w ww . j a va 2 s .c o m //System.out.println("File = "+files[f].getAbsolutePath()); //System.out.println("List = "+Arrays.deepToString(excludeExtention)); //System.out.println("We got = "+getExtension(files[f])); if (!Arrays.asList(excludeExtention).contains(getExtension(files[f]))) { FileInputStream bi = new FileInputStream(files[f]); String entry = files[f].getAbsolutePath(); entry = entry.substring(directory.getAbsolutePath().length() + 1); entry = baseDir + entry; JarEntry je = new JarEntry(entry); jo.putNextEntry(je); byte[] buf = new byte[1024]; int anz; while ((anz = bi.read(buf)) != -1) { jo.write(buf, 0, anz); } bi.close(); } } } }
From source file:org.dspace.installer_edm.InstallerEDMConfEDMExport.java
/** * Recorre el war para escribir los archivos web.xml, la api de dspace y los jar de lucene * * @throws IOException/*from ww w. j a v a 2 s. c om*/ * @throws TransformerException */ private void writeNewJar() throws IOException, TransformerException { final int BUFFER_SIZE = 1024; // directorio de trabajo File jarDir = new File(this.eDMExportWarJarFile.getName()).getParentFile(); // archivo temporal del nuevo war File newJarFile = File.createTempFile("EDMExport", ".jar", jarDir); newJarFile.deleteOnExit(); // flujo de escritura para el nuevo war JarOutputStream jarOutputStream = new JarOutputStream(new FileOutputStream(newJarFile)); try { // recorrer los archivos del war Enumeration<JarEntry> entries = eDMExportWarJarFile.entries(); // libreras de lucene Pattern luceneLibPattern = Pattern.compile("^WEB-INF/lib/(lucene-.+?)-\\d+.+\\.jar$"); boolean newApiCopied = false; if (dspaceApi == null) newApiCopied = true; boolean replace = false; // recorrer while (entries.hasMoreElements()) { replace = false; installerEDMDisplay.showProgress('.'); JarEntry entry = entries.nextElement(); InputStream intputStream = null; // todos menos web.xml if (!entry.getName().equals("WEB-INF/web.xml")) { // api de dspace, se muestra la actual y la de dspace para pedir si se copia if (!newApiCopied && entry.getName().matches("^WEB-INF/lib/dspace-api-\\d+.+\\.jar$")) { String response = null; do { installerEDMDisplay.showLn(); installerEDMDisplay.showQuestion(currentStepGlobal, "writeNewJar.replace.question", new String[] { entry.getName(), "WEB-INF/lib/" + dspaceApi.getName(), dspaceApi.getAbsolutePath() }); response = br.readLine(); if (response == null) continue; response = response.trim(); if (response.isEmpty() || response.equalsIgnoreCase(answerYes)) { replace = true; break; } else if (response.equalsIgnoreCase("n")) { break; } } while (true); // se reemplaza por la de dspace if (replace) { JarEntry newJarEntry = new JarEntry("WEB-INF/lib/" + dspaceApi.getName()); newJarEntry.setCompressedSize(-1); jarOutputStream.putNextEntry(newJarEntry); intputStream = new FileInputStream(dspaceApi); newApiCopied = true; if (debug) { installerEDMDisplay.showLn(); installerEDMDisplay.showQuestion(currentStepGlobal, "writeNewJar.replace", new String[] { entry.getName(), "WEB-INF/lib/" + dspaceApi.getName(), dspaceApi.getAbsolutePath() }); installerEDMDisplay.showLn(); } } } else { // libreras de lucene Matcher luceneLibMatcher = luceneLibPattern.matcher(entry.getName()); if (luceneLibMatcher.find()) { String prefixLuceneLib = luceneLibMatcher.group(1); File luceneLibFile = null; String patternFile = prefixLuceneLib + "-\\d+.+\\.jar"; for (File file : luceneLibs) { if (file.getName().matches(patternFile)) { luceneLibFile = file; break; } } if (luceneLibFile != null) { String response = null; do { installerEDMDisplay.showLn(); installerEDMDisplay.showQuestion(currentStepGlobal, "writeNewJar.replace.question", new String[] { entry.getName(), "WEB-INF/lib/" + luceneLibFile.getName(), luceneLibFile.getAbsolutePath() }); response = br.readLine(); if (response == null) continue; response = response.trim(); if (response.isEmpty() || response.equalsIgnoreCase(answerYes)) { replace = true; break; } else if (response.equalsIgnoreCase("n")) { break; } } while (true); // se reemplaza por la de dspace if (replace) { JarEntry newJarEntry = new JarEntry("WEB-INF/lib/" + luceneLibFile.getName()); newJarEntry.setCompressedSize(-1); jarOutputStream.putNextEntry(newJarEntry); intputStream = new FileInputStream(luceneLibFile); if (debug) { installerEDMDisplay.showLn(); installerEDMDisplay.showQuestion(currentStepGlobal, "writeNewJar.replace", new String[] { entry.getName(), "WEB-INF/lib/" + luceneLibFile.getName(), luceneLibFile.getAbsolutePath() }); installerEDMDisplay.showLn(); } } } // si no era la api de dspace o las libreras de lucene se copia tal cual } else if (!replace) { JarEntry entryOld = new JarEntry(entry); entryOld.setCompressedSize(-1); jarOutputStream.putNextEntry(entryOld); intputStream = eDMExportWarJarFile.getInputStream(entry); } } if (intputStream == null) { if (debug) installerEDMDisplay.showQuestion(currentStepGlobal, "writeNewJar.notIS", new String[] { entry.getName() }); continue; } // se lee el archivo y se copia al flujo de escritura del war int count; byte data[] = new byte[BUFFER_SIZE]; while ((count = intputStream.read(data, 0, BUFFER_SIZE)) != -1) { jarOutputStream.write(data, 0, count); } intputStream.close(); } } installerEDMDisplay.showLn(); // se aade web.xml al war addNewWebXml(jarOutputStream); // cerramos el archivo jar y borramos el war eDMExportWarJarFile.close(); eDMExportWarWorkFile.delete(); // sustituimos el viejo por el temporal try { /*if (newJarFile.renameTo(eDMExportWarWorkFile) && eDMExportWarWorkFile.setExecutable(true, true)) { eDMExportWarWorkFile = new File(myInstallerWorkDirPath + fileSeparator + eDMExportWarFile.getName()); } else { throw new IOException(); }*/ if (jarOutputStream != null) jarOutputStream.close(); FileUtils.moveFile(newJarFile, eDMExportWarWorkFile); //newJarFile.renameTo(eDMExportWarWorkFile); eDMExportWarWorkFile.setExecutable(true, true); eDMExportWarWorkFile = new File( myInstallerWorkDirPath + fileSeparator + eDMExportWarFile.getName()); } catch (Exception io) { io.printStackTrace(); throw new IOException(); } } finally { if (jarOutputStream != null) { jarOutputStream.close(); } } }
From source file:com.sonicle.webtop.mail.Service.java
public void processGetAttachments(HttpServletRequest request, HttpServletResponse response) { MailAccount account = getAccount(request); String pfoldername = request.getParameter("folder"); String puidmessage = request.getParameter("idmessage"); String pids[] = request.getParameterValues("ids"); String providername = request.getParameter("provider"); String providerid = request.getParameter("providerid"); try {/*w w w. j a va 2 s.c o m*/ account.checkStoreConnected(); FolderCache mcache = null; Message m = null; if (providername == null) { mcache = account.getFolderCache(pfoldername); long newmsguid = Long.parseLong(puidmessage); m = mcache.getMessage(newmsguid); } else { mcache = fcProvided; m = mcache.getProvidedMessage(providername, providerid); } HTMLMailData mailData = mcache.getMailData((MimeMessage) m); String name = m.getSubject(); if (name == null) { name = "attachments"; } try { name = MailUtils.decodeQString(name); } catch (Exception exc) { } name += ".zip"; //prepare hashmap to hold already used pnames HashMap<String, String> pnames = new HashMap<String, String>(); ServletUtils.setFileStreamHeaders(response, "application/x-zip-compressed", DispositionType.INLINE, name); JarOutputStream jos = new java.util.jar.JarOutputStream(response.getOutputStream()); byte[] b = new byte[64 * 1024]; for (String pid : pids) { Part part = mailData.getAttachmentPart(Integer.parseInt(pid)); String pname = part.getFileName(); if (pname == null) { pname = "unknown"; } /* try { pname = MailUtils.decodeQString(pname, "iso-8859-1"); } catch (Exception exc) { } */ //keep name and extension String bpname = pname; String extpname = null; int ix = pname.lastIndexOf("."); if (ix > 0) { bpname = pname.substring(0, ix); extpname = pname.substring(ix + 1); } //check for existing pname and find an unused name int xid = 0; String rpname = pname; while (pnames.containsKey(rpname)) { rpname = bpname + " (" + (++xid) + ")"; if (extpname != null) rpname += "." + extpname; } JarEntry je = new JarEntry(rpname); jos.putNextEntry(je); if (providername == null) { Folder folder = mailData.getFolder(); if (!folder.isOpen()) { folder.open(Folder.READ_ONLY); } } InputStream is = part.getInputStream(); int len = 0; while ((len = is.read(b)) != -1) { jos.write(b, 0, len); } is.close(); //remember used pname pnames.put(rpname, rpname); } jos.closeEntry(); jos.flush(); jos.close(); } catch (Exception exc) { Service.logger.error("Exception", exc); } }
From source file:lu.fisch.unimozer.Diagram.java
public void jar() { try {/*from ww w . j a v a2s . co m*/ // compile all if (compile()) if (save()) { // adjust the dirname String dir = getDirectoryName(); if (!dir.endsWith(System.getProperty("file.separator"))) { dir += System.getProperty("file.separator"); } // adjust the filename String name = getDirectoryName(); if (name.endsWith(System.getProperty("file.separator"))) { name = name.substring(0, name.length() - 1); } name = name.substring(name.lastIndexOf(System.getProperty("file.separator")) + 1); /*String[] classNames = new String[classes.size()+1]; Set<String> set = classes.keySet(); Iterator<String> itr = set.iterator(); classNames[0]=null; int c = 1; while (itr.hasNext()) { classNames[c]=itr.next(); c++; }/**/ Vector<String> mains = getMains(); String[] classNames = new String[mains.size()]; for (int c = 0; c < mains.size(); c++) classNames[c] = mains.get(c); // default class to launch String mc = ""; { if (classNames.length == 0) { mc = ""; JOptionPane.showMessageDialog(printOptions, "Unimozer was unable to detect a startable class\n" + "inside your project. The JAR-archive will be created\n" + "but it won't be executable!", "Mainclass", JOptionPane.INFORMATION_MESSAGE, Unimozer.IMG_INFO); } else if (classNames.length == 1) mc = classNames[0]; else mc = (String) JOptionPane.showInputDialog(frame, "Unimozer detected more than one runnable class.\n" + "Please select which one you want to be launched\n" + "automatically with the JAR-archive.", "Autostart", JOptionPane.QUESTION_MESSAGE, Unimozer.IMG_QUESTION, classNames, ""); } // target JVM String target = null; if (Runtime5.getInstance().usesSunJDK() && mc != null) { String[] targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6" }; if (System.getProperty("java.version").startsWith("1.7")) targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6", "1.7" }; if (System.getProperty("java.version").startsWith("1.8")) targets = new String[] { "1.1", "1.2", "1.3", "1.5", "1.6", "1.7", "1.8" }; target = (String) JOptionPane.showInputDialog(frame, "Please enter version of the JVM you want to target.", "Target JVM", JOptionPane.QUESTION_MESSAGE, Unimozer.IMG_QUESTION, targets, "1.6"); } // make the class-files and all // related stuff if (((Runtime5.getInstance().usesSunJDK() && target != null) || (!Runtime5.getInstance().usesSunJDK())) && (mc != null)) if (makeInteractive(false, target, false) == true) { StringList manifest = new StringList(); manifest.add("Manifest-Version: 1.0"); manifest.add("Created-By: " + Unimozer.E_VERSION + " " + Unimozer.E_VERSION); manifest.add("Name: " + name); if (mc != null) { manifest.add("Main-Class: " + mc); } // compose the filename File fDir = new File(dir + "dist" + System.getProperty("file.separator")); fDir.mkdir(); name = dir + "dist" + System.getProperty("file.separator") + name + ".jar"; String baseName = dir; String libFolderName = dir + "lib"; String distLibFolderName = dir + "dist" + System.getProperty("file.separator") + "lib"; File outFile = new File(name); FileOutputStream bo = new FileOutputStream(name); JarOutputStream jo = new JarOutputStream(bo); String dirname = getDirectoryName(); if (!dirname.endsWith(System.getProperty("file.separator"))) { dirname += System.getProperty("file.separator"); } // add the files to the array addToJar(jo, "", new File(dirname + "bin" + System.getProperty("file.separator"))); // add the files to the array addToJar(jo, "", new File(dirname + "src" + System.getProperty("file.separator")), new String[] { "java" }); /* // define a filter for files that do not start with a dot FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { return !name.startsWith("."); } }; // get the bin directory File binDir = new File(dirname+"bin"+System.getProperty("file.separator")); // get all files File[] files = binDir.listFiles(filter); for(int f=0;f<files.length;f++) { FileInputStream bi = new FileInputStream(files[f]); String entry = files[f].getAbsolutePath(); entry = entry.substring(binDir.getAbsolutePath().length()+1); JarEntry je = new JarEntry(entry); jo.putNextEntry(je); byte[] buf = new byte[1024]; int anz; while ((anz = bi.read(buf)) != -1) { jo.write(buf, 0, anz); } bi.close(); } */ // ask to include another direectory // directory filter /* FilenameFilter dirFilter = new FilenameFilter() { public boolean accept(File dir, String name) { File isDir = new File(dir+System.getProperty("file.separator")+name); return isDir.isDirectory() && !name.equals("bin") && !name.equals("src") && !name.equals("dist") && !name.equals("nbproject") && !name.equals("doc"); } }; // get directories File projectDir = new File(dirname); String[] subDirs = projectDir.list(dirFilter); if(subDirs.length>0) { String subdir = (String) JOptionPane.showInputDialog( frame, "Do you want to include any other resources directory?\n"+ "Click ?Cancel? to not include any resources directory!", "JAR Packager", JOptionPane.QUESTION_MESSAGE, Unimozer.IMG_QUESTION, subDirs, null); if(subdir!=null) { addToJar(jo,subdir+"/",new File(dirname+subdir+System.getProperty("file.separator"))); } } */ /* Set<String> set = classes.keySet(); Iterator<String> itr = set.iterator(); int i = 0; while (itr.hasNext()) { String classname = itr.next(); String act = classname + ".class"; FileInputStream bi = new FileInputStream(dirname+"bin"+System.getProperty("file.separator")+act); JarEntry je = new JarEntry(act); jo.putNextEntry(je); byte[] buf = new byte[1024]; int anz; while ((anz = bi.read(buf)) != -1) { jo.write(buf, 0, anz); } bi.close(); } */ // copy libs File lib = new File(libFolderName); File distLib = new File(distLibFolderName); StringList libs = null; if (lib.exists()) { libs = CopyDirectory.copyFolder(lib, distLib); } String cp = new String(); if (libs != null) { for (int i = 0; i < libs.count(); i++) { String myLib = libs.get(i); myLib = myLib.substring(baseName.length()); if (i != 0) cp = cp + " "; cp = cp + myLib; } //manifest.add("Class-Path: "+cp); } // Let's search for the path of the swing-layout JAR file String cpsw = ""; if (getCompleteSourceCode().contains("org.jdesktop.layout")) { if (Main.classpath != null) { // copy the file String src = Main.classpath; File f1 = new File(src); String dest = distLibFolderName + System.getProperty("file.separator") + f1.getName(); // create folder if not exists File f2 = new File(distLibFolderName); if (!f2.exists()) f2.mkdir(); // copy the file InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); // add the manifest entry cpsw = "lib" + System.getProperty("file.separator") + f1.getName(); } } manifest.add("Class-Path: " + cp + " " + cpsw); // adding the manifest file manifest.add(""); JarEntry je = new JarEntry("META-INF/MANIFEST.MF"); jo.putNextEntry(je); String mf = manifest.getText(); jo.write(mf.getBytes(), 0, mf.getBytes().length); jo.close(); bo.close(); cleanAll(); JOptionPane.showMessageDialog(frame, "The JAR-archive has been generated ...", "Success", JOptionPane.INFORMATION_MESSAGE, Unimozer.IMG_INFO); } } } /*catch (ClassNotFoundException ex) { JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...", "Error :: ClassNotFoundException", JOptionPane.ERROR_MESSAGE,Unimozer.IMG_ERROR); }*/ catch (IOException ex) { JOptionPane.showMessageDialog(frame, "There was an error while creating the JAR-archive ...", "Error :: IOException", JOptionPane.ERROR_MESSAGE, Unimozer.IMG_ERROR); } }