gpt4 book ai didi

java - 执行复制/合并时批量删除文件

转载 作者:行者123 更新时间:2023-11-30 03:31:25 25 4
gpt4 key购买 nike

我有 2 个文件,“launcher.jar”和“camStudio.jar”,需要合并。我决定尝试使用批处理代码来完成此操作:

copy /b launcher.jar + camStudio.jar file.jar

但是,生成的“file.jar”仅包含“camStudio.jar”的内容。如何防止“launcher.jar”中的文件被删除?

最佳答案

组合两个 .jar 文件的内容比仅从命令行调用 copy 稍微复杂一些。 .jar 文件不是普通的目录,而是一种压缩文件,因此您需要特殊的实用程序来操作它们。幸运的是,这些工具附带了标准 JKD。

JDK 附带了实用程序 jar,毫不奇怪,该实用程序用于操作 .jar 文件。其用法描述如下:

Usage: jar {ctxui}[vfmn0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-n perform Pack200 normalization after creating a new archive
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .

用于组合两个 .jar 文件的相关命令是 xc。即使如此,组合 .jar 文件也需要一两行以上的时间,因此我将这个 .bat 文件放在一起以实现自动化。

:: Pass one or more .jar files as command line arguments
:: Combine_Jar [file1] [file2 ...]
:: Combine_Jar Test.jar
:: Combine_Jar Test.jar Test2.jar Test3.jar
@echo off & setlocal enabledelayedexpansion

set "jarDir=%cd%"
set "newJar="
set "folders="
pushd %temp%
for %%a in (%*) do (
call :extract %%a
set "newJar=!newJar!_%%~na_"
)



set "tempDirs=!newJar:_=^"!"
set "tempDirs=%tempDirs:^"^"=^" ^"%"

set "newJar=!newJar:~1,-1!.jar"
set "newJar=!newJar:__=_!"
if exist "!newJar!" del /Q "!newJar!"

jar cf "!newJar!" %tempDirs%

for %%a in (%*) do call rd /s /q "%%~na"

move /Y "!newJar!" "%jarDir%" > nul
popd
exit /B

:extract
set "tempDir=%~n1"

if exist "%tempDir%" (
rd /s /q "%tempDir%"
)
md "%tempDir%"

pushd "%tempDir%"
jar xf "%jarDir%\%~1"
popd
exit /B

它将所有 jar 文件作为参数传递到单个 jar 文件中。

关于java - 执行复制/合并时批量删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com