删除jar包中的指定文件

如何删除jar包中的指定文件呢?

当然使用解压缩软件(rar,zip,7z)肯定没问题.但是我想自动化,图形界面的工具就无能为力了.

核心方法:

Java代码   收藏代码
  1. /*** 
  2.      * 删除jar包中的内容 
  3.      * @param jarPath 
  4.      * @param fileName : "META-INF/BCKEY.DSA" 
  5.      * @throws IOException 
  6.      * @throws ArchiveException 
  7.      */  
  8.     public static void deleteFileInJar(String jarPath,String fileName) throws IOException, ArchiveException{  
  9.         List<String>fileNames=new ArrayList<String>();  
  10.         if(!ValueWidget.isNullOrEmpty(fileName)){  
  11.         fileNames.add(fileName);}  
  12.         deleteFileInJar(jarPath, fileNames);  
  13.     }  
  14.     /** 
  15.      * 删除jar包中的内容 
  16.      * @param jarPath 
  17.      * @param fileNames : ["META-INF/BCKEY.DSA"],注意斜杠 
  18.      * @throws IOException 
  19.      * @throws ArchiveException 
  20.      */  
  21.     public static void deleteFileInJar(String jarPath,List<String>fileNames) throws IOException, ArchiveException{  
  22.         List<ZipFileBean> zipFiles = CompressZipUtil  
  23.                 .deCompressRecursionFileList(jarPath, ""true);  
  24.         List<ZipApkFile> zipApkFiles = extendZipFileBean(zipFiles,fileNames);  
  25.         CompressZipUtil.setPrint(false);  
  26.         File newFile=new File(jarPath + ".bak");  
  27.         while(newFile.exists()){  
  28.             //若bak文件存在,则循环修改名称,只到文件不存在  
  29.             System.out.println("file exist:"+newFile.getAbsolutePath());  
  30.             newFile=new File(jarPath + RandomUtils.getTimeRandom2());  
  31.         }  
  32.         CompressZipUtil.persistenceZip(newFile, zipApkFiles);  
  33.         File jarFile=new File(jarPath);  
  34.           
  35.         System.out.println("delete old jar:"+jarFile.getAbsolutePath());  
  36.         boolean isSuccess=jarFile.delete();  
  37.         if(!isSuccess){  
  38.             System.out.println("删除失败:"+jarFile.getAbsolutePath());  
  39.         }else{  
  40.             System.out.println("modify name");  
  41.             newFile.renameTo(jarFile);  
  42.         }  
  43.     }  

 使用说明:

比如我想删除jar(zip)包中的config\manual.properties

zip包结构:

 

main方法如下:

 

Java代码   收藏代码
  1. public static void main(String[] args) throws IOException, ArchiveException {  
  2.         String jarPath="D:\\bin\\config\\config.zip";  
  3.   
  4.         deleteFileInJar(jarPath, "config/manual.properties"/*"META-INF/BCKEY.DSA"*/);  
  5.         System.out.println("jarPath:"+jarPath);  
  6.   
  7.           
  8.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值