tmf: Use Apache Common Compress for importing from archive
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / project / wizards / importtrace / TarLeveledStructureProvider.java
index 685657865291bffc05283d4f5f6498492636ab38..4ecdad54f03c730f82d5b01de8b6eb743fd30770 100644 (file)
@@ -11,6 +11,7 @@
  *     IImportStructureProvider to ILeveledImportStructureProvider
  *     Mickael Istria (Red Hat Inc.) - Bug 486901
  *     Marc-Andre Laperle <marc-andre.laperle@ericsson.com> - Copied to Trace Compass to work around bug 501379
+ *     Marc-Andre Laperle <marc-andre.laperle@ericsson.com> - Adapted to use Apache Common Compress
  *******************************************************************************/
 package org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace;
 
@@ -22,6 +23,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
 import org.eclipse.core.resources.ResourceAttributes;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
@@ -39,11 +41,11 @@ public class TarLeveledStructureProvider implements
         ILeveledImportStructureProvider {
     private TarFile tarFile;
 
-    private TarEntry root = new TarEntry("/");//$NON-NLS-1$
+    private TarArchiveEntry root = new TarArchiveEntry("/", true);//$NON-NLS-1$
 
-    private Map<TarEntry, List<TarEntry>> children;
+    private Map<TarArchiveEntry, List<TarArchiveEntry>> children;
 
-    private Map<IPath, TarEntry> directoryEntryCache = new HashMap<>();
+    private Map<IPath, TarArchiveEntry> directoryEntryCache = new HashMap<>();
 
     private int stripLevel;
 
@@ -57,35 +59,36 @@ public class TarLeveledStructureProvider implements
     public TarLeveledStructureProvider(TarFile sourceFile) {
         super();
         tarFile = sourceFile;
-        root.setFileType(TarEntry.DIRECTORY);
     }
 
     /**
      * Creates a new container tar entry with the specified name, iff it has
      * not already been created. If the parent of the given element does not
      * already exist it will be recursively created as well.
-     * @param pathname The path representing the container
+     * @param pathName The path representing the container
      * @return The element represented by this pathname (it may have already existed)
      */
-    protected TarEntry createContainer(IPath pathname) {
-        TarEntry existingEntry = directoryEntryCache.get(pathname);
+    protected TarArchiveEntry createContainer(IPath pathName) {
+        IPath newPathName = pathName;
+        TarArchiveEntry existingEntry = directoryEntryCache.get(newPathName);
         if (existingEntry != null) {
             return existingEntry;
         }
 
-        TarEntry parent;
-        if (pathname.segmentCount() == 1) {
+        TarArchiveEntry parent;
+        if (newPathName.segmentCount() == 1) {
             parent = root;
         } else {
-            parent = createContainer(pathname.removeLastSegments(1));
+            parent = createContainer(newPathName.removeLastSegments(1));
         }
-        TarEntry newEntry = new TarEntry(pathname.toString());
-        newEntry.setFileType(TarEntry.DIRECTORY);
-        directoryEntryCache.put(pathname, newEntry);
-        List<TarEntry> childList = new ArrayList<>();
+        // Add trailing / so that the entry knows it's a folder
+        newPathName = newPathName.addTrailingSeparator();
+        TarArchiveEntry newEntry = new TarArchiveEntry(newPathName.toString());
+        directoryEntryCache.put(newPathName, newEntry);
+        List<TarArchiveEntry> childList = new ArrayList<>();
         children.put(newEntry, childList);
 
-        List<TarEntry> parentChildList = children.get(parent);
+        List<TarArchiveEntry> parentChildList = children.get(parent);
         NonNullUtils.checkNotNull(parentChildList).add(newEntry);
         return newEntry;
     }
@@ -94,9 +97,9 @@ public class TarLeveledStructureProvider implements
      * Creates a new tar file entry with the specified name.
      * @param entry the entry to create the file for
      */
-    protected void createFile(TarEntry entry) {
+    protected void createFile(TarArchiveEntry entry) {
         IPath pathname = new Path(entry.getName());
-        TarEntry parent;
+        TarArchiveEntry parent;
         if (pathname.segmentCount() == 1) {
             parent = root;
         } else {
@@ -104,7 +107,7 @@ public class TarLeveledStructureProvider implements
                     .removeLastSegments(1));
         }
 
-        List<TarEntry> childList = children.get(parent);
+        List<TarArchiveEntry> childList = children.get(parent);
         NonNullUtils.checkNotNull(childList).add(entry);
     }
 
@@ -120,10 +123,7 @@ public class TarLeveledStructureProvider implements
     @Override
     public InputStream getContents(Object element) {
         try {
-            return tarFile.getInputStream((TarEntry) element);
-        } catch (TarException e) {
-            IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
-            return null;
+            return tarFile.getInputStream((TarArchiveEntry) element);
         } catch (IOException e) {
             IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
             return null;
@@ -138,7 +138,7 @@ public class TarLeveledStructureProvider implements
      */
     public ResourceAttributes getResourceAttributes(Object element) {
         ResourceAttributes attributes = new ResourceAttributes();
-        TarEntry entry = (TarEntry) element;
+        TarArchiveEntry entry = (TarArchiveEntry) element;
         attributes.setExecutable((entry.getMode() & 0100) != 0);
         attributes.setReadOnly((entry.getMode() & 0200) == 0);
         return attributes;
@@ -146,24 +146,24 @@ public class TarLeveledStructureProvider implements
 
     @Override
     public String getFullPath(Object element) {
-        String name = stripPath(((TarEntry) element).getName());
+        String name = stripPath(((TarArchiveEntry) element).getName());
         return ArchiveUtil.toValidNamesPath(name).toOSString();
     }
 
     @Override
     public String getLabel(Object element) {
         if (element.equals(root)) {
-            return ((TarEntry) element).getName();
+            return ((TarArchiveEntry) element).getName();
         }
 
-        String name = ((TarEntry) element).getName();
+        String name = ((TarArchiveEntry) element).getName();
         return stripPath(ArchiveUtil.toValidNamesPath(name).lastSegment());
     }
 
     /**
      * Returns the entry that this importer uses as the root sentinel.
      *
-     * @return TarEntry entry
+     * @return TarArchiveEntry entry
      */
     @Override
     public Object getRoot() {
@@ -199,12 +199,12 @@ public class TarLeveledStructureProvider implements
         children = new HashMap<>(1000);
 
         children.put(root, new ArrayList<>());
-        Enumeration<TarEntry> entries = tarFile.entries();
+        Enumeration<TarArchiveEntry> entries = tarFile.entries();
         while (entries.hasMoreElements()) {
-            TarEntry entry = entries.nextElement();
+            TarArchiveEntry entry = entries.nextElement();
             IPath path = new Path(entry.getName()).addTrailingSeparator();
 
-            if (entry.getFileType() == TarEntry.DIRECTORY) {
+            if (entry.isDirectory()) {
                 createContainer(path);
             } else
             {
@@ -221,7 +221,7 @@ public class TarLeveledStructureProvider implements
 
     @Override
     public boolean isFolder(Object element) {
-        return (((TarEntry) element).getFileType() == TarEntry.DIRECTORY);
+        return (((TarArchiveEntry) element).isDirectory());
     }
 
     /*
This page took 0.028131 seconds and 5 git commands to generate.