首页 > 大杂烩 > 一怒之下写了ViewCVS的批量下载器

一怒之下写了ViewCVS的批量下载器

2008年9月28日

今天找Protege4.0的源码,找来找去也看不到SVN或者CVS的地址,只提供了一个WEB界面的ViewCVS,不知道是不是我比较傻,于是为了能批量签出,自己写了个批量下载器。

该下载器的功能是能从编译过的jar文件中解析出文件路径推算出类的包全名,然后根据规律去ViewCVS用http协议进行签出,签出后按照包的存放地址依次到位。

  1. import java.io.File;
  2. import java.util.ArrayList;
  3.  
  4. public class FileManager {
  5.  
  6.     String dir = "";
  7.  
  8.     String temp = "";
  9.    
  10.     String filesName = "";
  11.  
  12.     public String[] serachFiles(String dir) {
  13.  
  14.         File root = new File(dir);
  15.  
  16.         File[] filesOrDirs = root.listFiles();
  17.  
  18.         //String[] result = new String[1024];
  19.  
  20.         for (int i = 0; i < filesOrDirs.length; i++) {
  21.             if (filesOrDirs[i].isDirectory()) {
  22.                 //filesName +=filesOrDirs[i].getName()+".";
  23.                 serachFiles(filesOrDirs[i].getAbsolutePath());
  24.             } else {
  25.                 //result[i] = filesOrDirs[i].getName();
  26.  
  27.                 temp += "http://smi-protege.stanford.edu/svn/*checkout*/protege4/protege-standalone/trunk/plugins/org.protege.editor.owl/src/"
  28.                     +filesOrDirs[i].getAbsolutePath().substring(filesOrDirs[i].getAbsolutePath().indexOf("org.protege.editor.owl")+23, filesOrDirs[i].getAbsolutePath().length()).replace("\\", "/") + ",";
  29.                
  30.             }
  31.         }
  32.  
  33.         return temp.split(",");
  34.  
  35.     }
  36.  
  37.     /** *//**
  38.      * @param args
  39.      */
  40.     public static void main(String[] args) {
  41.         FileManager fm = new FileManager();
  42.         String[] files = fm.serachFiles("D:\\aa\\org.protege.editor.owl");//手动解压缩jar
  43.         for (int i = 0; i < files.length; i++) {
  44.             if(files[i].indexOf(".class")>0&&files[i].indexOf("$")<0)
  45.             System.out.println(files[i].replaceAll("class", "java"));
  46.         }
  47.  
  48.     }
  49.    
  50.     public ArrayList<String> getURLs(){
  51.         String[] files = serachFiles("D:\\aa");
  52.         ArrayList<String> URLs = new ArrayList<String>();
  53.         for (int i = 0; i < files.length; i++) {
  54.             if(files[i].indexOf(".class")>0&&files[i].indexOf("$")<0)
  55.                 URLs.add(files[i].replaceAll("class", "java"));
  56.         }
  57.         return URLs;
  58.        
  59.        
  60.     }
  61.    
  62. }
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.ArrayList;
  4.  
  5.  
  6. public class FileDownload {
  7.     public static void download(String address, String localFileName) {
  8.         String temp = localFileName;
  9.         String localPar = localFileName.substring(0, localFileName.lastIndexOf("\\")+1);       
  10.         localFileName = localFileName.substring(localFileName.lastIndexOf("\\")+1,localFileName.length());
  11.         (new File(localPar)).mkdirs();
  12.         OutputStream out = null;
  13.         URLConnection conn = null;
  14.         InputStream  in = null;
  15.         try {
  16.             URL url = new URL(address);
  17.  
  18.             out = new BufferedOutputStream(
  19.                 new FileOutputStream(temp));
  20.             conn = url.openConnection();
  21.             in = conn.getInputStream();
  22.             byte[] buffer = new byte[1024];
  23.             int numRead;
  24.             long numWritten = 0;
  25.             while ((numRead = in.read(buffer)) != -1) {
  26.                 out.write(buffer, 0, numRead);
  27.                 numWritten += numRead;
  28.             }
  29.             System.out.println(temp + "\t" + numWritten);
  30.         } catch (Exception exception) {
  31.             exception.printStackTrace();
  32.         } finally {
  33.             try {
  34.                 if (in != null) {
  35.                     in.close();
  36.                 }
  37.                 if (out != null) {
  38.                     out.close();
  39.                 }
  40.             } catch (IOException ioe) {
  41.             }
  42.         }
  43.     }
  44.  
  45.     public static void download(String address) {
  46.         int lastSlashIndex = address.lastIndexOf(/);
  47.         if (lastSlashIndex >= 0 &&
  48.             lastSlashIndex < address.length() - 1) {
  49.             download(address, address.substring(lastSlashIndex + 1));
  50.         } else {
  51.             System.err.println("Could not figure out local file name for " +
  52.                 address);
  53.         }
  54.     }
  55.  
  56.     public static void main(String[] args) {
  57.         FileManager fm = new FileManager();
  58.         ArrayList<String> urls = fm.getURLs();
  59.         for(String url : urls){
  60.             //System.out.println(url);
  61.             download(url,url.replaceFirst("http://smi-protege.stanford.edu/svn/\\*checkout\\*/protege4/protege-standalone/trunk/plugins/", "d:/").replace("/", "\\"));
  62.            
  63.         }
  64. //        download("aa","d:\\aa\\bb");
  65. //        for (int i = 0; i < args.length; i++) {
  66. //            download(args[i]);
  67. //        }
  68.     }
  69. }

大杂烩

  1. 目前还没有任何评论.
  1. 目前还没有任何 trackbacks 和 pingbacks.