/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.commons.compress.changes;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

/**
 * Performs ChangeSet operations on a stream.
 * This class is thread safe and can be used multiple times.
 * It operates on a copy of the ChangeSet. If the ChangeSet changes,
 * a new Performer must be created.
 * 
 * @ThreadSafe
 * @Immutable
 */
public class ChangeSetPerformer {
    private final Set changes;
    
    /**
     * Constructs a ChangeSetPerformer with the changes from this ChangeSet
     * @param changeSet the ChangeSet which operations are used for performing
     */
    public ChangeSetPerformer(final ChangeSet changeSet) {
        changes = changeSet.getChanges();
    }
    
    /**
     * Performs all changes collected in this ChangeSet on the input stream and
     * streams the result to the output stream. Perform may be called more than once.
     * 
     * @param in
     *            the InputStream to perform the changes on
     * @param out
     *            the resulting OutputStream with all modifications
     * @throws IOException
     *             if an read/write error occurs
     */
    public void perform(ArchiveInputStream in, ArchiveOutputStream out)
            throws IOException {
        Set workingSet = new LinkedHashSet(changes);
        
        for (Iterator it = workingSet.iterator(); it.hasNext();) {
            Change change = (Change) it.next();

            if (change.type() == Change.TYPE_ADD) {
                copyStream(change.getInput(), out, change.getEntry());
                it.remove();
            }
        }

        ArchiveEntry entry = null;
        while ((entry = in.getNextEntry()) != null) {
            boolean copy = true;

            for (Iterator it = workingSet.iterator(); it.hasNext();) {
                Change change = (Change) it.next();

                final int type = change.type();
                final String name = entry.getName();
                if (type == Change.TYPE_DELETE && name != null) {
                    if (name.equals(change.targetFile())) {
                        copy = false;
                        it.remove();
                        break;
                    }
                } else if(type == Change.TYPE_DELETE_DIR && name != null) {
                    if (name.matches(
                            change.targetFile() + "/.*")) {
                        copy = false;
                        break;
                    }
                }
            }

            if (copy) {
                if (!isDeletedLater(workingSet, entry)) {
                    copyStream(in, out, entry);
                }
            }
        }
    }

    /**
     * Checks if an ArchiveEntry is deleted later in the ChangeSet. This is
     * necessary if an file is added with this ChangeSet, but later became
     * deleted in the same set.
     * 
     * @param entry
     *            the entry to check
     * @return true, if this entry has an deletion change later, false otherwise
     */
    private boolean isDeletedLater(Set workingSet, ArchiveEntry entry) {
        String source = entry.getName();

        if (!workingSet.isEmpty()) {
            for (Iterator it = workingSet.iterator(); it.hasNext();) {
                Change change = (Change) it.next();
                final int type = change.type();
                if (type == Change.TYPE_DELETE || type == Change.TYPE_DELETE_DIR) {
                    String target = change.targetFile();

                    if (source.equals(target)) {
                        return true;
                    }

                    return (type == Change.TYPE_DELETE_DIR) && source.matches(target + "/.*");
                }
            }
        }
        return false;
    }

    /**
     * Copies the ArchiveEntry to the Output stream
     * 
     * @param in
     *            the stream to read the data from
     * @param out
     *            the stream to write the data to
     * @param entry
     *            the entry to write
     * @throws IOException
     *             if data cannot be read or written
     */
    private void copyStream(InputStream in, ArchiveOutputStream out,
            ArchiveEntry entry) throws IOException {
        out.putArchiveEntry(entry);
        IOUtils.copy(in, out);
        out.closeArchiveEntry();
    }
}

@@ -3430,45 +3430,19 @@
 ame.
-matches(%0A                            
+startsWith(
 chan
@@ -3461,18 +3461,16 @@
 e() + %22/
-.*
 %22)) %7B%0A  
@@ -4442,96 +4442,8 @@
 ();%0A
-                if (type == Change.TYPE_DELETE %7C%7C type == Change.TYPE_DELETE_DIR) %7B%0A    
     
@@ -4487,25 +4487,24 @@
 rgetFile();%0A
-%0A
             
@@ -4507,24 +4507,50 @@
         
-    
 if (
+type == Change.TYPE_DELETE && 
 source.e
@@ -4562,28 +4562,24 @@
 (target)) %7B%0A
-    
             
@@ -4607,36 +4607,32 @@
                 
-    
 %7D%0A%0A             
@@ -4634,26 +4634,18 @@
         
-    return
+if
  (type =
@@ -4668,17 +4668,16 @@
 LETE_DIR
-)
  && sour
@@ -4683,15 +4683,18 @@
 rce.
-matches
+startsWith
 (tar
@@ -4705,12 +4705,44 @@
 + %22/
-.*
 %22)
+)%7B%0A                    return true
 ;%0A  

Fix bug: deleteDir should not delete file with matching name Don't exit loop if name does not match Use startsWith() rather than match() as it's simpler and quicker