安装程序的工作:将源文件复制到相应的目录。
升级程序的工作:将源文件中更新过的文件覆盖目的文件,增加的文件复制到相应的目录。
卸载程序的工作:将程序文件夹的内容删除。
针对以上内容,写一个简单的安装程序
(主要文件:installsheildimpl,用递归的方式进行了文件夹的遍历)
/***********************************************************************
* module: installsheild.java
* author: administrator
* created: 2004-12-13 22:37:53
* purpose: 安装程序接口,用于统一调用方式。
***********************************************************************/
package org.heixue.test.install;
/**
* @author administrator
*/
public interface installsheild {
public final static int install=1;
public final static int update=2;
public final static int uninstall=3;
public void install(int type,string srcfold,string destfold) throws installexception ;
}
/***********************************************************************
* module: installsheildimpl.java
* author: administrator
* created: 2004-12-13 22:48:20
* purpose: 安装程序的实现
***********************************************************************/
package org.heixue.test.install;
import java.io.*;
//import org.heixue.util.log;
import org.heixue.util.filelog;
import org.heixue.util.file.filecopy;
/**
* @author administrator
*/
public class installsheildimpl implements installsheild {
private string srcfold=null;
private string destfold=null;
private filelog log=null;
/**
*
*/
public installsheildimpl() {
}
/*
* @see org.heixue.test.update.installsheild#install(java.lang.string, java.lang.string, int)
*/
public void install(int type, string srcfold, string destfold) throws installexception {
this.srcfold=srcfold;
this.destfold=destfold;
if(config.getout()!=null)
log=new filelog(config.getout());
else
log=new filelog(system.out);
if(destfold==null) throw new installexception("您没有设置目的文件夹位置!");
switch(type){
case installsheild.install: if(srcfold==null) throw new installexception("您没有设置源文件夹位置!");doinstall();break;
case installsheild.update: if(srcfold==null) throw new installexception("您没有设置源文件夹位置!");doupdate();break;
case installsheild.uninstall:douninstall();break;
default:throw new installexception("没有这项操作!");
}
}
/**
* :
* #perpose: 安装程序,主要进行文件的拷贝.
*/
public void doinstall() throws installexception{
if(srcfold==null) throw new installexception("您没有设置源文件夹位置!");
if(destfold==null) throw new installexception("您没有设置目的文件夹位置!");
file file1=new file(srcfold);
file file2=new file(destfold);
if(!file2.exists()) file2.mkdir();
installfiles("","");
}
private void installfiles(string src,string dest) throws installexception{
file file1=new file(srcfold,src);
file file2=new file(destfold,dest);
if(file1.isfile()){
log.info(file2.getpath());
filecopy.copybyfile(file1,file2);
}else if(file1.isdirectory()){
if(!file2.exists()) file2.mkdir();
log.info(file2.getpath());
file[] fs=file1.listfiles();
for(int i=0;i<fs.length;i++){
string strpath=fs[i].getpath().substring(srcfold.length()+1);
installfiles(strpath,strpath);
}
}else{
throw new installexception("不存在该文件或目录!");
}
}
/**
* :
* #perpose: 升级程序,根据文件的创建日期进行判断更新
*/
public void doupdate() throws installexception{
if(srcfold==null) throw new installexception("您没有设置源文件夹位置!");
if(destfold==null) throw new installexception("您没有设置目的文件夹位置!");
file file1=new file(srcfold);
file file2=new file(destfold);
if(!file2.exists()) file2.mkdir();
updatefiles("","");
}
private void updatefiles(string src,string dest) throws installexception{
file file1=new file(srcfold,src);
file file2=new file(destfold,dest);
if(file1.isfile()){
if(!file2.exists()||file1.lastmodified()>file2.lastmodified()){
log.info(file2.getpath());
filecopy.copybyfile(file1,file2);
}
}else if(file1.isdirectory()){
if(!file2.exists()) file2.mkdir();
log.info(file2.getpath());
file[] fs=file1.listfiles();
for(int i=0;i<fs.length;i++){
string strpath=fs[i].getpath().substring(srcfold.length()+1);
updatefiles(strpath,strpath);
}
}else{
throw new installexception("不存在该文件或目录!");
}
}
/**
* :
* #perpose: 卸载程序,将目的文件夹下所有文件删除
*/
public void douninstall() throws installexception{
if(destfold==null) throw new installexception("您没有设置目的文件夹位置!");
deletefiles("");
}
private void deletefiles(string dest) throws installexception{
file file1=new file(destfold,dest);
if(file1.isfile()){
file1.delete();
log.info(file1.getpath());
}else if(file1.isdirectory()){
file[] fs=file1.listfiles();
for(int i=0;i<fs.length;i++){
string strpath=fs[i].getpath().substring(srcfold.length()+1);
deletefiles(strpath);
}
file1.delete();
log.info(file1.getpath());
}else{
throw new installexception("不存在该文件或目录!");
}
}
public static void main(string[] args) throws installexception{
installsheildimpl isi=new installsheildimpl();
isi.install(installsheild.install,"d:\\test","d:\\test2");
//isi.install(installsheild.update,"d:\\test","d:\\test2");
//isi.install(installsheild.uninstall,"d:\\test","d:\\test2");
}
}
/***********************************************************************
* module: config.java
* author: administrator
* created: 2004-12-14 10:24:06
* purpose: 一些配置信息
***********************************************************************/
package org.heixue.test.install;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.outputstream;
/**
* @author administrator
*/
public class config {
private static int installtype=0;
private static string srcfold=null;
private static string destfold=null;
private static outputstream out=null;
private static config _config = null;
/**
*
*/
public void initialize(int type,string src,string dest,string logpath) throws filenotfoundexception {
if(logpath!=null)
out = new fileoutputstream(logpath);
initialize(type,src,dest,out);
}
/**
* @param type :安装类型
* @param src :源文件夹
* @param dest :目标文件夹
* @param o :日志流输出
*/
public void initialize(int type,string src,string dest,outputstream o){
installtype=type;
srcfold=src;
destfold=dest;
out=o;
}
/**
* @return:
* #perpose:
*/
public static string getdestfold() {
return destfold;
}
/**
* @return:
* #perpose:
*/
public static int getinstalltype() {
return installtype;
}
/**
* @return:
* #perpose:
*/
public static outputstream getout() {
return out;
}
/**
* @return:
* #perpose:
*/
public static string getsrcfold() {
return srcfold;
}
/**
* @return:
* #perpose:
*/
public static config getinstance() {
if(_config==null)
_config = new config();
return _config;
}
}
/***********************************************************************
* module: installexception.java
* author: administrator
* created: 2004-12-13 22:53:25
* purpose: 安装过程异常
***********************************************************************/
package org.heixue.test.install;
/**
* @author administrator
*/
public class installexception extends exception {
/**
*
*/
public installexception() {
super();
}
public installexception(string reason) {
super(reason);
}
}
/***********************************************************************
* module: install.java
* author: administrator
* created: 2004-12-14 10:53:28
* purpose: 安装文件的类,输入参数:安装类型,源文件夹,目标文件夹,日志文件
***********************************************************************/
package org.heixue.test.install;
import java.io.filenotfoundexception;
/**
* @author administrator
*/
public class install {
/**
*
*/
public install() {
super();
// todo auto-generated constructor stub
}
public static void main(string[] args) throws filenotfoundexception, installexception{
int installtype=0;
string srcfold=null;
string destfold=null;
string logpath=null;
if(args.length==4){
if(args[0].equals("install")){
installtype=installsheild.install;
}else if(args[0].equals("update")){
installtype=installsheild.update;
}else if(args[0].equals("uninstall")){
installtype=installsheild.uninstall;
}else{
}
srcfold=args[1];
destfold=args[2];
logpath=args[3];
if(logpath.equals("null"))
logpath=null;
config.getinstance().initialize(installtype,srcfold,destfold,logpath);
installsheild is = new installsheildimpl();
is.install(installtype,srcfold,destfold);
}else{
system.out.println("command line:java install type srcfold destfold logpath");
system.out.println("eg:java install install d:\\test d:\\test2 d:\\install.log");
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
其下是两个工具类
/***********************************************************************
* module: filecopy.java
* author: administrator
* created: 2004-12-6 22:20:15
* purpose: 文件复制
***********************************************************************/
package org.heixue.util.file;
import java.io.*;
/**
* @author administrator
*/
public class filecopy {
/**
*
*/
public filecopy() {
}
public static boolean copy(string src,string dest){
try{
//instance the file as file_in and file_out
java.io.file file_in=new java.io.file(src);
java.io.file file_out=new java.io.file(dest);
fileinputstream in1=new fileinputstream(file_in);
fileoutputstream out1=new fileoutputstream(file_out);
byte[] bytes=new byte[1024];
int c;
while((c=in1.read(bytes))!=-1)
out1.write(bytes,0,c);
in1.close();
out1.close();
return(true); //if success then return true
}
catch(exception e)
{
system.out.println("error!");
return(false); //if fail then return false
}
}
public static boolean copybyfile(file src,file dest){
try{
//instance the file as file_in and file_out
fileinputstream in1=new fileinputstream(src);
fileoutputstream out1=new fileoutputstream(dest);
byte[] bytes=new byte[1024];
int c;
while((c=in1.read(bytes))!=-1)
out1.write(bytes,0,c);
in1.close();
out1.close();
return(true); //if success then return true
}
catch(exception e)
{
system.out.println(e.tostring());
return(false); //if fail then return false
}
}
}
/***********************************************************************
* module: filelog.java
* author: administrator
* created: 2004-12-6 22:20:15
* purpose: 产生文件日志
***********************************************************************/
package org.heixue.util;
import java.io.filenotfoundexception;
import java.io.ioexception;
/**
* @author heixue
*
*/
public class filelog {
private string logfilepath="/usr/tomcat_log.txt";//"c:\\tomcat_log.txt"
java.io.outputstream outputstream=null;
/**
*
*/
public filelog() {
super();
}
/**
* @param stream:输出流
*/
public filelog(java.io.outputstream stream) {
outputstream=stream;
}
/**
* @param filepath:文件路径
*/
public filelog(string filepath) {
super();
try {
outputstream= new java.io.fileoutputstream(filepath,true);
} catch (filenotfoundexception e) {
e.printstacktrace();
}
}
public void error(string str){
java.util.date date=new java.util.date();
string time=(date.getyear()+1900)+"-"+(date.getmonth()+1)+"-"+date.getdate()+" "+date.gethours()+":"+date.getminutes()+":"+date.getseconds();
str="error:"+time+"–>"+str+"\n";
if(outputstream!=null) try {
this.outputstream.write(str.getbytes());
this.outputstream.flush();
} catch (ioexception e) {
e.printstacktrace();
}
}
public void info(string str){
java.util.date date=new java.util.date();
string time=(date.getyear()+1900)+"-"+(date.getmonth()+1)+"-"+date.getdate()+" "+date.gethours()+":"+date.getminutes()+":"+date.getseconds();
str="info:"+time+"–>"+str+"\n";
if(outputstream!=null) try {
this.outputstream.write(str.getbytes());
this.outputstream.flush();
} catch (ioexception e) {
e.printstacktrace();
}
}
public static void main(string[] args) {
//filelog fl=new filelog();
//fl.setoutputstream(system.out);
//fl.error("error occours!逆势");
filelog fl=new filelog("c:/log.txt");
fl.error("error occours!逆势");
}
/**
* @return
*/
public string getlogfilepath() {
return logfilepath;
}
/**
* @param string
*/
public void setlogfilepath(string string) {
logfilepath = string;
}
/**
* @return
*/
public java.io.outputstream getoutputstream() {
return outputstream;
}
/**
* @param stream
*/
public void setoutputstream(java.io.outputstream stream) {
outputstream = stream;
}
}
