title: How can I use the Gradle Copy task to rename a directory on the path of a file I am copying?
body: <h2>Background</h2>
  <p>I have a Gradle Copy task like this:</p>
  <pre><code>task copyFromZip(type: Copy) {
      from zipTree(&quot;Example.zip&quot;)
      into &quot;build/output&quot;
  }
  </code></pre>
  <p>Within the Zip file is something like this:</p>
  <pre><code>+--- photos
  |    +--- StackOverflow
  |         \--- foo.png
  |    \--- Super User
  |         \--- bar.png
  +--- text
  |    \--- Stack Exchange
  |        +--- StackOverflow
  |             \--- foo.txt
  |        \--- Super User
  |             \--- bar.txt
  </code></pre>
  <p>When I run the Copy task, the directories are preserved. For instance, I have <code>build/output/photos/StackOverflow/foo.png</code>.</p>
  <h2>Goal</h2>
  <p>What I want to do is rename the directories as I copy. For instance, say I realized that &quot;StackOverflow&quot; should have spaces in it. I want the file <code>photos/StackOverflow/foo.png</code> to be copied to <code>build/output/photos/Stack Overflow/foo.png</code>, and the same to be applied to the files in other folders with the name &quot;StackOverflow&quot; instead of &quot;Stack Overflow&quot;.</p>
  <h2>What I've tried</h2>
  <p>My first thought was to add a <code>rename{}</code> block.</p>
  <pre><code>task copyFromZip(type: Copy) {
      from zipTree(&quot;Example.zip&quot;)
      into &quot;build/output&quot;
      rename { String filePath -&gt;
          filePath.replace(&quot;StackOverflow&quot;, &quot;Stack Overflow&quot;)
      }
  }
  </code></pre>
  <p>However, I discovered that <code>rename{}</code> only gives the name of the file, not the path.</p>
  <p>I also tried manipulating the <code>relativePath</code> using <code>eachFile{}</code></p>
  <pre><code>task copyFromZip(type: Copy) {
      from zipTree(&quot;Example.zip&quot;)
      into &quot;build/output&quot;
      eachFile{ file -&gt;
          file.relativePath = file.relativePath.replace(&quot;StackOverflow&quot;, &quot;Stack Overflow&quot;)
      }
  }
  </code></pre>
  <p>However, that results in a <code>Could not expand ZIP</code> error.</p>
  <p>How can I use the Gradle Copy task to rename a directory on the path of a file I am copying?</p>