Ant Zip Extracted Parent Directory

拈花ヽ惹草 提交于 2019-12-21 20:38:00

问题


I have several zip files that I need to unzip within an Ant target. All the zip files are in the same directory, and have the same internal directory and file structure.

So I am using the following snippet to unzip all the zip files in the directory, but each zip file does not contain a parent folder at the root, so each successive zip file is unzipped and overwrites the previous files.

<unzip dest="C:/Program Files/Samsung/Samsung TV Apps SDK/Apps">            
    <fileset dir=".">
        <include name="**/*.zip"/>
    </fileset>
</unzip>

Is there a better way to unzip a group of files, and create a directory to unzip them to that is based on the zip file name?

So, if the zip files are:

1.zip
2.zip
3.zip

then the content of each will be extracted to:

1/
2/
3/

Thanks


回答1:


One solution might be to use the ant-contrib 'for' and 'propertyregex' tasks to do this:

<for param="my.zip">
  <fileset dir="." includes="**/*.zip" />
  <sequential>
    <propertyregex property="my.zip.dir"
              input="@{my.zip}"
              regexp="(.*)\..*"
              select="\1"
              override="yes" />
    <unzip src="@{my.zip}" dest="${my.zip.dir}" />
  </sequential>
</for>

The 'propertyregex' strips the .zip extension from the zip file name to use as the target directory name.




回答2:


Without ant-contrib: https://stackoverflow.com/a/12169523/957081

<!-- Get the path of the war file. I know the file name pattern in this case -->
<path id="warFilePath">
    <fileset dir="./tomcat/webapps/">
        <include name="myApp-*.war"/>
    </fileset>
</path>

<property name="warFile" refid="warFilePath" />

<!-- Get file name without extension -->
<basename property="warFilename" file="${warFile}" suffix=".war" />

<!-- Create directory with the same name as the war file name -->
<mkdir dir="./tomcat/webapps/${warFilename}" />

<!-- unzip war file -->
<unwar dest="./tomcat/webapps/${warFilename}">
    <fileset dir="./tomcat/webapps/">
        <include name="${warFilename}.war"/>    
    </fileset>
</unwar>


来源:https://stackoverflow.com/questions/4430276/ant-zip-extracted-parent-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!