- 解压jar包
- 重新打包jar包
- 测试方法
- 删除jar包
- 遍历删除压缩文件
网上很多关于jar包处理的都不全面,而却很多根本无法使用,这是我经过试验得出的有效方法。同时,关于jar包无法file.delete()删除jar包,查找了很多文章都没具体实现,此文也实现了,jar包解压,再压缩流程,覆盖旧jar包,也算删除了,另外提供一种方式删除jar包,方法如下
解压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 Listlist =new ArrayList(); public static void compressJar(String srcPath,String targetPath) { 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 fileList = allFile(srcPath+File.separator); 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 = 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(); } out.close(); } } catch (Exception e) { e.printStackTrace(); } } 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 测试方法 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:testcom.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(); }



