在执行复制/合并时批量删除文件 [英] Batch Deleting Files While Performing a Copy/Merge

查看:286
本文介绍了在执行复制/合并时批量删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件,launcher.jar和camStudio.jar,我需要合并。我决定尝试使用批处理与代码:

  copy / b launcher.jar + camStudio.jar file.jar但是,生成的file.jar只包含camStudio.jar的内容。如何防止launcher.jar中的文件被删除?

解决方案

c> .jar 文件比从命令行调用 copy 稍微复杂一点。而不是正常的目录, .jar 文件是一种压缩文件,因此您需要特殊的实用程序来操作它们。幸运的是这些工具都带有标准的JKD。



JDK附带了 jar 实用程序,用于操作 .jar 文件。它的用法如下:

 用法:jar {ctxui} [vfmn0Me] [jar-file] [manifest- [entry-point] [-C dir] files ... 
选项:
-c创建新存档
-t存档的内容列表
-x extract named或所有)文件
-u更新现有归档
-v在标准输出上生成详细输出
-f指定归档文件名
-m包括指定清单文件的清单信息
-n在创建新存档后执行Pack200规范化
-e指定独立应用程序的应用程序入口点
捆绑到可执行jar文件
-0仅存储;使用无ZIP压缩
-M不为条目创建清单文件
-i为指定的jar文件生成索引信息
-C更改到指定的目录并包括以下文件
如果任何文件是一个目录,那么它会被递归处理。
清单文件名,归档文件名和入口点名称是以与'm','f'和'e'标志相同的顺序指定的


示例1:将两个类文件归档到一个名为classes.jar的归档文件中:
jar cvf classes.jar Foo.class Bar.class
示例2:使用现有清单文件'mymanifest'并将foo /目录中的所有
文件归档到'classes.jar'中:
jar cvfm classes.jar mymanifest -C foo /。

用于组合两个 .jar 文件的相关命令是 x c 。即使这样,组合 .jar 文件需要超过一两行,所以我把这个 .bat 文件自动化。

  ::将一个或多个.jar文件作为命令行参数传递
:: Combine_Jar [ file1] [file2 ...]
:: Combine_Jar Test.jar
:: Combine_Jar Test.jar Test2.jar Test3.jar
@echo off& setlocal enabledelayedexpansion

设置jarDir =%cd%
设置newJar =
设置folders =
pushd%temp%
% %a在(%*)do(
调用:extract %% a
设置newJar =!newJar!_ %%〜na_


b
$ b settempDirs =!newJar:_ = ^!
settempDirs =%tempDirs:^^= ^^%

set newJar =!newJar:〜1,-1!.jar
setnewJar =!newJar:__ = _!
如果存在!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

如果存在%tempDir%(
rd / s / q%tempDir%

md%tempDir %

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

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


I have 2 files, "launcher.jar" and "camStudio.jar" that I need merged. I decided to try to do this using batch with the code:

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

However, the resulting "file.jar" only contains the contents of "camStudio.jar". How can I prevent the files in "launcher.jar" from being deleted?

解决方案

Combining the contents of two .jar files is a little more complicated than just calling copy from the command line. Rather than a normal directory, .jar files are a type of compressed file, so you need special utilities to manipulate them. Fortunately these tools come with the standard JKD.

The JDK comes with the utility jar that is unsurprisingly used for manipulating .jar files. It's usage is described as this:

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/ .

Relevant commands for combining two .jar files are x and c. Even with this, combining the .jar files takes more than a line or two, so I put together this .bat files to automate it.

:: 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

It will all jar files passed as arguments into a single jar files.

这篇关于在执行复制/合并时批量删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆