分解因数 递归_递归分解WAR文件
分解因數 遞歸
抽象
是否曾經需要分解WAR文件以及分解WAR文件中的所有JAR文件? 是的,我也是!
我寫了ferris-war-exploder來爆炸:
基本上,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()方法。 我有2個示例:爆炸一個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)。 Oracle 從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
分解因數 遞歸
總結
以上是生活随笔為你收集整理的分解因数 递归_递归分解WAR文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ai里字体怎么剪切蒙版(ai怎么建立剪切
- 下一篇: junit4和junit5_JUnit5