解压war包和重新压缩war包[通俗易懂]

解压war包和重新压缩war包[通俗易懂]publicclassWarUtils{ /** *解压war包 *@paramunWarPathwar解压的路径 *@paramwarFile解压的war包文件 */ publicstaticvoidunWar(StringunWarPath,FilewarFile){ try{ //读取文件流 Buffered

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

public class WarUtils {
	/**
	 * 解压war包
	 * @param unWarPath war解压的路径
	 * @param warFile 解压的war包文件
	 */
	public static void unWar(String unWarPath,File warFile) {
		try {
	        //读取文件流	
			BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(warFile));
			//创建解压流的文件类型
			ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.JAR, bufferedInputStream);
			JarArchiveEntry entry = null;
			while ((entry = (JarArchiveEntry) in.getNextEntry()) != null) {
				//判断目录是否存在
				if (entry.isDirectory()) {
					//创建解压文件目录
					new File(unWarPath, entry.getName()).mkdir();
				} else {
					//输出文件流
					OutputStream out = FileUtils.openOutputStream(new File(unWarPath, entry.getName()));
					IOUtils.copy(in, out);
					//关闭输出文件流
					out.close();
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			System.err.println("未找到war文件");
		} catch (ArchiveException e) {
			System.err.println("不支持的压缩格式");
		} catch (IOException e) {
			System.err.println("文件写入发生错误");
		}
	}
	
	/**
	 * 判断war包是否已经存在,若存在则删除
	 * @param filePath
	 */
	public static void delWarFile(String filePath){
		try {
			File file = new File(filePath);
			//判断文件是否存在
			if(file.exists()){
				file.delete();	
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	 /**
	  * 创建解压的war包目录,并返回目录路径
	  * @param warPath war所在路径
	  * @param unWarPath war包解压路径
	  * @return
	  */
	 public static String mkDirByFileName(String warPath, String unWarPath){
		 try {
			//创建文件
			 File warFile = new File(warPath);
			 //获取文件全部名称,包括后缀,如test.war
			 String fileFullName = warFile.getName();
			 if(fileFullName!=null){
				 //获取文件名称,如test
				 String fileName = fileFullName.substring(0, fileFullName.lastIndexOf("."));
				 //创建解压目录:解压路径+文件名称目录
				 String unWarFilePath = unWarPath+"\\"+fileName;
				 File file =new File(unWarFilePath);
				 //如果文件夹不存在则创建    
				 if(!file.exists()&&!file.isDirectory()){
					 file .mkdir();
				 }
				 //判断文件后后缀是否war
				 String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf(".")+1, fileFullName.length());
				 if(fileSuffix.toLowerCase().equals("war")){
					//解压war包
					 unWar(unWarFilePath,warFile);
					 return unWarFilePath;
				 }
			 }
			 return null;
		 } catch (Exception e) {
			 System.err.println(e.getMessage());
			 return null;
		 }
	 }
	 
	 /** 
	  * @desc 将源文件/文件夹生成指定格式的压缩文件,格式war 
	  * @param resourePath 源文件/文件夹 
	  * @param targetPath  目的压缩文件保存路径 
	  * @return void 
	  * @throws Exception  
	 */  
	public static void compressedFile(String resourcesPath,String targetPath) throws Exception{
		File resourcesFile = new File(resourcesPath);     //源文件  
		File targetFile = new File(targetPath);           //目的  
		//如果目的路径不存在,则新建  
		if(!targetFile.exists()){       
			targetFile.mkdirs();    
		}           
		String targetName = resourcesFile.getName()+".war";   //目的压缩文件名  
		//压缩文件路径
		String targetCompPath = targetPath+"\\"+targetName;
		FileOutputStream outputStream = new FileOutputStream(targetCompPath);  
		ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));  
		createCompressedFile(out, resourcesFile, "");  
		out.close();    
	}       
	/** 
	 * @desc 生成压缩文件。 
	 * 如果是文件夹,则使用递归,进行文件遍历、压缩 
	 * 如果是文件,直接压缩 
	 * @param out  输出流 
	 * @param file  目标文件 
	 * @return void 
	 * @throws Exception  	
	 */  
	public static void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{  
		//如果当前的是文件夹,则进行进一步处理  
		if(file.isDirectory()){  
			//得到文件列表信息  
			File[] files = file.listFiles();  
			//将文件夹添加到下一级打包目录  
			out.putNextEntry(new ZipEntry(dir+"/"));  
			dir = dir.length() == 0 ? "" : dir +"/";  
			//循环将文件夹中的文件打包  
			for(int i = 0 ; i < files.length ; i++){  
				createCompressedFile(out, files[i], dir + files[i].getName());         //递归处理  
			}  
		} else{   //当前的是文件,打包处理  
			//文件输入流  
			FileInputStream fis = new FileInputStream(file);  
			out.putNextEntry(new ZipEntry(dir));  
			//进行写操作  
			int j =  0;  
			byte[] buffer = new byte[1024];  
			while((j = fis.read(buffer)) > 0){  
				out.write(buffer,0,j);  
			}  
			//关闭输入流  
			fis.close();  
		}  
	}
	
	//删除当前目录以及目录下的文件
	public static boolean deleteDir(File delFile) {
		if (delFile.isDirectory()) {
			String[] children = delFile.list();
			//递归删除目录中的子目录以及文件
			for (int i=0; i<children.length; i++) {
				boolean success = deleteDir(new File(delFile, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空,可以删除
        return delFile.delete();
	}
	
	//文件重新命名
	public static void toFileRename(String filePath,String unWarPath){
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String now = format.format(new Date());
		File file = new File(filePath);
		//判断文件是否存在
		if(file.exists()){
			//获取文件全部名称,包括后缀,如test.war
			String fileFullName = file.getName();
			//获取文件名称,如test
			String fileName = fileFullName.substring(0, fileFullName.lastIndexOf("."));
			
			String fileSuffix =  fileFullName.substring(fileFullName.lastIndexOf("."),fileFullName.length());
			String renameFilePath = unWarPath +"\\"+fileName+"_bak"+now+fileSuffix;
			file.renameTo(new File(renameFilePath));
		}
	}
}

涉及到的jar包有ant-1.8.2.jar,ant-launcher-1.8.2.jar,commons-lang-2.6.jar,commons-compress-1.10.jar,commons-io-2.4.jar

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/188321.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CANoe之CAPL编程(CANoe系列其三)「建议收藏」

    CANoe之CAPL编程(CANoe系列其三)「建议收藏」CANoe之CAPL编程(CANoe系列其三)摘要1、CAPL概述1.1、CAPL语言特性1.2、CAPL的程序结构1.3、CAPL的数据类型1.4、CAPL事件类型概述2、CAPL事件类型2.1、系统事件2.2、CAN控制器事件2.3、CAN消息事件2.4、时间事件2.5、键盘事件2.6、错误帧事件2.7、环境变量事件2.8、系统变量事件摘要    &…

    2025年7月28日
    2
  • javastream流详解_Java获取文件流的所有方式

    javastream流详解_Java获取文件流的所有方式一、Stream流引入Lambda表达式,基于Lambda所带来的函数式编程,又引入了一个全新的Stream概念,用于解决集合类库既有的鼻端。(Lambda表达式详解在上篇博客内容)现有一个需求:将list集合中姓张的元素过滤到一个新的集合中然后将过滤出来的姓张的元素中,再过滤出来长度为3的元素,存储到一个新的集合中1.用常规方法解决需求/…

    2022年10月6日
    2
  • cardboard应用_cardboard怎么用

    cardboard应用_cardboard怎么用GoogleCardboard虚拟现实眼镜开发初步(一)虚拟现实技术简介不得不说这几年虚拟现实技术逐渐火热,伴随着虚拟现实设备的价格迅速平民化,越来越多的虚拟现实设备来到了我们眼前,也因此虚拟现实方面的开发离我们也越来越近。这几年迅速崛起的Oculus,其成功就在于拉近了虚拟现实与群众的距离,把原本价格高不可攀的虚拟现实设备放到了我们可以触手可及的位置,Oculus的技术开辟了

    2022年9月2日
    4
  • 读书笔记-HBase in Action-第二部分Advanced concepts-(2)Coprocessor

    读书笔记-HBase in Action-第二部分Advanced concepts-(2)Coprocessor

    2022年1月30日
    59
  • hrbust1224「建议收藏」

    hrbust1224「建议收藏」1.图形输出2.每一个图形的输出都有自己的规律3.不一定要自己一个个printf加for的一行行输出这样会有错4.好比是hrbust1224;这里写代码片include

    2022年5月4日
    45
  • python和java哪个更值得学 知乎_学完python再学java

    python和java哪个更值得学 知乎_学完python再学java​在编程界经常会引发一个讨论,就是python和Java哪个更值得学,Java语言具有跨平台的特性,在应用范围上有许多选择的余地,而Python在这几年的火热程度丝毫没有减退。个人观点,看学习的目的

    2025年6月19日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号