/** * Take the name of a jar file and extract the plugin.xml file, if possible, to a temporary file. * * @param f The jar file to extract from. * @return a temporary file to which the plugin.xml file has been copied. */ public static File unpackPluginXML(File f) { InputStream in = null; OutputStream out = null; try { JarFile jar = new JarFile(f); ZipEntry entry = jar.getEntry(PLUGIN_XML_FILE); if (entry == null) { return null; } File dest = File.createTempFile("jabref_plugin", ".xml"); dest.deleteOnExit(); in = new BufferedInputStream(jar.getInputStream(entry)); out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] buffer = new byte[2048]; for (; ; ) { int nBytes = in.read(buffer); if (nBytes <= 0) break; out.write(buffer, 0, nBytes); } out.flush(); return dest; } catch (IOException ex) { ex.printStackTrace(); return null; } finally { try { if (out != null) out.close(); if (in != null) in.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
/** * Copy a plugin to the user plugin directory. Does not check whether the plugin already exists. * * @param source The local or remote location to copy the plugin from. * @return true if the install was successful */ public static int copyPlugin(JFrame frame, URL source, String destFileName) { if (destFileName == null) destFileName = source.getFile(); if (!PluginCore.userPluginDir.exists()) { boolean created = PluginCore.userPluginDir.mkdirs(); if (!created) { return UNABLE_TO_CREATE_DIR; } } File destFile = new File(PluginCore.userPluginDir, destFileName); URLDownload ud = new URLDownload(frame, source, destFile); try { ud.download(); return SUCCESS; } catch (IOException e) { e.printStackTrace(); return UNABLE_TO_COPY_FILE; } }
public static int copyPlugin(JFrame frame, File source, String destFileName) { if (destFileName == null) destFileName = source.getName(); if (!PluginCore.userPluginDir.exists()) { boolean created = PluginCore.userPluginDir.mkdirs(); if (!created) { return UNABLE_TO_CREATE_DIR; } } File destFile = new File(PluginCore.userPluginDir, destFileName); BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(source)); out = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] buf = new byte[1024]; int count; while ((count = in.read(buf, 0, buf.length)) > 0) { out.write(buf, 0, count); } } catch (IOException ex) { ex.printStackTrace(); return UNABLE_TO_COPY_FILE; } finally { if (in != null) { try { in.close(); } catch (IOException ignore) { // UNABLE_TO_COPY_FILE; } } if (out != null) { try { out.close(); } catch (IOException ignore) { // UNABLE_TO_COPY_FILE; } } } return SUCCESS; }