程序安装,升级及卸载核心部分(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;
}
/**
* @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;
}
}
升级程序的工作:将源文件中更新过的文件覆盖目的文件,增加的文件复制到相应的目录。
卸载程序的工作:将程序文件夹的内容删除。
针对以上内容,写一个简单的安装程序
(主要文件: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;
}
}
-= 资 源 教 程 =-
文 章 搜 索