0

I'm trying to write a replacement of the integrated <zip> task so that it supports passwords using <exec> and 7za.exe. The idea is to have a drop-in replacement of the <zip> task.

The difficulty is, that <zip> support so many ways to declare which files to include/exclude such as:

  • includes
  • includesfile
  • excludes
  • excludesfile
  • defaultexcludes
  • and nested <fileset> declarations

Is there a way to use the result of these fileset instructions inside the exec task?

kenorb
  • 155,785
  • 88
  • 678
  • 743
ViToni
  • 882
  • 9
  • 12

2 Answers2

0

The undocumented property ${toString:filesetid} has all files, default separator is ';'.
To convert the separator use pathconvert task, the resulting property will contain the files with choosen separator, f.e. :

<fileset dir="C:/diff1" includes="**/*.html" id="diff">
 <different targetdir="C:/diff2"
   ignoreFileTimes="true"/>
</fileset>

<!-- one file one line -->
<pathconvert refid="diff" pathsep="${line.separator}" property="htmldiff"/>

<!-- blank as separator -->
<pathconvert refid="diff" pathsep=" " property="htmldiff"/>  

<echo file="C:/diff1/htmldiff.txt">${htmldiff}</echo>

for use in exec with arg line, blank as separator seems appropiate.

Rebse
  • 10,307
  • 2
  • 38
  • 66
0

Based on the answer I was able to come up with a solution which works similar to the integrated <zip> task. Using <pathconvert> and single quotes does the trick:

<macrodef name="sevenzip" description="Command line interface for 7zip">
    <attribute name="level"  default="5"/> <!-- will be ignored for now, just to make it compatible with normal ZIP task -->
    <attribute name="basedir"/>
    <attribute name="excludes" default=""/>
    <attribute name="includes" default="**/**"/>
    <attribute name="destfile"/>
    <attribute name="password" default=""/>

    <sequential>
        <description>7-Zip integration</description>
        <local name="passArg" />
        <if>
            <equals arg1="@{password}" arg2=""/>
            <then>
                <property name="passArg" value="" />
            </then>
            <else>
                <!-- p<SECRET> = set password of archive -->
                <property name="passArg" value='"-p@{password}"' />
            </else>
        </if>

        <fileset id="mask" dir="@{basedir}">
            <include name="@{includes}" />
            <exclude name="@{excludes}" />
        </fileset>

        <local name="mask" />
        <pathconvert property="mask" refid="mask" pathsep="' '" />
        <!-- the single quotes help to wrap file names containing spaces -->

        <exec executable="${7zip.cmd}" failonerror="true">
            <!-- command -->
            <arg value="a"/>                <!-- a : add files to archive -->
            <!-- arg value="u"/ -->             <!-- u : update files to archive -->

            <!-- switches -->
            <arg value="-bd"/>              <!-- -bd : disable percentage indicator -->
            <arg value="-tzip"/>            <!-- -t<type> : set type of archive -->             

            <arg value="${passArg}"/>       

            <arg value="--"/>               <!-- : Stop switches parsing -->

            <!-- archive file -->
            <arg value="@{destfile}"/>

            <!-- file names / wildcards -->
            <arg line="'${mask}'"/>
            <!-- 
                the single quotes are the outter wrapper for the single quotes
                from pathconvert, the line attribute add the result to the arguments
                but NOT as a single escaped string
            -->

        </exec>
    </sequential>
</macrodef>
ViToni
  • 882
  • 9
  • 12