网上很多关于jar包处理的都不全面,而却很多根本无法使用,这是我经过试验得出的有效方法。同时,关于jar包无法file.delete()删除jar包,查找了很多文章都没具体实现,此文也实现了,jar包解压,再压缩流程,覆盖旧jar包,也算删除了,另外提供一种方式删除jar包,方法如下
解压jar包
/**
* 解压jar包
* @param destDir 解压位置
* @param jarFile 需要解压的jar
*/
public static void extractJar(String destDir,String jarFile) {
// String jarFile = "E:/test/com.ide.core_1.0.0.jar";
if((jarFile == null) || (jarFile.equals("")) || !jarFile.endsWith(".jar")) {
return;
}
try {
JarFile jar = new JarFile(jarFile);
Enumeration<?> enumeration = jar.entries();
while(enumeration.hasMoreElements()) {
JarEntry jarEntry = (JarEntry) enumeration.nextElement();
String[] names = jarEntry.getName().split("/");
for(int i = 0; i < names.length; i++) {
String path = destDir;
for(int j = 0; j < (i + 1); j++) {
path += File.separator + names[j];
}
File file = new File(path);
if(!file.exists()) {
if((i == (names.length - 1)) && !jarEntry.getName().endsWith("/")) {
file.createNewFile();
} else {
file.mkdirs();
continue;
}
} else {
continue;
}
InputStream is = jar.getInputStream(jarEntry);
FileOutputStream fos = new FileOutputStream(file);
while(is.available() > 0) {
fos.write(is.read());
}
fos.flush();
fos.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
重新打包jar包
提供两种情况compressJar方法,直接指定需要压缩的文件夹,以及压缩路径;zipDirectory方法输入要解压文件的父文件,先遍历父文件下所有文件夹,再对文件夹压缩。
private static List<String> list =new ArrayList();
/**
* 压缩jar包
* @param srcPath 需要压缩的文件
* @param targetPath 压缩的jar包,存放路劲
*/
@SuppressWarnings("resource")
public static void compressJar(String srcPath,String targetPath) {
FileOutputStream fileOutputStream=null;
CheckedOutputStream cs =null;
ZipOutputStream out=null;
InputStream in=null;
try {
File file = new File(srcPath);
//判断是否是目录
if (file.isDirectory()) {
if (list.size() > 0)
list.clear();
byte b[] = new byte[128];
// 压缩文件的保存路径
String zipFile = targetPath + File.separator + file.getName() + ".jar";
// 压缩文件目录
// String filepath = file.getAbsolutePath() + File.separator;
List<String> fileList = allFile(srcPath+File.separator);
fileOutputStream = new FileOutputStream(zipFile);
// 使用输出流检查
cs = new CheckedOutputStream(fileOutputStream, new CRC32());
// 声明输出zip流
out = new ZipOutputStream(new BufferedOutputStream(
cs));
for (int i = 0; i < fileList.size(); i++) {
in = new FileInputStream((String) fileList.get(i));
String fileName = ((String) (fileList.get(i))).replace(File.separatorChar, '/');
System.out.println("ziping " + fileName);
String tmp = file.getName() + "/";
fileName = fileName.substring(fileName.lastIndexOf(tmp) + file.getName().length() + 1);
ZipEntry e = new ZipEntry(fileName);
out.putNextEntry(e);
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
}
out.closeEntry();
}
System.out.println("done!");
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (in!=null) {
in.close();
}if (out!=null) {
out.close();
}if (out!=null) {
out.close();
}if (cs!=null) {
cs.close();
}if (fileOutputStream!=null) {
fileOutputStream.close();
}
System.gc();//一定加上这个,否则源文件删除不了
} catch (Exception e2) {
// TODO: handle exception
}
}
}
/**
* jar压缩
* @param parentDirPath 要压缩文件夹的父文件夹
* @param targetPath 目标文件夹
*/
public static void zipDirectory(String parentDirPath,String targetPath)
{
try {
File dirFile= new File(parentDirPath);
File[] listArr = dirFile.listFiles();
for (File childFile : listArr) {
if(childFile.isDirectory())
{
if(list.size()>0)
list.clear();
byte b[] = new byte[128];
// 压缩文件的保存路径
String zipFile =targetPath+File.separator+childFile.getName()+".jar";
// 压缩文件目录
String filepath =childFile.getAbsolutePath()+File.separator;
List fileList = allFile(filepath);
FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
// 使用输出流检查
CheckedOutputStream cs = new CheckedOutputStream(fileOutputStream, new CRC32());
// 声明输出zip流
ZipOutputStream out = new ZipOutputStream( new BufferedOutputStream(
cs));
for ( int i = 0; i < fileList.size(); i++) {
InputStream in = new FileInputStream((String)fileList.get(i));
String fileName = ((String)(fileList.get(i))).replace(File.separatorChar,'/');
String tmp= childFile.getName()+"/";
fileName = fileName.substring(fileName.lastIndexOf(tmp)+childFile.getName().length()+1);
ZipEntry e = new ZipEntry(fileName);
out.putNextEntry(e);
int len = 0;
while((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.closeEntry();
}
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static List allFile(String path)
{
File file = new File(path);
String[] array = null;
String sTemp = "";
if(file.isDirectory())
{
} else{
return null;
}
array= file.list();
if(array.length>0)
{
for( int i=0;i<array.length;i++)
{
sTemp = path+array[i];
file = new File(sTemp);
if(file.isDirectory())
{
allFile(sTemp+"/");
} else{
list.add(sTemp);
}
}
} else{
return null;
}
return list;
}
测试方法
public static void main(String[] args) throws Exception {
String jarPath="E:\\test\\com.ide.platform_1.0.0\\library.jar";
String jarPath1="E:\\test\\com.ide.core_1.0.0.jar";
String jarPath2="E:\\test\\com.ide.project.core_1.0.0.jar";
String destDir= jarPath.substring(0,jarPath.lastIndexOf(".jar"));
String destDir1= jarPath1.substring(0,jarPath1.lastIndexOf(".jar"));
String destDir2= jarPath2.substring(0,jarPath2.lastIndexOf(".jar"));
extractJar(destDir,jarPath);
extractJar(destDir1,jarPath1);
extractJar(destDir2,jarPath2);
//重新压缩
compressJar(destDir,jarPath.substring(0,jarPath.lastIndexOf("\\")));
compressJar(destDir1,jarPath1.substring(0,jarPath1.lastIndexOf("\\")));
compressJar(destDir2,jarPath1.substring(0,jarPath2.lastIndexOf("\\")));
}
删除jar包
jarsPath:需要删除的jar包;如:E:\test\com.ide.project.core_1.0.0.jar
Runtime.getRuntime().exec("cmd /c ping localhost -n 6 > nul && del "+jarsPath);
遍历删除压缩文件
public static void main(String[] args) throws Exception {
File file = new File("E:\\test\\com.ide.core_1.0.0");
deleteFile(file );
}
public static void deleteFile(File file){
if(file == null || !file.exists()){
return;
}
//获得文件对象的子文件对象列表
File[] files = file.listFiles();
//遍历这些子文件
for (File f : files){
//判断这些子文件是否是目录
if(f.isDirectory()){
//是文件夹的话继续递归 如果是发现文件直接删除
deleteFile(f);
}else {
f.delete();
}
}
//删除空文件夹
file.delete();
}