一怒之下写了ViewCVS的批量下载器
今天找Protege4.0的源码,找来找去也看不到SVN或者CVS的地址,只提供了一个WEB界面的ViewCVS,不知道是不是我比较傻,于是为了能批量签出,自己写了个批量下载器。
该下载器的功能是能从编译过的jar文件中解析出文件路径推算出类的包全名,然后根据规律去ViewCVS用http协议进行签出,签出后按照包的存放地址依次到位。
- import java.io.File;
- import java.util.ArrayList;
- public class FileManager {
- String dir = "";
- String temp = "";
- String filesName = "";
- public String[] serachFiles(String dir) {
- File root = new File(dir);
- File[] filesOrDirs = root.listFiles();
- //String[] result = new String[1024];
- for (int i = 0; i < filesOrDirs.length; i++) {
- if (filesOrDirs[i].isDirectory()) {
- //filesName +=filesOrDirs[i].getName()+".";
- serachFiles(filesOrDirs[i].getAbsolutePath());
- } else {
- //result[i] = filesOrDirs[i].getName();
- temp += "http://smi-protege.stanford.edu/svn/*checkout*/protege4/protege-standalone/trunk/plugins/org.protege.editor.owl/src/"
- +filesOrDirs[i].getAbsolutePath().substring(filesOrDirs[i].getAbsolutePath().indexOf("org.protege.editor.owl")+23, filesOrDirs[i].getAbsolutePath().length()).replace("\\", "/") + ",";
- }
- }
- return temp.split(",");
- }
- /** *//**
- * @param args
- */
- public static void main(String[] args) {
- FileManager fm = new FileManager();
- String[] files = fm.serachFiles("D:\\aa\\org.protege.editor.owl");//手动解压缩jar
- for (int i = 0; i < files.length; i++) {
- if(files[i].indexOf(".class")>0&&files[i].indexOf("$")<0)
- System.out.println(files[i].replaceAll("class", "java"));
- }
- }
- public ArrayList<String> getURLs(){
- String[] files = serachFiles("D:\\aa");
- ArrayList<String> URLs = new ArrayList<String>();
- for (int i = 0; i < files.length; i++) {
- if(files[i].indexOf(".class")>0&&files[i].indexOf("$")<0)
- URLs.add(files[i].replaceAll("class", "java"));
- }
- return URLs;
- }
- }
- import java.io.*;
- import java.net.*;
- import java.util.ArrayList;
- public class FileDownload {
- public static void download(String address, String localFileName) {
- String temp = localFileName;
- String localPar = localFileName.substring(0, localFileName.lastIndexOf("\\")+1);
- localFileName = localFileName.substring(localFileName.lastIndexOf("\\")+1,localFileName.length());
- (new File(localPar)).mkdirs();
- OutputStream out = null;
- URLConnection conn = null;
- InputStream in = null;
- try {
- URL url = new URL(address);
- out = new BufferedOutputStream(
- new FileOutputStream(temp));
- conn = url.openConnection();
- in = conn.getInputStream();
- byte[] buffer = new byte[1024];
- int numRead;
- long numWritten = 0;
- while ((numRead = in.read(buffer)) != -1) {
- out.write(buffer, 0, numRead);
- numWritten += numRead;
- }
- System.out.println(temp + "\t" + numWritten);
- } catch (Exception exception) {
- exception.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (IOException ioe) {
- }
- }
- }
- public static void download(String address) {
- int lastSlashIndex = address.lastIndexOf(‘/‘);
- if (lastSlashIndex >= 0 &&
- lastSlashIndex < address.length() - 1) {
- download(address, address.substring(lastSlashIndex + 1));
- } else {
- System.err.println("Could not figure out local file name for " +
- address);
- }
- }
- public static void main(String[] args) {
- FileManager fm = new FileManager();
- ArrayList<String> urls = fm.getURLs();
- for(String url : urls){
- //System.out.println(url);
- download(url,url.replaceFirst("http://smi-protege.stanford.edu/svn/\\*checkout\\*/protege4/protege-standalone/trunk/plugins/", "d:/").replace("/", "\\"));
- }
- // download("aa","d:\\aa\\bb");
- // for (int i = 0; i < args.length; i++) {
- // download(args[i]);
- // }
- }
- }