删除war里面某个class linux,只更新war包中特定的class

原创,无依赖

1.[代码] 取得class里的package

/**

* 取得class里的package

* @author microsoft

*

*/

public class ClassUtil {

public static String readPackageFromClassFile(File f) throws FileNotFoundException, IOException {

String str ;

FileInputStream in = new FileInputStream(f);

in.skip(16);

StringBuffer sb = new StringBuffer();

int i=-1;

while((i=in.read())!=-1){

if(i==7){

break;

}

sb.append((char)i);

}

in.close();

str = sb.toString()+".class";

return str;

}

}

2.[代码]只更新war包中特定的class

/**

* 只更新war包中特定的class

* 在同一个目录放下本jar app.war aaa.class bbb.classs几个文件

* 执行 java-jar updateWar

* @author microsoft

*

*/

public class UpdateWar {

public static void main(String[] args) throws Throwable{

Zip("./DataCenterService.war","./DataCenterServiceNew.war");

}

public static void Zip(String warSrc,String warTarget) throws Throwable{

File dir = new File(".");

System.out.println(dir.getAbsolutePath());

Map map = new HashMap();

for(File file:dir.listFiles()){

if(file.getName().endsWith(".class")){

map.put("WEB-INF/classes/"+ClassUtil.readPackageFromClassFile(file), file);

}

}

ZipFile zipFile = new ZipFile(warSrc);

Setset = map.keySet();

final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(warTarget));

for(Enumeration> e = zipFile.entries(); e.hasMoreElements(); ) {

ZipEntry entryIn = (ZipEntry) e.nextElement();

zos.putNextEntry(new ZipEntry(entryIn.getName()));

byte[] buf ;

if(!set.contains(entryIn.getName())){

InputStream is = zipFile.getInputStream(entryIn);

buf = IOUtils.toByteArray(zipFile.getInputStream(entryIn ));

is.close();

}else{

buf = IOUtils.toByteArray(new FileInputStream(map.get(entryIn.getName())));

}

zos.write(buf);

}

zos.close();

zipFile.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值