递归分解WAR文件

介绍了一种名为ferris-war-exploder的工具,该工具能够递归地解压WAR、JAR、EAR和ZIP格式的文件。文章详细解释了如何使用此工具,并提供了代码示例。

抽象

是否曾经需要分解WAR文件以及分解WAR文件中的所有JAR文件? 是的,我也是!

我写了Ferris-war-exploder来爆炸:

  1. 一个JAR文件
  2. 一个WAR文件,它找到的每个JAR文件也会爆炸。
  3. 包含每个JAR文件(请参阅#1)和WAR文件(请参阅#2)的EAR文件也爆炸了。

基本上,ferris-war-exploder会爆炸任何ZIP文件格式的东西。 ZIP格式的所有条目也会被分解。 这是递归发生的,因此任何可以爆炸的东西都会爆炸。

免责声明

这篇文章仅供参考。 在使用所提供的任何信息之前,请认真思考。 从中学到东西,但最终自己做出决定,风险自负。

要求

我使用以下主要技术完成了本文的所有工作。 您可能可以使用不同的技术或版本来做相同的事情,但不能保证。

  • NetBeans 11.2
  • Maven 3.3.9(与NetBeans捆绑在一起)
  • Java 11(zulu11.35.15-ca-jdk11.0.5-win_x64)

下载

访问我的GitHub页面https://github.com/mjremijan以查看我所有的开源项目。 这篇文章的代码位于: https : //github.com/mjremijan/ferris-war-exploder

让我们开始吧

ferris-war-exploder会爆炸任何ZIP文件格式的文件。 ZIP格式的所有条目也会被分解。 这是递归发生的,因此任何可以爆炸的东西都会爆炸。

您需要告诉它爆炸的档案(WAR,JAR,EAR,ZIP)。

您需要告诉它在哪里爆炸存档。

注意一旦WAR爆炸,请参阅我的ferris-magic-number来分析所有.class文件。

清单1显示了启动应用程序的main()方法。 我有两个示例:爆炸一个JAR和爆炸一个WAR。

清单1 –

 public class Main { 
   public static void main(String[] args) throws Exception 
   { 
     System.out.printf( "=== Welcome to Ferris WAR Exploder ===%n" ); 
     new Unzip( "./src/test/jars/commons-lang3-3.7.jar" , "./target/unzipped/jar" ) 
       .unzip(); 
     new Unzip( "./src/test/wars/sample.war" , "./target/unzipped/war" ) 
       .unzip(); 
     System.out.printf( "%n=== DONE ===%n" ); 
   }  } 

清单2显示了Unzip类。 此类包含有趣的代码以递归方式分解档案。 清单2中的所有内容都不难理解,因此请您自己阅读。

清单2 –

 package org.ferris.war.exploder;  import java.io.File;  import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  import java.util.zip.ZipEntry;  import java.util.zip.ZipFile;  import java.util.zip.ZipInputStream;  /** 
  * 
  * @author Michael Remijan mjremijan@yahoo.com @mjremijan 
  */  public class Unzip { 
   protected File zipFile; 
   protected File destinationDirectory; 
   public Unzip(String zipFilePath, String destinationDirectoryPath) { 
     setZipFile(zipFilePath); 
     setDestinationDirectory(destinationDirectoryPath); 
   } 
   public Unzip(File zipFile) { 
     this .zipFile = zipFile; 
     setDestinationDirectory(zipFile.getParent()); 
   } 
   protected void setDestinationDirectory(String destinationDirectoryPath) { 
     destinationDirectory = new File(destinationDirectoryPath, zipFile.getName()); 
     if (destinationDirectory.exists() && destinationDirectory.isDirectory()) { 
       throw new RuntimeException( 
         String.format( 
           "The destination directory \"%s\" already exists." , 
            destinationDirectory.getPath() 
         ) 
       ); 
     } 
     if (destinationDirectory.exists() && destinationDirectory.isFile()) { 
       destinationDirectory = new File(destinationDirectoryPath, zipFile.getName() + ".d" ); 
     } 
     mkdirs(destinationDirectory, 
        "Failed to create the destination directory \"%s\"." 
     ); 
   } 
   protected void setZipFile(String zipFilePath) { 
     zipFile = new File(zipFilePath); 
     if (!zipFile.exists()) { 
       throw new RuntimeException( 
         String.format( 
           "The file \"%s\" does not exist" , zipFile.getPath() 
         ) 
       ); 
     } 
     if (!zipFile.canRead()) { 
       throw new RuntimeException( 
         String.format( 
           "The file \"%s\" is not readable" , zipFile.getPath() 
         ) 
       ); 
     } 
   } 
   protected void unzip() throws Exception { 
     System.out.printf( "%n=== Unipping %s ===%n%n" , zipFile.getPath()); 
     try (ZipInputStream zip 
       = new ZipInputStream( new FileInputStream(zipFile)); 
     ){ 
       for (ZipEntry z = zip.getNextEntry(); z != null ; z = zip.getNextEntry()) { 
         if (z.isDirectory()) { 
           mkdirs( new File(destinationDirectory, z.getName()), 
             "Failed to create a zip entry directory \"%s\"" 
           ); 
         } else { 
           File zfile = new File(destinationDirectory, z.getName()); 
           mkdirs(zfile.getParentFile(), 
              "Failed to create parent directory for zip entry file \"%s\"." 
           ); 
           File unzippedFile = unzipEntry(z, zip); 
           if (isZip(unzippedFile)) { 
             new Unzip(unzippedFile).unzip(); 
           } 
         } 
       } 
     } 
   } 
   protected boolean isZip(File file) { 
     boolean b = false ; 
     try { 
       b = new ZipFile(file).getName().length() > 0 ; 
     } catch (IOException ignore) {} 
     return b; 
   } 
   protected File unzipEntry(ZipEntry z, ZipInputStream zip) throws Exception { 
     File zfile = new File(destinationDirectory, z.getName()); 
     System.out.printf( " %s%n" , zfile.getAbsolutePath()); 
     try ( FileOutputStream out = new FileOutputStream(zfile)) { 
       zip.transferTo(out); 
     } 
     zip.closeEntry();; 
     return zfile; 
   } 
   protected void mkdirs(File dir, String errorMessageFormat) { 
     if (dir.exists() && dir.isDirectory()) { 
       return ; 
     } 
     dir.mkdirs(); 
     if (!dir.exists()) { 
       throw new RuntimeException( 
         String.format(errorMessageFormat, dir.getPath() 
         ) 
       ); 
     } 
   }  } 

摘要

ferris-war-exploder项目并不太复杂,但是当您需要完全爆炸WAR或EAR归档文件时,它非常方便。 请享用!

参考文献

ZipOutputStream。 (nd)。 甲骨文 从https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/zip/ZipOutputStream.html检索。

翻译自: https://www.javacodegeeks.com/2020/03/explode-a-war-file-recursively.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值