1 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.jface.dialogs.MessageDialog; 19 import org.eclipse.swt.widgets.Shell; 20 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 21 22 25 public class ArchiveFileManipulations { 26 27 private static ZipLeveledStructureProvider zipProviderCache; 28 29 private static TarLeveledStructureProvider tarProviderCache; 30 31 39 public static boolean isTarFile(String fileName) { 40 if (fileName.length() == 0) { 41 return false; 42 } 43 44 try { 45 new TarFile(fileName); 46 } catch (TarException tarException) { 47 return false; 48 } catch (IOException ioException) { 49 return false; 50 } 51 52 return true; 53 } 54 55 63 public static boolean isZipFile(String fileName) { 64 if (fileName.length() == 0) { 65 return false; 66 } 67 68 try { 69 new ZipFile (fileName); 70 } catch (IOException ioException) { 71 return false; 72 } 73 74 return true; 75 } 76 77 83 public static void clearProviderCache(Shell shell) { 84 if (zipProviderCache != null) { 85 closeZipFile(zipProviderCache.getZipFile(), shell); 86 zipProviderCache = null; 87 } 88 tarProviderCache = null; 89 } 90 91 96 public static ZipLeveledStructureProvider getZipStructureProvider( 97 ZipFile targetZip, Shell shell) { 98 if (zipProviderCache == null) { 99 zipProviderCache = new ZipLeveledStructureProvider(targetZip); 100 } else if (!zipProviderCache.getZipFile().getName().equals( 101 targetZip.getName())) { 102 clearProviderCache(shell); 103 zipProviderCache = new ZipLeveledStructureProvider(targetZip); 105 } else if (!zipProviderCache.getZipFile().equals(targetZip)) { 106 zipProviderCache = new ZipLeveledStructureProvider(targetZip); 109 } 110 111 return zipProviderCache; 112 } 113 114 124 public static boolean closeZipFile(ZipFile file, Shell shell) { 125 try { 126 file.close(); 127 } catch (IOException e) { 128 displayErrorDialog(DataTransferMessages.ZipImport_couldNotClose, 129 shell); 130 return false; 131 } 132 133 return true; 134 } 135 136 145 public static TarLeveledStructureProvider getTarStructureProvider( 146 TarFile targetTar, Shell shell) { 147 if (tarProviderCache == null) { 148 tarProviderCache = new TarLeveledStructureProvider(targetTar); 149 } else if (!tarProviderCache.getTarFile().getName().equals( 150 targetTar.getName())) { 151 ArchiveFileManipulations.clearProviderCache(shell); 152 tarProviderCache = new TarLeveledStructureProvider(targetTar); 154 } 155 156 return tarProviderCache; 157 } 158 159 165 protected static void displayErrorDialog(String message, Shell shell) { 166 MessageDialog.openError(shell, getErrorDialogTitle(), message); 167 } 168 169 172 protected static String getErrorDialogTitle() { 173 return IDEWorkbenchMessages.WizardExportPage_internalErrorTitle; 174 } 175 } 176 | Popular Tags |