1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
16 import java
.io
.IOException
;
17 import java
.util
.zip
.ZipException
;
18 import java
.util
.zip
.ZipFile
;
20 import org
.eclipse
.swt
.widgets
.Shell
;
21 import org
.eclipse
.tracecompass
.tmf
.core
.util
.Pair
;
22 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.ArchiveFileManipulations
;
23 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.ZipLeveledStructureProvider
;
24 import org
.eclipse
.ui
.wizards
.datatransfer
.FileSystemStructureProvider
;
27 * Various utilities for dealing with archives in the context of importing
30 @SuppressWarnings({"restriction" })
31 public class ArchiveUtil
{
34 * Returns whether or not the source file is an archive file (Zip, tar,
39 * @return whether or not the source file is an archive file
41 public static boolean isArchiveFile(File sourceFile
) {
42 String absolutePath
= sourceFile
.getAbsolutePath();
43 return isTarFile(absolutePath
) || ArchiveFileManipulations
.isZipFile(absolutePath
) || isGzipFile(absolutePath
);
46 private static boolean isTarFile(String fileName
) {
47 TarFile specifiedTarSourceFile
= getSpecifiedTarSourceFile(fileName
);
48 if (specifiedTarSourceFile
!= null) {
50 specifiedTarSourceFile
.close();
52 } catch (IOException e
) {
59 private static boolean isGzipFile(String fileName
) {
60 if (!fileName
.isEmpty()) {
61 try (GzipFile specifiedTarSourceFile
= new GzipFile(fileName
);) {
63 } catch (IOException e
) {
69 private static ZipFile
getSpecifiedZipSourceFile(String fileName
) {
70 if (fileName
.length() == 0) {
75 return new ZipFile(fileName
);
76 } catch (ZipException e
) {
78 } catch (IOException e
) {
85 private static TarFile
getSpecifiedTarSourceFile(String fileName
) {
86 if (fileName
.length() == 0) {
90 // FIXME: Work around Bug 463633. Remove this block once we move to Eclipse 4.5.
91 if (new File(fileName
).length() < 512) {
96 return new TarFile(fileName
);
97 } catch (TarException
| IOException e
) {
104 @SuppressWarnings("resource")
105 static boolean ensureZipSourceIsValid(String archivePath
, Shell shell
) {
106 ZipFile specifiedFile
= getSpecifiedZipSourceFile(archivePath
);
107 if (specifiedFile
== null) {
110 return ArchiveFileManipulations
.closeZipFile(specifiedFile
, shell
);
113 static boolean ensureTarSourceIsValid(String archivePath
) {
114 TarFile specifiedFile
= getSpecifiedTarSourceFile(archivePath
);
115 if (specifiedFile
== null) {
118 return closeTarFile(specifiedFile
);
121 static boolean ensureGzipSourceIsValid(String archivePath
) {
122 return isGzipFile(archivePath
);
125 static boolean closeTarFile(TarFile file
) {
128 } catch (IOException e
) {
136 * Get the root file system object and it's associated import provider for
137 * the specified source file. A shell is used to display messages in case of
143 * the parent shell to use to display error messages
144 * @return the root file system object and it's associated import provider
146 @SuppressWarnings("resource")
147 public static Pair
<IFileSystemObject
, FileSystemObjectImportStructureProvider
> getRootObjectAndProvider(File sourceFile
, Shell shell
) {
148 if (sourceFile
== null) {
152 IFileSystemObject rootElement
= null;
153 FileSystemObjectImportStructureProvider importStructureProvider
= null;
155 // Import from directory
156 if (!isArchiveFile(sourceFile
)) {
157 importStructureProvider
= new FileSystemObjectImportStructureProvider(FileSystemStructureProvider
.INSTANCE
, null);
158 rootElement
= importStructureProvider
.getIFileSystemObject(sourceFile
);
160 // Import from archive
161 FileSystemObjectLeveledImportStructureProvider leveledImportStructureProvider
= null;
162 String archivePath
= sourceFile
.getAbsolutePath();
163 if (isTarFile(archivePath
)) {
164 if (ensureTarSourceIsValid(archivePath
)) {
165 // We close the file when we dispose the import provider,
166 // see disposeSelectionGroupRoot
167 TarFile tarFile
= getSpecifiedTarSourceFile(archivePath
);
168 leveledImportStructureProvider
= new FileSystemObjectLeveledImportStructureProvider(new TarLeveledStructureProvider(tarFile
), archivePath
);
170 } else if (ensureZipSourceIsValid(archivePath
, shell
)) {
171 // We close the file when we dispose the import provider, see
172 // disposeSelectionGroupRoot
173 ZipFile zipFile
= getSpecifiedZipSourceFile(archivePath
);
174 leveledImportStructureProvider
= new FileSystemObjectLeveledImportStructureProvider(new ZipLeveledStructureProvider(zipFile
), archivePath
);
175 } else if (ensureGzipSourceIsValid(archivePath
)) {
176 // We close the file when we dispose the import provider, see
177 // disposeSelectionGroupRoot
178 GzipFile zipFile
= null;
180 zipFile
= new GzipFile(archivePath
);
181 leveledImportStructureProvider
= new FileSystemObjectLeveledImportStructureProvider(new GzipLeveledStructureProvider(zipFile
), archivePath
);
182 } catch (IOException e
) {
186 if (leveledImportStructureProvider
== null) {
189 rootElement
= leveledImportStructureProvider
.getRoot();
190 importStructureProvider
= leveledImportStructureProvider
;
193 if (rootElement
== null) {
197 return new Pair
<>(rootElement
, importStructureProvider
);