原创,无依赖
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();
}
}