[317332] ZipException if non-archive file added to Deployment Assembly page
diff --git a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/Messages.java b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/Messages.java
index 6a0f4dd..039e7e8 100644
--- a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/Messages.java
+++ b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/Messages.java
@@ -35,6 +35,7 @@
 	public static String ManifestEntryColumn;
 	public static String ManifestEntrySourceColumn;
 	public static String ConfigureParentLink;
+	public static String InvalidArchivesWarning;
 	
 	static {
 		// initialize resource bundle
diff --git a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/ExternalJarReferenceWizardFragment.java b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/ExternalJarReferenceWizardFragment.java
index a53f727..d0d9577 100644
--- a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/ExternalJarReferenceWizardFragment.java
+++ b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/ExternalJarReferenceWizardFragment.java
@@ -44,6 +44,11 @@
 
 	protected void buttonPressed() {
 		selected = chooseExternalArchiveEntries(browse.getShell());
+		
+		if(selected != null) {
+			removeInvalidArchiveFiles();
+		}
+		
 		viewer.refresh();
 		if(selected != null && selected.length > 0) {
 			isComplete = true;			
diff --git a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/JarReferenceWizardFragment.java b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/JarReferenceWizardFragment.java
index a16370f..7ce3e9c 100644
--- a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/JarReferenceWizardFragment.java
+++ b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/JarReferenceWizardFragment.java
@@ -10,9 +10,16 @@
  ******************************************************************************/
 package org.eclipse.jst.common.ui.internal.assembly.wizard;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
 
+import org.eclipse.core.resources.IContainer;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IWorkspaceRoot;
@@ -20,16 +27,21 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jdt.internal.ui.JavaPlugin;
 import org.eclipse.jdt.internal.ui.viewsupport.FilteredElementTreeSelectionDialog;
 import org.eclipse.jdt.internal.ui.wizards.TypedElementSelectionValidator;
-import org.eclipse.jdt.internal.ui.wizards.buildpaths.ArchiveFileFilter;
+import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerFilter;
 import org.eclipse.jface.window.Window;
 import org.eclipse.jst.common.ui.internal.IJstCommonUIContextIds;
+import org.eclipse.jst.common.ui.internal.JstCommonUIPlugin;
 import org.eclipse.jst.common.ui.internal.Messages;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionEvent;
@@ -116,6 +128,11 @@
 		selected = chooseEntries(
 				browse.getShell(), 
 				project.getFullPath());
+
+		if(selected != null) {
+			removeInvalidArchiveFiles();
+		}
+		
 		viewer.refresh();
 		if(selected != null && selected.length > 0) {
 			isComplete = true;
@@ -198,7 +215,7 @@
 		dialog.setValidator(validator);
 		dialog.setTitle(Messages.ArchiveDialogNewTitle);
 		dialog.setMessage(Messages.ArchiveDialogNewDescription);
-		dialog.addFilter(new ArchiveFileFilter(new ArrayList(), true, true));
+		dialog.addFilter(getDialogViewerFilter());
 		dialog.setInput(root);
 		dialog.setComparator(new ResourceComparator(ResourceComparator.NAME));
 		dialog.setInitialFilter("*.jar,*.war,*.rar,*.zip"); //$NON-NLS-1$
@@ -215,4 +232,120 @@
 		}
 		return null;
 	}
+	
+	protected void removeInvalidArchiveFiles(){
+		// Tries to open archive to verify it's valid
+		// If it is not a valid archive, a dialog is shown informing the user of the invalid archives
+		ArrayList<IPath> invalidArchiveFiles = new ArrayList<IPath>();
+		ArrayList<IPath> validArchiveFiles = new ArrayList<IPath>();
+		ZipFile zipFile = null;
+		for(IPath path:selected){
+			try {
+				String osPath = null;
+				if(path.segmentCount() > 1){
+					IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+					if(file.exists()) {
+						IPath loc = file.getLocation();
+						if(loc != null) { 
+							osPath = loc.toOSString();
+						}
+					}
+				}
+				if(osPath == null){
+					osPath = path.toOSString();
+				}
+				zipFile = new ZipFile(new File(osPath));
+				validArchiveFiles.add(path);
+			} catch (ZipException e1){
+				invalidArchiveFiles.add(path);
+			} catch (IOException e2){
+				invalidArchiveFiles.add(path);
+			}finally {
+				if (zipFile != null){
+					try {
+						zipFile.close();
+					} catch (IOException e) {
+						JstCommonUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JstCommonUIPlugin.PLUGIN_ID, e.getMessage(), e));
+					}
+				}
+			}
+		}
+		if(invalidArchiveFiles.size() > 0) {
+			selected = validArchiveFiles.toArray(new IPath[validArchiveFiles.size()]);
+			showInvalidArchiveFilesAsWarning(invalidArchiveFiles);
+		}
+	}
+	
+	private void showInvalidArchiveFilesAsWarning(ArrayList<IPath>invalidArchiveFiles) {
+		String message = Messages.InvalidArchivesWarning;
+		boolean first = true;
+		for(IPath path: invalidArchiveFiles) {
+			if(!first) {
+				message += ", \'"; //$NON-NLS-1$
+			} else {
+				message += "\'"; //$NON-NLS-1$
+				first = false;
+			}
+			message += path.lastSegment() + "\'"; //$NON-NLS-1$
+		}
+		MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Warning", message); //$NON-NLS-1$
+	}
+	
+	private static ViewerFilter getDialogViewerFilter() {
+		return new ViewerFilter() {
+			private final String[] excludedExtensions= { "txt", "css", "dll", "htm", "html", "class", "java", "classpath",      //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$ //$NON-NLS-8$
+				"compatibility", "component", "container", "cvsignore", "del", "dnx", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$
+				"dtd", "mf", "project", "properties", "psf", "rc", "runtime", "sh", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$ //$NON-NLS-8$
+				"spec", "sql", "tld", "xmi", "xml", "xsd", "gif", "jpg", "js", "vsd", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$
+				"png", "bat", "xsl", "factorypath"}; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ //$NON-NLS-4$ 
+
+			private HashMap<String,String> excludedExtensionsMap = null;
+
+			@Override
+			public boolean select(Viewer viewer, Object parent, Object element) {
+				if(excludedExtensionsMap == null) {
+					initializeExludeMap();
+				}
+				if (element instanceof IFile) {
+					IFile file = (IFile) element;
+					String ext = file.getFileExtension();
+					if(ext != null) {
+						ext = ext.toLowerCase();
+						if(excludedExtensionsMap.get(ext) != null) {
+							return false;
+						}
+					}
+					return true;
+				} else if (element instanceof IContainer) { // IProject, IFolder
+					// ignore closed projects
+					if (element instanceof IProject && !((IProject)element).isOpen())
+						return false;
+					// ignore .settings folder
+					if (element instanceof IFolder) {
+						IFolder folder = (IFolder) element;
+						if (folder.getName().equals(".settings"))
+							return false;
+					}
+					try {
+						IResource[] resources= ((IContainer)element).members();
+						for (int i= 0; i < resources.length; i++) {
+							// Only show containers that contain an archive
+							if (select(viewer, parent, resources[i])) {
+								return true;
+							}
+						}
+					} catch (CoreException e) {
+						JavaPlugin.log(e.getStatus());
+					}
+				}
+				return false;
+			}
+			
+			private void initializeExludeMap() {
+				excludedExtensionsMap = new HashMap<String, String>();
+				for(int i = 0; i < excludedExtensions.length; i++)
+				excludedExtensionsMap.put(excludedExtensions[i], excludedExtensions[i]);
+			}
+		};
+	}
 }
diff --git a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/VariableReferenceWizardFragment.java b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/VariableReferenceWizardFragment.java
index 3be7e85..dc4dea6 100644
--- a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/VariableReferenceWizardFragment.java
+++ b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/assembly/wizard/VariableReferenceWizardFragment.java
@@ -10,13 +10,20 @@
  ******************************************************************************/
 package org.eclipse.jst.common.ui.internal.assembly.wizard;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPVariableElement;
 import org.eclipse.jdt.internal.ui.wizards.buildpaths.CPVariableElementLabelProvider;
@@ -28,6 +35,7 @@
 import org.eclipse.jface.viewers.TreeViewer;
 import org.eclipse.jface.viewers.Viewer;
 import org.eclipse.jst.common.ui.internal.IJstCommonUIContextIds;
+import org.eclipse.jst.common.ui.internal.JstCommonUIPlugin;
 import org.eclipse.jst.common.ui.internal.Messages;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Image;
@@ -107,7 +115,7 @@
 			} else if( o instanceof ExtendedVariable) {
 				p = ((ExtendedVariable)o).element.getPath().append(((ExtendedVariable)o).pathAfterElement);
 			}
-			if(p == null || p.isEmpty() || p.toFile().isDirectory()) {
+			if(p == null || p.isEmpty() || p.toFile().isDirectory() || !isValidArchive(p)) {
 				isComplete = false;
 			}
 		}
@@ -251,4 +259,38 @@
 		}
 		return new Path(((CPVariableElement)selected).getName());
 	}
+	
+	private boolean isValidArchive(IPath path) {
+		boolean valid = true;
+		ZipFile zipFile = null;
+		try {
+			String osPath = null;
+			if(path.segmentCount() > 1){
+				IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+				if(file.exists()) {
+					IPath loc = file.getLocation();
+					if(loc != null) { 
+						osPath = loc.toOSString();
+					}
+				}
+			}
+			if(osPath == null){
+				osPath = path.toOSString();
+			}
+			zipFile = new ZipFile(new File(osPath));
+		} catch (ZipException e1){
+			valid = false;
+		} catch (IOException e2){
+			valid = false;
+		}finally {
+			if (zipFile != null){
+				try {
+					zipFile.close();
+				} catch (IOException e) {
+					JstCommonUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JstCommonUIPlugin.PLUGIN_ID, e.getMessage(), e));
+				}
+			}
+		}
+		return valid;
+	}
 }
diff --git a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/messages.properties b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/messages.properties
index 819fbcd..03f1a56 100644
--- a/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/messages.properties
+++ b/plugins/org.eclipse.jst.common.ui/src/org/eclipse/jst/common/ui/internal/messages.properties
@@ -18,4 +18,5 @@
 ManifestEntries=&Manifest Entries:
 ManifestEntryColumn=Manifest Entry
 ManifestEntrySourceColumn=Source
-ConfigureParentLink=Configure Available Manifest Entries
\ No newline at end of file
+ConfigureParentLink=Configure Available Manifest Entries
+InvalidArchivesWarning=The following files were not added because they are not archive files or are corrupted: 
\ No newline at end of file