--- AbstractArchiver.java	2008-09-05 14:47:20.000000000 -0500
+++ AbstractArchiver.java.2e-take3	2008-09-05 14:46:29.000000000 -0500
@@ -17,18 +17,6 @@
  *  limitations under the License.
  */
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.UndeclaredThrowableException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-
 import org.codehaus.plexus.PlexusConstants;
 import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
@@ -53,6 +41,20 @@
 import org.codehaus.plexus.util.DirectoryScanner;
 import org.codehaus.plexus.util.IOUtil;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.UndeclaredThrowableException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.TreeSet;
+import java.util.Set;
+
 
 /**
  * @version $Id: AbstractArchiver.java 7143 2008-01-06 21:50:04Z jochen $
@@ -76,16 +78,9 @@
     private File destFile;
 
     /**
-     * A list of the following objects:
-     * <ul>
-     *   <li>Instances of {@link ArchiveEntry}, which are passed back by
-     *     {@link #getResources()} without modifications.</li>
-     *   <li>Instances of {@link PlexusIoResourceCollection}, which
-     *     are converted into an {@link Iterator} over instances of
-     *     {@link ArchiveEntry} by {@link #getResources()}.
-     * </ul>
+     * A map of ArchiveEntries with .getName as the key.
      */
-    private List resources = new ArrayList();
+    private Map resources = new HashMap();
 
     private int defaultFileMode = DEFAULT_FILE_MODE;
 
@@ -208,9 +203,9 @@
                 if ( isSelected( fileSelectors, res ) )
                 {
                     String targetDir = PrefixFileMapper.getMappedFileName( prefix, sourceDir );
-
-                    resources.add( ArchiveEntry.createDirectoryEntry( targetDir, res,
-                                                                      getDefaultDirectoryMode() ) );
+                    ArchiveEntry entry = ArchiveEntry.createDirectoryEntry( targetDir, res,
+                                                                      getDefaultDirectoryMode() );
+                    resources.put( entry.getName(), entry );
                 }
             }
         }
@@ -293,13 +288,13 @@
             throw new ArchiverException( e.getMessage(), e );
         }
     }
-    
+
     public void addResource( PlexusIoResource resource, String destFileName, int permissions )
         throws ArchiverException
     {
-        resources.add( asArchiveEntry( resource, destFileName, permissions ) );
+        resources.put( destFileName, asArchiveEntry( resource, destFileName, permissions ) );
     }
-    
+
     public void addFile( File inputFile, String destFileName, int permissions )
         throws ArchiverException
     {
@@ -321,12 +316,12 @@
 
                 if ( include( fileStream, destFileName ) )
                 {
-                    resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
+                    resources.put( destFileName, ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
                 }
             }
             else
             {
-                resources.add( ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
+                resources.put( destFileName, ArchiveEntry.createFileEntry( destFileName, inputFile, permissions ) );
             }
         }
         catch ( IOException e )
@@ -348,75 +343,24 @@
     {
         return new ResourceIterator()
         {
-            private final Iterator archiveEntryIter = resources.iterator();
-            private boolean currentArchiveEntryValid;
-            private PlexusIoResourceCollection plexusIoResourceCollection;
-            private Iterator plexusIoResourceIter;
-            private ArchiveEntry archiveEntry;
+            //Sort the keys so that parents get created before children
+            //otherwise the permissions do not get created properly.
+            private final Set keys = new TreeSet(resources.keySet());
+            private final Iterator archiveEntryIter = keys.iterator();
 
-            public boolean hasNext() throws ArchiverException
+            public boolean hasNext()
             {
-                if (!currentArchiveEntryValid)
-                {
-                    if ( plexusIoResourceIter == null )
-                    {
-                        if ( archiveEntryIter.hasNext() )
-                        {
-                            Object o = archiveEntryIter.next();
-                            if ( o instanceof ArchiveEntry )
-                            {
-                                archiveEntry = (ArchiveEntry) o;
-                            }
-                            else if ( o instanceof PlexusIoResourceCollection )
-                            {
-                                plexusIoResourceCollection = (PlexusIoResourceCollection) o;
-                                try
-                                {
-                                    plexusIoResourceIter = plexusIoResourceCollection.getResources();
-                                }
-                                catch ( IOException e )
-                                {
-                                    throw new ArchiverException( e.getMessage(), e );
-                                }
-                                return hasNext();
-                            }
-                            else
-                            {
-                                throw new IllegalStateException( "Invalid object type: "
-                                                                 + o.getClass().getName() );
-                            }
-                        }
-                        else
-                        {
-                            archiveEntry = null;
-                        }
-                    }
-                    else
-                    {
-                        if ( plexusIoResourceIter.hasNext() )
-                        {
-                            PlexusIoResource resource = (PlexusIoResource) plexusIoResourceIter.next();
-                            archiveEntry = asArchiveEntry( plexusIoResourceCollection, resource );
-                        }
-                        else
-                        {
-                            plexusIoResourceIter = null;
-                            return hasNext();
-                        }
-                    }
-                    currentArchiveEntryValid = true;
-                }
-                return archiveEntry != null;
+               return archiveEntryIter.hasNext();
             }
 
-            public ArchiveEntry next() throws ArchiverException
+            public ArchiveEntry next()
             {
                 if ( !hasNext() )
                 {
                     throw new NoSuchElementException();
                 }
-                currentArchiveEntryValid = false;
-                return archiveEntry;
+                ArchiveEntry entry = (ArchiveEntry) resources.get( archiveEntryIter.next() );
+                return entry;
             }
         };
     }
@@ -539,9 +483,21 @@
     public void addResources( PlexusIoResourceCollection collection )
         throws ArchiverException
     {
-        resources.add( collection );
+        try
+        {
+            Iterator plexusIoResourceIter = collection.getResources();
+            while ( plexusIoResourceIter.hasNext() ) {
+                PlexusIoResource ioResource = (PlexusIoResource) plexusIoResourceIter.next();
+                ArchiveEntry entry = asArchiveEntry( collection, ioResource );
+                resources.put( entry.getName(), entry );
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new ArchiverException( e.getMessage(), e );
+        }
     }
-    
+
     public void addArchivedFileSet( final ArchivedFileSet fileSet )
         throws ArchiverException
     {
@@ -656,7 +612,7 @@
             return false; // File doesn't yet exist
         }
 
-        Iterator it = resources.iterator();
+        Iterator it = resources.values().iterator();
         if ( !it.hasNext() )
         {
             getLogger().debug( "isUp2date: false (No input files.)" );

