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