001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial implementation
010: * Red Hat, Inc - Extracted methods from WizardArchiveFileResourceImportPage1
011: *******************************************************************************/package org.eclipse.ui.internal.wizards.datatransfer;
012:
013: import java.io.IOException;
014: import java.util.zip.ZipFile;
015:
016: import org.eclipse.jface.dialogs.MessageDialog;
017: import org.eclipse.swt.widgets.Shell;
018: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
019:
020: /**
021: * @since 3.1
022: */
023: public class ArchiveFileManipulations {
024:
025: private static ZipLeveledStructureProvider zipProviderCache;
026:
027: private static TarLeveledStructureProvider tarProviderCache;
028:
029: /**
030: * Determine whether the file with the given filename is in .tar.gz or .tar
031: * format.
032: *
033: * @param fileName
034: * file to test
035: * @return true if the file is in tar format
036: */
037: public static boolean isTarFile(String fileName) {
038: if (fileName.length() == 0) {
039: return false;
040: }
041:
042: try {
043: new TarFile(fileName);
044: } catch (TarException tarException) {
045: return false;
046: } catch (IOException ioException) {
047: return false;
048: }
049:
050: return true;
051: }
052:
053: /**
054: * Determine whether the file with the given filename is in .zip or .jar
055: * format.
056: *
057: * @param fileName
058: * file to test
059: * @return true if the file is in tar format
060: */
061: public static boolean isZipFile(String fileName) {
062: if (fileName.length() == 0) {
063: return false;
064: }
065:
066: try {
067: new ZipFile(fileName);
068: } catch (IOException ioException) {
069: return false;
070: }
071:
072: return true;
073: }
074:
075: /**
076: * Clears the cached structure provider after first finalizing it properly.
077: *
078: * @param shell
079: * The shell to display any possible Dialogs in
080: */
081: public static void clearProviderCache(Shell shell) {
082: if (zipProviderCache != null) {
083: closeZipFile(zipProviderCache.getZipFile(), shell);
084: zipProviderCache = null;
085: }
086: tarProviderCache = null;
087: }
088:
089: /**
090: * @param targetZip
091: * @param shell
092: * @return the structure provider
093: */
094: public static ZipLeveledStructureProvider getZipStructureProvider(
095: ZipFile targetZip, Shell shell) {
096: if (zipProviderCache == null) {
097: zipProviderCache = new ZipLeveledStructureProvider(
098: targetZip);
099: } else if (!zipProviderCache.getZipFile().getName().equals(
100: targetZip.getName())) {
101: clearProviderCache(shell);
102: // ie.- new value, so finalize&remove old value
103: zipProviderCache = new ZipLeveledStructureProvider(
104: targetZip);
105: } else if (!zipProviderCache.getZipFile().equals(targetZip)) {
106: // duplicate handle to same zip
107: // dispose old zip and use new one in case old one is closed
108: zipProviderCache = new ZipLeveledStructureProvider(
109: targetZip);
110: }
111:
112: return zipProviderCache;
113: }
114:
115: /**
116: * Attempts to close the passed zip file, and answers a boolean indicating
117: * success.
118: *
119: * @param file
120: * The zip file to attempt to close
121: * @param shell
122: * The shell to display error dialogs in
123: * @return Returns true if the operation was successful
124: */
125: public static boolean closeZipFile(ZipFile file, Shell shell) {
126: try {
127: file.close();
128: } catch (IOException e) {
129: displayErrorDialog(
130: DataTransferMessages.ZipImport_couldNotClose, shell);
131: return false;
132: }
133:
134: return true;
135: }
136:
137: /**
138: * Returns a structure provider for the specified tar file.
139: *
140: * @param targetTar
141: * The specified tar file
142: * @param shell
143: * The shell to display dialogs in
144: * @return the structure provider
145: */
146: public static TarLeveledStructureProvider getTarStructureProvider(
147: TarFile targetTar, Shell shell) {
148: if (tarProviderCache == null) {
149: tarProviderCache = new TarLeveledStructureProvider(
150: targetTar);
151: } else if (!tarProviderCache.getTarFile().getName().equals(
152: targetTar.getName())) {
153: ArchiveFileManipulations.clearProviderCache(shell);
154: // ie.- new value, so finalize&remove old value
155: tarProviderCache = new TarLeveledStructureProvider(
156: targetTar);
157: }
158:
159: return tarProviderCache;
160: }
161:
162: /**
163: * Display an error dialog with the specified message.
164: *
165: * @param message
166: * the error message
167: */
168: protected static void displayErrorDialog(String message, Shell shell) {
169: MessageDialog.openError(shell, getErrorDialogTitle(), message);
170: }
171:
172: /**
173: * Get the title for an error dialog. Subclasses should override.
174: */
175: protected static String getErrorDialogTitle() {
176: return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle;
177: }
178: }
|