【发布时间】:2011-10-04 00:37:40
【问题描述】:
我想使用 ANT 将几个 zip 文件组合在一起,但我有三个限制会导致标准技术失败:
- 有些文件(具有已知文件名)我不想包含在最终存档中。
- 一些源存档包含名称相同但大小写不同的文件。
- 运行脚本的机器使用不区分大小写的文件系统。
为了使我的问题具体化,这里有一个示例源存档。我确实不知道a.txt和A.txt代表的文件名,但我知道知道文件名b.txt。
$ touch a.txt ; zip src.zip a.txt ; rm a.txt
$ touch A.txt ; zip src.zip A.txt ; rm A.txt
$ touch b.txt ; zip src.zip b.txt ; rm b.txt
$ unzip -l src.zip
Archive: src.zip
Length Date Time Name
-------- ---- ---- ----
0 09-23-11 11:35 a.txt
0 09-23-11 11:35 A.txt
0 09-23-11 11:36 b.txt
-------- -------
0 3 files
这就是我想要的:(原始存档中的所有内容,除了 b.txt)
$ ant
$ unzip -l expected.zip
Archive: expected.zip
Length Date Time Name
-------- ---- ---- ----
0 09-23-11 11:35 a.txt
0 09-23-11 11:35 A.txt
-------- -------
0 2 files
我发现在互联网上推荐的两种技术是:
<target name="unzip-then-rezip">
<!-- Either a.txt or A.txt is lost during unzip and
does not appear in out.zip -->
<delete dir="tmp"/>
<delete file="out.zip"/>
<mkdir dir="tmp"/>
<unzip src="src.zip" dest="tmp"/>
<zip destfile="out.zip" basedir="tmp" excludes="b.txt"/>
</target>
<target name="direct-zip">
<!-- Have not found a way to exclude b.txt from out.zip -->
<delete file="out.zip"/>
<zip destfile="out.zip">
<zipgroupfileset dir="." includes="*.zip" />
</zip>
</target>
使用unzip-then-rezip,我会丢失a.txt 或A.txt,因为底层文件系统不区分大小写并且不能存储这两个文件。使用direct-zip 似乎是正确的方法,但我还没有找到过滤掉我不想包含的文件的方法。
我将求助于创建自己的 ANT 任务来完成这项工作,但我更愿意使用标准的 ANT 任务(甚至是 ant-contrib),即使会降低性能或可读性。
【问题讨论】: