服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

程序安装,升级及卸载核心部分(java编写)


  安装程序的工作:将源文件复制到相应的目录。
升级程序的工作:将源文件中更新过的文件覆盖目的文件,增加的文件复制到相应的目录。
卸载程序的工作:将程序文件夹的内容删除。

针对以上内容,写一个简单的安装程序
(主要文件: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;
}

扫描关注微信公众号