1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial implementation
13  *     Red Hat, Inc - Extracted methods from WizardArchiveFileResourceImportPage1
14  *******************************************************************************/
15 
16 package org.eclipse.ui.internal.wizards.datatransfer;
17 
18 import java.io.IOException;
19 import java.util.zip.ZipFile;
20 
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
26 
27 /**
28  * @since 3.1
29  */
30 public class ArchiveFileManipulations {
31 
32 
33 	/**
34 	 * Determine whether the file with the given filename is in .tar.gz or .tar
35 	 * format.
36 	 *
37 	 * @param fileName
38 	 *            file to test
39 	 * @return true if the file is in tar format
40 	 */
isTarFile(String fileName)41 	public static boolean isTarFile(String fileName) {
42 		if (fileName.length() == 0) {
43 			return false;
44 		}
45 
46 		TarFile tarFile = null;
47 		try {
48 			tarFile = new TarFile(fileName);
49 		} catch (TarException | IOException ioException) {
50 			return false;
51 		} finally {
52 			if (tarFile != null) {
53 				try {
54 					tarFile.close();
55 				} catch (IOException e) {
56 					// ignore
57 				}
58 			}
59 		}
60 
61 		return true;
62 	}
63 
64 	/**
65 	 * Determine whether the file with the given filename is in .zip or .jar
66 	 * format.
67 	 *
68 	 * @param fileName
69 	 *            file to test
70 	 * @return true if the file is in tar format
71 	 */
isZipFile(String fileName)72 	public static boolean isZipFile(String fileName) {
73 		if (fileName.length() == 0) {
74 			return false;
75 		}
76 
77 		ZipFile zipFile = null;
78 		try {
79 			zipFile = new ZipFile(fileName);
80 		} catch (IOException ioException) {
81 			return false;
82 		} finally {
83 			if (zipFile != null) {
84 				try {
85 					zipFile.close();
86 				} catch (IOException e) {
87 					// ignore
88 				}
89 			}
90 		}
91 
92 		return true;
93 	}
94 
95 	/**
96 	 * Closes the given structure provider.
97 	 *
98 	 * @param structureProvider
99 	 *            The structure provider to be closed, can be <code>null</code>
100 	 * @param shell
101 	 *            The shell to display any possible Dialogs in
102 	 */
closeStructureProvider(ILeveledImportStructureProvider structureProvider, Shell shell)103 	public static void closeStructureProvider(ILeveledImportStructureProvider structureProvider, Shell shell) {
104 		if (structureProvider instanceof ZipLeveledStructureProvider) {
105 			closeZipFile(((ZipLeveledStructureProvider) structureProvider).getZipFile(), shell);
106 		}
107 		if (structureProvider instanceof TarLeveledStructureProvider) {
108 			closeTarFile(((TarLeveledStructureProvider) structureProvider).getTarFile(), shell);
109 		}
110 	}
111 
112 	/**
113 	 * Attempts to close the passed zip file, and answers a boolean indicating
114 	 * success.
115 	 *
116 	 * @param file
117 	 *            The zip file to attempt to close
118 	 * @param shell
119 	 *            The shell to display error dialogs in
120 	 * @return Returns true if the operation was successful
121 	 */
closeZipFile(ZipFile file, Shell shell)122 	public static boolean closeZipFile(ZipFile file, Shell shell) {
123 		try {
124 			file.close();
125 		} catch (IOException e) {
126 			displayErrorDialog(
127 					NLS.bind(DataTransferMessages.ZipImport_couldNotClose, file.getName()),
128 					shell);
129 			return false;
130 		}
131 
132 		return true;
133 	}
134 
135 	/**
136 	 * Attempts to close the passed tar file, and answers a boolean indicating
137 	 * success.
138 	 *
139 	 * @param file
140 	 *            The tar file to attempt to close
141 	 * @param shell
142 	 *            The shell to display error dialogs in
143 	 * @return Returns true if the operation was successful
144 	 * @since 3.4
145 	 */
closeTarFile(TarFile file, Shell shell)146 	public static boolean closeTarFile(TarFile file, Shell shell) {
147 		try {
148 			file.close();
149 		} catch (IOException e) {
150 			displayErrorDialog(
151 					NLS.bind(DataTransferMessages.ZipImport_couldNotClose, file.getName()),
152 					shell);
153 			return false;
154 		}
155 
156 		return true;
157 	}
158 
159 	/**
160 	 * Display an error dialog with the specified message.
161 	 *
162 	 * @param message
163 	 *            the error message
164 	 */
displayErrorDialog(String message, Shell shell)165 	protected static void displayErrorDialog(String message, Shell shell) {
166 		MessageDialog.open(MessageDialog.ERROR, shell, getErrorDialogTitle(), message, SWT.SHEET);
167 	}
168 
169 	/**
170 	 * Get the title for an error dialog. Subclasses should override.
171 	 */
getErrorDialogTitle()172 	protected static String getErrorDialogTitle() {
173 		return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle;
174 	}
175 }
176