Unable to extract a tar.gz file properly

I’m having problems when I try to extract a tar.gz file. Below is a complete example that downloads the latest release of ZooKeeper and extracts it in two different ways (via a copy task using tarTree and via an ant.untar task).

The problem is that the tar.gz file should contain a single root directory but some of the directories that are located inside that root directory are put adjacent to it instead of being put inside it.

Here is a build.gradle file that behaves this way for me:

defaultTasks 'getZooKeeper'
  task getZooKeeper() {
  def scratch_dir = "scratch/"
  def bundle_dir = "bundle/"
    def zkURI = uri('http://www.apache.org/dist/zookeeper/stable/')
  def zkFilename = 'zookeeper-3.4.5.tar.gz'
    ant.mkdir(dir: scratch_dir)
  ant.get(src: zkURI.resolve(zkFilename).toURL(), dest: scratch_dir, skipexisting: true, verbose: true)
  //ant.unzip(src: scratch_dir + zkFilename, dest: scratch_dir, compression: 'gzip')
      ant.untar(src: scratch_dir + zkFilename, dest: bundle_dir + "ant_extract/", compression: 'gzip')
      copy {
    from(tarTree(scratch_dir + zkFilename))
    into bundle_dir + "gradle_extract/"
  }
}