package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class updater {
protected static Logger log = LoggerFactory.getLogger(updater.class);
//需要被监视的源路径
public static String resourcepath="C:\\srczip\\";
//运行程序路径
public static String apppath="C:\\dmp\\";
//备份路径
public static String backuppath ="C:\\backup\\";
public static void main(String[] args)
{
if(args.length==0)
{
System.out.println("Pactera Updater Help Content v1.0 by yh");
System.out.println("");
System.out.println("*************************************");
System.out.println("start pactera service 3 args");
System.out.println("args:resourcepath apppath backuppath ");
System.out.println("example:c:\\\\dmp\\ c:\\\\tomcat\\webapp\\dmp\\ c:\\\\tomcat\\webapp\\dmp\\backup\\");
System.out.println("*************************************");
System.out.println("start pactera service 4 args");
System.out.println("args:resourcepath apppath backuppath rebackpath");
System.out.println("example:c:\\\\dmp\\ c:\\\\tomcat\\webapp\\dmp\\ c:\\\\tomcat\\webapp\\dmp\\backup\\ c:\\\\tomcat\\webapp\\dmp\\reback\\" );
System.out.println("*************************************");
System.out.println("zip file 2 args");
System.out.println("args:inputFileName outputFileName");
System.out.println("example:c:\\\\a\\ c:\\\\a.zip");
System.out.println("*************************************");
try
{
ThreadReadPath thread = new ThreadReadPath();
Thread t1 = new Thread(thread);
t1.start();
}
catch(Exception exp)
{
System.out.println(exp.getMessage());
}
}
if(args.length==1)
{
//帮助文档
if(args[0].indexOf('?')>0)
{
System.out.print("Pactera Updater Help Content v1.0 by yh");
System.out.print("*************************************");
System.out.print("start pactera service 3 args");
System.out.print("args:resourcepath apppath backuppath ");
System.out.print("example:c:\\\\dmp\\ c:\\\\tomcat\\\\webapp\\\\dmp\\ c:\\\\tomcat\\\\webapp\\\\dmp\\\\backup\\");
System.out.print("*************************************");
System.out.print("start pactera service 4 args");
System.out.print("args:resourcepath apppath backuppath rebackpath");
System.out.print("example:c:\\\\dmp\\ c:\\\\tomcat\\\\webapp\\\\dmp\\ c:\\\\tomcat\\\\webapp\\\\dmp\\\\backup\\ c:\\\\tomcat\\\\webapp\\\\dmp\\\\reback\\" );
System.out.print("*************************************");
System.out.print("zip file 2 args");
System.out.print("args:inputFileName outputFileName");
System.out.print("example:c:\\\\a\\ c:\\\\a.zip");
}
}
if(args.length==2)
{
//打包程序(压缩文件)
try
{
zip(args[0],args[1]);
}
catch(Exception exp)
{
System.out.print("打包异常,错误提示:"+exp.getMessage());
}
}
if(args.length==3)
{
resourcepath=args[0];
//运行程序路径
apppath=args[1];
//备份路径
backuppath =args[2];
try
{
System.out.println("*************************************");
System.out.println("* Pactera Updater Service Started *");
System.out.println("*************************************");
System.out.println("resourcepath:"+resourcepath);
System.out.println("apppath:"+apppath);
System.out.println("backuppath:"+apppath);
ThreadReadPath thread = new ThreadReadPath();
Thread t1 = new Thread(thread);
t1.start();
}
catch(Exception exp)
{
System.out.println(exp.getMessage());
}
}
}
/**
* 压缩文件
* @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\
* @param outputFileName 输出zip文件的路径,例如:c:\\a.zip
*/
public static void zip(String inputFileName, String outputFileName) throws Exception {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
zip(out, new File(inputFileName), "");
log.debug("压缩完成!");
out.closeEntry();
out.close();
}
/**
* 压缩文件
* @param out org.apache.tools.zip.ZipOutputStream
* @param file 待压缩的文件
* @param base 压缩的根目录
*/
private static void zip(ZipOutputStream out, File file, String base) throws Exception {
if (file.isDirectory()) {
File[] fl = file.listFiles();
base = base.length() == 0 ? "" : base + File.separator;
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new ZipEntry(base));
log.debug("添加压缩文件:" + base);
FileInputStream in = new FileInputStream(file);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* 解压zip文件
* @param zipFileName 待解压的zip文件路径,例如:c:\\a.zip
* @param outputDirectory 解压目标文件夹,例如:c:\\a\
*/
public static void unZip(String zipFileName, String outputDirectory) throws Exception {
ZipFile zipFile = new ZipFile(zipFileName);
try {
Enumeration<?> e = zipFile.getEntries();
ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
log.debug("解压:" + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
log.debug("创建目录:" + outputDirectory + File.separator + name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0, fileName.lastIndexOf("/")));
fileName = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
}
File f = new File(outputDirectory + File.separator + zipEntry.getName());
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
FileOutputStream out = new FileOutputStream(f);
byte[] by = new byte[1024];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
in.close();
out.close();
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close();
log.debug("解压完成!");
}
}
private static void createDirectory(String d
java 自动程序更新
需积分: 50 190 浏览量
2016-02-23
10:59:05
上传
评论 3
收藏 3.03MB RAR 举报
杨航AI
- 粉丝: 634
- 资源: 642
最新资源
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
- 基于同态加密技术的匿名电子投票系统源码.zip
- Pyqt5项目框架-PyQt项目开发实践
- 基于C通过MQTT的智能农业大棚管理系统(本科毕业设计)
- python+CNN的网络入侵检测算法源码.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈