开发者

Ant Zip Extracted Parent Directory

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开发者_JAVA技巧 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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜