How to use a CopySpecInternal in my own task?

I am writing my own Gradle plugin. In my task I have a CopySpecInternal. I use the follow code to visit the CopySpec. But I lost the information from the “rename”. How must I visit CopySpecInternal that I receive the target file names? The copyTo is only sample. I will add the files to a container.

        CopySpecInternal rootSpec = ...;
        rootSpec.buildRootResolver().getAllSource().visit( new FileVisitor() {
          @Override
          public void visitFile( FileVisitDetails details ) {
            // copyTo is a sample
            details.copyTo( details.getRelativePath().getFile( target ) );
          }

          @Override
          public void visitDir( FileVisitDetails arg0 ) {
            //ignore
          }
        } );

I have found a solution:

    private void processFiles( CopyActionProcessingStreamAction action, CopySpecInternal copySpec ) {
    CopyActionExecuter copyActionExecuter = new CopyActionExecuter( getInstantiator(), getFileSystem() );
    CopyAction copyAction = new CopyAction() {
        @Override
        public WorkResult execute( CopyActionProcessingStream stream ) {
            stream.process( action );
            return new SimpleWorkResult( true );
        }
    };
    copyActionExecuter.execute( copySpec, copyAction );
}

and copyTo sample look like:

    processFiles( new CopyActionProcessingStreamAction() {
        @Override
        public void processFile( FileCopyDetailsInternal details ) {
            details.copyTo( details.getRelativePath().getFile( target ) );
        }
    }, rootSpec );